RoR - 在表单上调用没有视图的新操作

时间:2014-08-27 15:30:59

标签: forms ruby-on-rails-4 routes

我在名为ClientsController的{​​{1}}上创建了一个新操作。我有update_establishments的表单,我想在不生成新视图的情况下调用该操作。该操作将生成一个新视图。

clients_controller.rb
     def update_establishments
       create_zip
       respond_to do |format|
       if @client.update(client_params)
         set_zip
         format.html { redirect_to @client, notice:'Estabelecimento adicionado com sucesso.'}
         format.json {head :no_content}
       else
         format.html { render action: 'edit'}
         format.json { render json: @client.error, status: :unprocessable_entity }
       end
    end
  end    


clients/_locations_form.html.erb

     form_for @client, :url=>{:action=>'update_establishments'}  do |form| %>
    ...

这会引发错误Client 解释ActionController::UrlGenerationError in Clients#add_establishments

我对No route matches {:action=>"update_establishments", :id=>"35", :controller=>"clients"}

没有做任何更改

我是否需要创建名为routes.rb的视图才能使其正常工作?

1 个答案:

答案 0 :(得分:1)

我认为您需要为routes.rb文件添加新路线,例如:

resources :clients do
  member do
   patch 'update_establishments'
  end
end

并为您的表单更新方法'patch':

form_for @client, :url=>{:action=>'update_establishments'}, html: {method: "patch"} do |form|