首页 文章

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

提问于
浏览
0

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

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| %>
    ...

这会引发错误 ActionController::UrlGenerationError in Clients#add_establishments ,解释 No route matches {:action=>"update_establishments", :id=>"35", :controller=>"clients"}

我对 routes.rb 没有做任何改动

我是否需要创建一个名为 clients/update_establishments 的视图才能使其工作?

1 回答

  • 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|

相关问题