Passing a local variable to a partial that is rendered after the view has already loaded

徘徊边缘 提交于 2019-12-04 12:49:11

It's not working because f only exists in the context of your controller action.

When a user clicks your link, a new HTTP request is made for your javascript. The controller action handling this request is different from the one that rendered the page, so you need to set f in this controller action and pass it as a local to addRoundLink.js.erb.

Example code

  • I'm assuming f is determine by your model object/instance.

In your controller

   def add_round
        model = MyModel.find params[:id]
        f = model.determine_f

        render :partial => 'add_round', :locals => { :f => f }
   end

In your new.html.erb

<%= link_to "Add Round", add_round_path(@model), :remote => true %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!