【问题标题】:Rails 4 : Table Boolean Column Update Using "link_to "with a specific value "TRUE" alwaysRails 4:始终使用具有特定值“TRUE”的“link_to”更新表布尔列
【发布时间】:2016-04-27 11:06:19
【问题描述】:

在我的客户控制器中,更新方法代码如下:

  def update
    @customer= Customer.find(params[:id])
    if @customer.update_attributes(customer_params)
      redirect_to  customers_path
    else
      render 'edit'
    end
  end

在我的客户索引页面视图中,我计划添加一个“link_to”链接,如果单击它,则该特定客户字段“doc_delete”应更新为值“TRUE”。

<td><%= link_to "[Update", *************what is here ?******** method: :put %></td>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2


    【解决方案1】:

    你需要一个表格来为你做这件事

    <% unless customer.doc_delete? %>
      <%= form_for customer do |f| %>
        <%= f.hidden_field_tag :doc_delete, true %>
        <%= f.submit %>
      <% end %>
    <% end %>
    

    在哪里插入这个表格?

    如果您使用以下方式渲染客户:

       <%=render @costumers %>
    

    然后您将在/customers/_customer.html.erb中添加表单

    如果你手动循环它们:

    <% @customers.each do |customer| %>
      <%=customer.full_name %>
      ## Here you can add the form
    <% end %>
    

    【讨论】:

    • 我在我的客户索引中插入了这个link_to更新,它列出了大量的客户,所以如果我把这个表格插入我的索引会影响吗?
    • 您需要同时更新一个costumer 还是全部?
    • 感谢好友的支持 :)
    【解决方案2】:

    你可以通过button_to传递隐藏参数:

    <%= button_to "Update", user, method: :put, params: { doc_delete: true } %>
    

    这将创建一个微型表单,就像Marwen 所暗示的那样。虽然效率很低,但这将是向您的update 操作发送数据的最佳方式。

    --

    另一种更有效的方法是定义自定义路由/动作:

    #config/routes.rb
    resources :customers do
       patch :doc_delete, on: :member #-> url.com/users/:id/doc_delete
    end
    
    #app/controllers/customers_controller.rb
    class CustomersController < ApplicationController
       def doc_delete
          @customer = Customer.find params[:id]
          redirect_to customers_path if @customer.update doc_delete: true
       end
    end
    
    #app/views/customers/index.html.erb
    <% @customers.each do |customer| %>
       <%= link_to "Update", customer_doc_delete_path(customer) %>
    <% end %>
    

    【讨论】:

    • 谢谢丰富,按照我发布的答案的方式做呢?这样做好不好?
    • 是的,但是您需要了解如何传递 doc_delete 属性。你可以通过链接做到这一点,但它会很丑:link_to "Update", user_path(user, doc_delete: true), method: :patch
    • 是否可以像使用 button_to 一样通过 link_to 传递参数?
    • 不,因为link_to 是一个HTML 链接&lt;a href="...button_to 创建一个表单(就像@marwen 发布的内容)
    • 毫无疑问,我每天都在即兴创作 :)
    【解决方案3】:

    另一种方式,您可以使用 Ajax

    #app/views/customers/index.html.erb
    <% @customers.each do |customer| %>
       <% if !customer.doc_delete == true %>
         <%= link_to "Update", customer_doc_delete_path(customer), remote: true %>
       <% else %>
         <%= Updated %>
       <% end %>
    <% end %>
    
    #config/routes.rb
    resources :customers do
       patch :doc_delete, on: :member #-> url.com/customers/:id/doc_delete
    end
    
    #app/controllers/customers_controller.rb
    class CustomersController < ApplicationController
       def doc_delete
          @customer = Customer.find params[:id]
    
          if @customer.update doc_delete: true
            respond_to do | format |  
              format.js {render :nothing => true} 
            end 
          end
       end
    end
    

    【讨论】:

      【解决方案4】:

      在我的 index.html 中

            <td>
              <%= hidden_field_tag 'delete_present', :value => "present" %>
              <%=link_to "[update]", customer_path(customer, :doc_delete => true), :method => :put, :confirm => "Are you sure?" %>
            </td>
      

      在我的客户控制器中

      def update
            if params[:doc_delete].present?
              @customer= Customer.find(params[:id])
              @customer.doc_delete=true
              @customer.save
              redirect_to  customers_path
            else  
              @customer= Customer.find(params[:id])
              if @customer.update_attributes(customer_params)
                redirect_to  customers_path
              else
                render 'edit'
              end
            end
        end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-05
        • 2014-09-07
        • 1970-01-01
        • 2019-03-25
        • 2011-02-27
        • 2012-09-26
        • 1970-01-01
        • 2013-04-21
        相关资源
        最近更新 更多