【问题标题】:RoR - Call a new Action without a View on a formRoR - 在表单上调用没有视图的新操作
【发布时间】:2014-10-21 06:42:17
【问题描述】:

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

客户端控制器.rb def update_establishments 创建_zip respond_to 做 |格式| 如果@client.update(client_params) set_zip format.html { redirect_to @client,注意:'Estabelecimento adicionado com sucesso.'} format.json {head :no_content} 别的 format.html { 渲染动作:'编辑'} format.json { 渲染 json:@client.error,状态::unprocessable_entity } 结尾 结尾 结尾 客户/_locations_form.html.erb form_for @client, :url=>{:action=>'update_establishments'} 做 |form| %> ...

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

我没有对routes.rb进行任何更改

我是否需要创建一个名为 clients/update_establishments 的视图才能完成这项工作?

【问题讨论】:

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


    【解决方案1】:

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

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

    并为您的表单更新方法“补丁”:

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

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多