Forum: Ruby on Rails Rails 4 ujs button only works after second click

7f959297e473789a12397cc20cd3996c?d=identicon&s=25 dasibre (Guest)
on 2013-10-03 00:07
(Received via mailing list)
Here's the code
https://gist.github.com/dasibre/6786245

*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.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-10-03 10:12
(Received via mailing list)
On 2 October 2013 23:06, dasibre <james.naadjie@gmail.com> wrote:
> <%= link_to "Pin", {controller: "pinboard_players", action: "new", id:
>       end
>   end
>
> When I click on the button, to display the select form, it takes two clicks
> for the javascript to work.

Have a look in development.log to see what is happening when you press
it the first time, then the second.

Colin
Ce43b11af679aaa142f5ba7e35e64c4b?d=identicon&s=25 Sur Max (sur)
on 2013-10-05 17:50
(Received via mailing list)
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
crimson9.com
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.