在我的客户控制器中,更新方法代码如下:
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>
答案 0 :(得分:2)
您可以通过button_to
传递隐藏的参数:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Expression Blend 4 created this (and a lot more) from some system theme on Windows 7 -->
<Style TargetType="{x:Type MenuItem}">
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
<Style.Triggers>
<Trigger Property="Role" Value="TopLevelHeader">
<Setter Property="Padding" Value="7,2,8,3"/>
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
</Trigger>
<Trigger Property="Role" Value="TopLevelItem">
<Setter Property="Padding" Value="7,2,8,3"/>
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
</Trigger>
<Trigger Property="Role" Value="SubmenuHeader">
<Setter Property="Padding" Value="5,4,2,3"/>
<!-- Changed from 2,3,2,3 -->
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}"/>
</Trigger>
<Trigger Property="Role" Value="SubmenuItem">
<Setter Property="Padding" Value="5,4,2,3"/>
<!-- Changed from 2,3,2,3 -->
</Trigger>
</Style.Triggers>
</Style>
<!-- Expression Blend 4 created this from some system theme on Windows 7 -->
<!-- Edited like in: http://devlicio.us/blogs/christopher_bennage/archive/2008/06/19/styling-separators-in-wpf.aspx -->
<!-- Decreased in height to be more platform standard -->
<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Separator}">
<Grid Margin="0,3,0,2" SnapsToDevicePixels="true">
<!-- Changed from 0,6,0,4 -->
<Rectangle Fill="#E0E0E0" Height="1" Margin="30,0,1,1"/>
<Rectangle Fill="White" Height="1" Margin="30,1,1,0"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
这将创建一个微形式,就像<%= button_to "Update", user, method: :put, params: { doc_delete: true } %>
提到的那样。虽然效率很低,但这是将数据发送到Marwen
操作的最佳方式。
-
另一种更有效的方法是定义自定义路线/动作:
update
答案 1 :(得分: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 %>
答案 2 :(得分:1)
另一种方法是,您可以使用 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
答案 3 :(得分:0)
在我的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