简体   繁体   English

Rails form_tag路由到非繁琐的操作,而没有在浏览器的URL中包含操作名称

[英]Rails form_tag routing to non-Restful action without including name of action in url on the browser

I have a form_tag in index.html.erb which must route to an action update_multiple for updating DB with selection in the form. 我在index.html.erb有一个form_tag,它必须路由到一个update_multiple动作,以便通过选择表单来更新数据库。 But there are multiple issues all interconnected to form and routes. 但是存在多个相互联系的形式和路线问题。

Issue 1: despite giving the action explicitly and method as PUT in form_tag, it takes _method as delete and goes on to destroy action. 问题1:尽管在form_tag中将操作和方法明确地指定为PUT,但它仍将_method用作删除并继续销毁操作。 I'm not sure if index form doesn't take non-restful action. 我不确定索引表是否不会采取非静态操作。

Upon clicking submit the error is: 单击提交后,错误为:

{"utf8"=>"✓","_method"=>"delete","authenticity_token"=>"e/NgCpW+PQ==","true"=>"{:value=>nil}","false"=>"{:value=>nil}","cv_attachment_id"=>"33","commit"=>"Select Main","user_id"=>"97","id"=>"update_multiple"} {“ utf8” =>“✓”,“ _ method” =>“删除”,“ authenticity_token” =>“ e / NgCpW + PQ ==”,“ true” =>“ {:value => nil}”,“ false“ =>” {:value => nil}“,” cv_attachment_id“ =>” 33“,” commit“ =>” Select Main“,” user_id“ =>” 97“,” id“ =>” update_multiple“ }

the cv_attachment_id is of the record selected but what is the last id and what should it be for normal operation and what are true and false which are both set to nil? cv_attachment_id是所选择的记录,但是最后一个id是什么,对于正常操作它应该是什么,以及将true和false都设置为nil的是什么?

form code in index.html.erb index.html.erb中的表单代码

<%= form_tag update_multiple_user_cv_attachments_path, method: :put, action: :update_multiple do %>
      <table class="table table-bordered table-striped">
        <thead>
          <tr>
            <th> Select a CV </th>
            <th> Resume Name </th>
            <th> Creation Date </th>
            <th> Delete Resume </th>
          </tr>
        </thead>
        <tbody>
        <% @cv_attachments.each do |cv_attachment| %>
        <%= hidden_field_tag cv_attachment.main, :value => params[:main] %>
          <tr>
            <td><%= radio_button_tag "cv_attachment_ids[]", cv_attachment.id, cv_attachment.main %> </td>
            <td><%= cv_attachment.attachment.file.basename %></td>
            <td><%= cv_attachment.posting_date %></td>
            <td><%= button_to "Delete", user_cv_attachment_path(current_user, cv_attachment), method: "delete", data: { confirm: 'Are you sure?' }, class: "btn btn-danger btn-outline btn-md" %></td>
          </tr>
        <% end %>
        </tbody>
      </table>
      <br>
      <%= submit_tag "Select Main", :class =>'button' %>
     <% end %>

routes.rb routes.rb

#resources :cv_attachments, only: [:index, :show, :edit, :create, :new, :destroy,]

resources :cv_attachments do
  collection do
    put 'update_multiple'
  end
end

Issue 2: As you can see I have specified 2 cv_attachments for Restful and non-restful actions and because it screwed up the routes, I had to comment the Restful ones so how to have both of them. 问题2:如您所见,我为“宁静”和“非宁静”动作指定了2个cv_attachments,并且由于它弄乱了路线,因此我不得不评论“宁静”的路线,以及如何同时使用这两个路线。

Issue 3: Also by doing collection and put it's adding update_multiple to end of url after clicking submit like: 问题3:同样,通过执行收集操作并在单击“提交”后将update_multiple添加到url的末尾,如下所示:

http://localhost:3000/users/97/cv_attachments/update_multiple http:// localhost:3000 / users / 97 / cv_attachments / update_multiple

but I do not want the action name in url: http://localhost:3000/users/97/cv_attachments 但我不希望网址中包含操作名称: http:// localhost:3000 / users / 97 / cv_attachments

I would be inclined to create a member route on users. 我倾向于在用户上创建成员路由。

routes 路线

 resources :users do
  member do
    post :update_main_attachment
  end
end

form 形成

<%= form_tag update_main_attachment_user_path do %>
  <thead></thead>
  <tbody>
    <% @cv_attachments.each do |cv_attachment| %>
      <% if cv_attachment.main %>
        <%= hidden_field_tag "ex_main_cv", cv_attachment.id %>
      <% end %>
      <tr>
        <td><%= radio_button_tag "main_cv", cv_attachment.id, cv_attachment.main %> </td>
        <td><%= cv_attachment.attachment.file.basename %></td>
      </tr>
    <% end %>
  </tbody>
<% end %>

To answer your 2nd issue, you can nest collection routes under resourceful routes: 要回答第二个问题,您可以在资源丰富的路由下嵌套收集路由:

resources :cv_attachments, only: [:index, :show, :edit, :create, :new, :destroy,] do
  collection do
    put 'update_multiple'
  end
end

For your 3rd issue, you can give the collection route a name using the :as option. 对于第3期,您可以使用:as选项为集合路由指定名称。 Refer to the Rails docs . 请参考Rails文档

@Margo thx again for I've taken your inputs. 再次@Margo thx,因为我接受了您的输入。 I figured out why form went to destroy instead of going to update. 我弄清楚了为什么表格要销毁而不是更新。 Actually there was a button_to for deleting attachment and as soon as I changed to link_to the form went to the right controller method.And I never suspected an innocuous looking button which was not 'form Submit'-button, was the culprit! 实际上,有一个button_to用于删除附件,当我将其更改为link_to时,表单就转到了正确的控制器方法,而且我从来没有怀疑罪魁祸首!

Here's the solution: routes.rb 解决方法如下:routes.rb

resources :cv_attachments, only: [:index, :show, :create, :new, :destroy] do
collection do
  put 'update_multiple'
end

end 结束

index.html--only giving part of form index.html-仅提供部分表单

<tbody>
          <% @cv_attachments.each do |cv_attachment| %>
            <% if cv_attachment.main %>
                <%= hidden_field_tag "ex_main", cv_attachment.id %>
            <% end %>
            <tr>
              <td><%= radio_button_tag "new_main", cv_attachment.id, cv_attachment.main, :id => "#{cv_attachment.id}"%> </td>
              <td><%= cv_attachment.attachment.file.basename %></td>
              <td><%= cv_attachment.posting_date %></td>
              <td><%= link_to "Delete", user_cv_attachment_path(current_user, cv_attachment), method: "delete", data: { confirm: 'Are you sure?' }, class: "btn btn-danger btn-outline btn-md" %></td>
            </tr>
          <% end %>
        </tbody>

Controller: 控制器:

def update_multiple
if request.put?
  if params["ex_main"] != params["new_main"]
    CvAttachment.find(params[:ex_main]).toggle!(:main)
    CvAttachment.find(params[:new_main]).toggle!(:main)
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM