为什么我的remote_form_tag有:action定义没有发布动作?

时间:2010-10-26 00:10:38

标签: ruby-on-rails ajax remote-forms

这是我在remote_form_tag中使用的:

<% form_remote_tag(:url => {:controller => '/companies', :action => 'update'},
      :update => 'tags') do  %>
      <%= text_field :company, :tag_list %> 
       <%= submit_tag 'Save' %> 
  <% end %>

这是在Company.view中,其中Company是启用了acts_as_taggable_on的模型。

我的期望是,通过ajax,发布公司/ 10 /更新

但是,相反,发布的内容是:

http://localhost:3000/companies/10

,回复是:

No action responded to 10. Actions: create, destroy, edit, email_this_week, index, new, show, and update

这是CompaniesController中的更新方法:

 def update
    @company = Company.find(params[:id])
    if request.xhr?
      # add the given tag to the company
      @company.tags << params[:company][:taglist]
      @company.save
      render :partial => 'tags'
    else
      if @company.update_attributes(params[:company])
        flash[:notice] = "Successfully updated company."
        redirect_to @company
      else
        render :action => 'edit'
      end
    end
  end

帮助...?

     DELETE /companies/:company_id/contacts/:id(.:forma
   {:controller=>"contacts", :action=>"destroy"}
            companies GET    /companies(.:format)
   {:controller=>"companies", :action=>"index"}
                      POST   /companies(.:format)
   {:controller=>"companies", :action=>"create"}
          new_company GET    /companies/new(.:format)
   {:controller=>"companies", :action=>"new"}
         edit_company GET    /companies/:id/edit(.:format)
   {:controller=>"companies", :action=>"edit"}
              company GET    /companies/:id(.:format)
   {:controller=>"companies", :action=>"show"}
                      PUT    /companies/:id(.:format)
   {:controller=>"companies", :action=>"update"}
                      DELETE /companies/:id(.:format)
   {:controller=>"companies", :action=>"destroy"}

1 个答案:

答案 0 :(得分:0)

当您使用ID 10更新公司等资源时,Rails将使用RESTful路径:

PUT /companies/10

在路由您的请求时会考虑PUT方法。取自您的路线:

PUT    /companies/:id(.:format)
  {:controller=>"companies", :action=>"update"}

这是Rails的正确行为。只需在update中实施CompaniesController方法。

如果您需要有关Rails中RESTful路由的更多信息,请查看此文档:http://guides.rubyonrails.org/routing.html