I want to use the value of an input tag开发者_开发知识库 in rjs
to do:
<%= button_to_function "add tag" do |page|
page.insert_html :bottom, :selected_tags, :partial => 'tag', :object => Tag.new(:name => "#{page[:tag_name].value}" )
end %>
But it throws an alert:
RJS error:
TypeError: $("tag_name").value is not a function
Is there a way to use rjs syntax to read values of tag elements ?
Wait a second:
<%= button_to_function "add tag" do |page|
page.insert_html :bottom, :selected_tags, :partial => 'tag', :object => Tag.new(:name => "#{page[:tag_name].value}" )
end %>
button_to_function and the page.insert_html happen when the pages gets rendered (object creation, rendering, etc). Only the actual INSERT happens when the user clicks the button.
I'd suggest that you:
- use a link_to_remote or a form_to_remoe (t
- create the Tag object then in a action on the server side, and render the partial
- send it back to the client and add it to the selected_tags list by rjs (render update do page)
Cheers Reto
@reto is right, you have to distinguish client and server-side. The JS generated in the link_to_button
block will be executed at the client, but the partial is rendered at the server, so there is no way your code can work.
The link_to_remove
option proposed is easy to implement, but it could be improved, as you would be connecting the server for something you already know when the view is rendered. Don't fear JavaScript and:
- server: send a HTML template
- client: substitute the values when the button is pressed
- client: insert the new HTML block in the container.
There are many ways of accomplishing that, play a little bit with it to figure it out.
精彩评论