Rails 3 form_for ajax call

∥☆過路亽.° 提交于 2019-12-24 00:52:20

问题


I have method in my controller:

def work_here
 @company = Company.find(params[:id])
 current_user.work_apply(current_user, @company)
 redirect_to company_path
end

On my view:

<%= render 'companies/work_form' if signed_in? %>

_work_form.html.erb:

<%= form_for company, :remote => true, :url => { :controller => "companies", :action => "work_here" } do |f| %>
<%= f.hidden_field :id, :value => company.id %>
<%= f.submit "I working here" %>

<% end %>

And I have a work_here.js.erb file:

$("#work_at_form").html("<%= escape_javascript("render('companies/works')") %>")

But my form works without ajax request(in other pages ajax forks fine), my js.erb file never use. Can anyone give me advise? Thanks.


回答1:


The work_here.js.erb can't be read because you never call it. A redirect is allways do. render it when the request is js format.

def work_here
 @company = Company.find(params[:id])
 current_user.work_apply(current_user, @company)
 respond_to do |format|
   format.html { redirect_to company_path }
   format.js { render }
 end
end


来源:https://stackoverflow.com/questions/4791118/rails-3-form-for-ajax-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!