Frederick Cheung wrote:
>> How can I replace a {some_partial foo:bar} match with a partial
>> rendering?
>
> the string scanner will tell you its position. Keep track of the
> relevant positions and then use String's usual methods to replace
> content between 2 positions.
Got it. Okay, I have made this method:
def dropify(content)
# create a StringScanner
s = StringScanner.new(content)
while s.scan_until /\{/
# where are we now?
from = s.pointer - 1
# locals to pass to the partial
arguments = {}
partial = s.scan /\w+/
s.skip /\s+/
while argument = s.scan( /\w+:\w+/)
# splitting the argument up into a name and a value
argument.split!(":")
name = argument.first.to_sym
value = argument.last
# putting it into the arguments hash
arguments[name] = value
s.skip /\s+/
end
# where are we now?
to = s.pointer+1
s.skip_until /\}/
# put the partial on top of the {some_partial foo:bar} match
content[from, to] = render :partial => "drops/#{partial}", :locals
=> arguments
end
# return
content
end
Let me start out by saying that both the argument, name, value,
arguments, to and from has the values they should have when I debug
them, but for some reason I'm getting an error that looks like this:
compile error
C:/.../app/views/drops/_contact_form.html.erb:-11: syntax error,
unexpected tIDENTIFIER, expecting ']'
bar=foos = local_assigns[:bar=foo]
^
C:/.../app/views/drops/_contact_form.html.erb:-7: syntax error,
unexpected tASSOC, expecting kEND
bar=>foos = local_assigns[:bar=>foo]
^
C:/.../app/views/drops/_contact_form.html.erb:-5: syntax error,
unexpected tIDENTIFIER, expecting ']'
foo=bar = local_assigns[:foo=bar]
Do anyone know what this means?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---