Rails 4 ujs button only works after second click

39 views
Skip to first unread message

dasibre

unread,
Oct 2, 2013, 6:06:00 PM10/2/13
to rubyonra...@googlegroups.com
Here's the code

new.js.erb file has the following js code
 
$('a.pinbutton').on('click', function() {
$(this).parent().append('<%= escape_javascript(render("select")) %>')
});
 
here's the player index with the new link
<%= link_to "Pin", {controller: "pinboard_players", action: "new", id: player}, remote: true, class: "pinbutton", id: "#{player.id}" %>
 
controller new action action:
def new
session[:player] = params[:id]
@pinboard_player = PinboardPlayer.new(player_id: params[:id])
@current_user_pinboards = get_user_pinboards
respond_to do |format|
format.html
format.js
end
end

When I click on the button, to display the select form, it takes two clicks for the javascript to work.

Colin Law

unread,
Oct 3, 2013, 4:10:43 AM10/3/13
to rubyonra...@googlegroups.com
Have a look in development.log to see what is happening when you press
it the first time, then the second.

Colin

Sur Max

unread,
Oct 3, 2013, 7:44:02 AM10/3/13
to rubyonra...@googlegroups.com
Your view that has the link link_to 'Pin'  already binds the event and points to the new action so you won't need to bind the event again to this link in new.js.erb.

Conclusion is your new.js.erb has problems.
Replace your new.js.erb code with the following:

 $('a.pinbutton').parent().append('<%= escape_javascript(render("select")) %>')


regards,
Sur
Reply all
Reply to author
Forward
0 new messages