javascript Rails 4:将局部变量从包含 form_for 帮助程序的部分传递到 js.erb 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26938130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 4: Passing a local variable to a js.erb file from a partial containing a form_for helper
提问by rorykoehler
How do I pass a local variable from a form_for helper to a js file?
如何将局部变量从 form_for 助手传递到 js 文件?
I have the following file called _subscribe.html.erb
我有以下文件名为 _subscribe.html.erb
<%= form_for @subscription, :url => {:controller => "subscriptions", :action => "create"} do |f| %>
<div><%= hidden_field_tag :group_id, group.id %></div>
<%= f.submit "Subscribe", class: "btn btn-primary subscribe_btn" %>
<% end %>
The group local variable gets passed from a do block into this form and works (html only up until now). I want to turn this into an ajax call so i added remote: true
to the form and added the file create.js.erb to the subscriptions folder:
group 局部变量从 do 块传递到这种形式并起作用(直到现在仅 html)。我想把它变成一个 ajax 调用,所以我添加remote: true
到表单并将文件 create.js.erb 添加到订阅文件夹:
$("#new_subscription").html("<%= escape_javascript(render('subscriptions/unsubscribe', locals: { group: group } )) %>")
and this is my subscriptions controller create action:
这是我的订阅控制器创建操作:
def create
@subscription = current_user.subscriptions.create(:group_id => params[:group_id])
@subscription.save
respond_to do |format|
format.html { redirect_to groups_path, flash[:success] = "You have successfully subscribed to this group" }
format.js
end
end
However now I get this error:
但是现在我收到此错误:
ActionView::Template::Error (undefined local variable or method `group' for #<#<Class:0x007fe7ecfd4950>:0x007fe7ecfde7e8>):
1: $("#new_subscription").html("<%= escape_javascript(render('subscriptions/unsubscribe', locals: { group: group } )) %>")
app/views/subscriptions/create.js.erb:1:in `_app_views_subscriptions_create_js_erb__3184685282411376061_70317032458920'
app/controllers/subscriptions_controller.rb:8:in `create'
The group local variable is not being passed from the form_for to the create.js.erb on submit. I have tried everything in the past 4 hours but am still coming up short. How do I fix this issue and pass the local variable from the _subscribe.html.erb partial to the create.js.erb file?
组局部变量在提交时不会从 form_for 传递到 create.js.erb。我在过去 4 小时内尝试了所有方法,但仍然不够用。如何解决此问题并将局部变量从 _subscribe.html.erb 部分传递到 create.js.erb 文件?
Edit:
编辑:
More code... first my subscriptions controller create action:
更多代码...首先我的订阅控制器创建操作:
def create
@group = Group.find(params[:group_id])
@subscription = current_user.subscriptions.create(:group_id => @group.id)
@subscription.save
respond_to do |format|
format.html { redirect_to groups_path }
format.js
end
end
and destroy action:
和销毁动作:
def destroy
@group = Group.find(params[:id])
@subscription = current_user.subscriptions.find_by(group_id: @group.id)
@subscription.destroy
respond_to do |format|
format.html { redirect_to groups_path }
format.js
end
end
& create.js.erb
& create.js.erb
$("#subscribe_button").html("<%= escape_javascript(render('subscriptions/unsubscribe', locals: { group: @group } )) %>")
& destory.js.erb
& destory.js.erb
$("#unsubscribe_button").html("<%= escape_javascript(render('subscriptions/subscribe', locals: { group: @group } )) %>")
I am still getting the following error in the logs:
我仍然在日志中收到以下错误:
Started POST "/subscriptions" for 127.0.0.1 at 2014-11-20 22:23:39 +0000
Processing by SubscriptionsController#create as JS
Parameters: {"utf8"=>"?", "group_id"=>"1", "commit"=>"Subscribe"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'b0afd34150d0c021a1e4d0dfa357107a2aa59f00' LIMIT 1
Group Load (0.4ms) SELECT "groups".* FROM "groups" WHERE "groups"."id" = LIMIT 1 [["id", "1"]]
(0.2ms) BEGIN
Subscription Exists (0.6ms) SELECT 1 AS one FROM "subscriptions" WHERE ("subscriptions"."group_id" = 1 AND "subscriptions"."user_id" = 2) LIMIT 1
SQL (0.6ms) INSERT INTO "subscriptions" ("created_at", "group_id", "role", "updated_at", "user_id") VALUES (, , , , ) RETURNING "id" [["created_at", Thu, 20 Nov 2014 22:23:39 UTC +00:00], ["group_id", 1], ["role", "pending"], ["updated_at", Thu, 20 Nov 2014 22:23:39 UTC +00:00], ["user_id", 2]]
(0.4ms) COMMIT
(0.1ms) BEGIN
Subscription Exists (0.7ms) SELECT 1 AS one FROM "subscriptions" WHERE ("subscriptions"."group_id" = 1 AND "subscriptions"."id" != 96 AND "subscriptions"."user_id" = 2) LIMIT 1
(0.2ms) COMMIT
Rendered subscriptions/_unsubscribe.html.erb (100.3ms)
Rendered subscriptions/create.js.erb (101.7ms)
Completed 500 Internal Server Error in 120ms
ActionView::Template::Error (undefined local variable or method `group' for #<#<Class:0x007fdc05a244a8>:0x007fdc0545b350>):
1: <%= debug group %>
2: <div id="unsubscribe_button">
3: <%= button_to "Unsubscribe", subscription_path(group), :remote => true, :method => 'delete', class: "btn subscribe_btn" %>
4: </div>
app/views/subscriptions/_unsubscribe.html.erb:1:in `_app_views_subscriptions__unsubscribe_html_erb__4479900334468069300_70291478478720'
app/views/subscriptions/create.js.erb:1:in `_app_views_subscriptions_create_js_erb__1667797080065562395_70291478425360'
app/controllers/subscriptions_controller.rb:8:in `create'
You can clearly see that the @group is set in the create action and is successfully creating a subscription but the local variable is not being set in the js.erb files.
您可以清楚地看到在创建操作中设置了@group 并成功创建了订阅,但未在 js.erb 文件中设置局部变量。
回答by jemminger
You don't. Execution ends when the current request ends, i.e. when the form is rendered. The .js.erb view template is not called until the form is submitted, starting a new request.
你没有。当前请求结束时,即呈现表单时,执行结束。.js.erb 视图模板在表单提交之前不会被调用,开始一个新的请求。
You'll have to create an instance variable e.g. @group = however_you_get_the_group
in the controller's create action to make it visible to the .js.erb view, and reference as such: locals: { group: @group }
您必须创建一个实例变量,例如@group = however_you_get_the_group
在控制器的创建操作中,使其对 .js.erb 视图可见,并引用如下:locals: { group: @group }
回答by Benito Serna
The problem is that when you are in the create
action, you don't have the variables that you set in the previous action.
问题是,当您在create
操作中时,您没有在前一个操作中设置的变量。
So you need to set again the @group
instance variable and then use it in your create.js.erb
template.
所以你需要再次设置@group
实例变量,然后在你的create.js.erb
模板中使用它。
def create
@group = Group.find(params[:group_id])
@subscription = current_user.subscriptions.create(:group_id => @group.id)
@subscription.save
respond_to do |format|
format.html { redirect_to groups_path, flash[:success] = "You have successfully subscribed to this group" }
format.js
end
end
$("#new_subscription").html("<%= escape_javascript(render('subscriptions/unsubscribe', locals: { group: @group } )) %>")