javascript - Rails 4: Passing a local variable to a js.erb file from a partial containing a form_for helper -



javascript - Rails 4: Passing a local variable to a js.erb file from a partial containing a form_for helper -

how pass local variable form_for helper js file?

i have next file called _subscribe.html.erb

<%= form_for @subscription, :url => {:controller => "subscriptions", :action => "create"} |f| %> <div><%= hidden_field_tag :group_id, group.id %></div> <%= f.submit "subscribe", class: "btn btn-primary subscribe_btn" %> <% end %>

the grouping local variable gets passed block form , works (html until now). want turn ajax phone call added remote: true form , added file create.js.erb subscriptions folder:

$("#new_subscription").html("<%= escape_javascript(render('subscriptions/unsubscribe', locals: { group: grouping } )) %>")

and subscriptions controller create action:

def create @subscription = current_user.subscriptions.create(:group_id => params[:group_id]) @subscription.save respond_to |format| format.html { redirect_to groups_path, flash[:success] = "you have subscribed group" } format.js end end

however error:

actionview::template::error (undefined local variable or method `group' #<#<class:0x007fe7ecfd4950>:0x007fe7ecfde7e8>): 1: $("#new_subscription").html("<%= escape_javascript(render('subscriptions/unsubscribe', locals: { group: grouping } )) %>") 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 grouping local variable not beingness passed form_for create.js.erb on submit. have tried in past 4 hours still coming short. how prepare issue , pass local variable _subscribe.html.erb partial create.js.erb file?

edit:

more code... first 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 |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 |format| format.html { redirect_to groups_path } format.js end end

& create.js.erb

$("#subscribe_button").html("<%= escape_javascript(render('subscriptions/unsubscribe', locals: { group: @group } )) %>")

& destory.js.erb

$("#unsubscribe_button").html("<%= escape_javascript(render('subscriptions/subscribe', locals: { group: @group } )) %>")

i still getting next error in logs:

started post "/subscriptions" 127.0.0.1 @ 2014-11-20 22:23:39 +0000 processing subscriptionscontroller#create js parameters: {"utf8"=>"✓", "group_id"=>"1", "commit"=>"subscribe"} user load (0.9ms) select "users".* "users" "users"."remember_token" = 'b0afd34150d0c021a1e4d0dfa357107a2aa59f00' limit 1 grouping load (0.4ms) select "groups".* "groups" "groups"."id" = $1 limit 1 [["id", "1"]] (0.2ms) begin subscription exists (0.6ms) select 1 1 "subscriptions" ("subscriptions"."group_id" = 1 , "subscriptions"."user_id" = 2) limit 1 sql (0.6ms) insert "subscriptions" ("created_at", "group_id", "role", "updated_at", "user_id") values ($1, $2, $3, $4, $5) 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 1 "subscriptions" ("subscriptions"."group_id" = 1 , "subscriptions"."id" != 96 , "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' #<#<class:0x007fdc05a244a8>:0x007fdc0545b350>): 1: <%= debug grouping %> 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 see @group set in create action , creating subscription local variable not beingness set in js.erb files.

you don't. execution ends when current request ends, i.e. when form rendered. .js.erb view template not called until form submitted, starting new request.

you'll have create instance variable e.g. @group = however_you_get_the_group in controller's create action create visible .js.erb view, , reference such: locals: { group: @group }

javascript jquery ruby-on-rails ajax ruby-on-rails-4

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -