[英]How to edit model on show view of other model
I have the model Customer
, and binded by has_many
model Location
. 我有模型Customer
,并受has_many
模型Location
has_many
。 This is customer and his locations (adresses). 这是客户及其位置(地址)。 After creating a new customer application redirects on show
view of Customer
model (shows all data just entered). 创建新的客户应用程序后,将在Customer
模型的show
视图上重定向(显示刚刚输入的所有数据)。 On show
view I have form for location add. 在show
视图上,我有用于添加位置的表格。 There are already entered locations shows on this view and each of them has the link to edit each separate location. 此视图上已经输入了位置,每个位置都有链接来编辑每个单独的位置。 I need, when user click on location edit link, he redirects to show
view of Customer
model, but the form already contains the data of clicked location. 我需要,当用户单击位置编辑链接时,他将重定向以show
“ Customer
模型”视图,但是表单已经包含单击位置的数据。 Show
view code of Customer
model: Show
Customer
模型的查看代码:
<p>Customer info:<br>
<strong><%= @customer.name %></strong><br />
<%= @customer.kind.name %><br />
<%= @customer.adres %><br />
<%= @customer.phone %><br />
<%= @customer.comment %><br />
</p>
<h3>Delivery location:</h3>
<% if @locations %>
<ul>
<% @locations.each do |loc| %>
<li>
<%= loc.adres %>
<%= loc.phone %>
<%= loc.comment %>
</li>
<% end %>
</ul>
<% end %>
<%= form_for Location.new do |l| %>
<%= l.text_field :adres %><br>
<%= l.text_field :phone %><br>
<%= l.text_field :comment %><br>
<%= l.text_field :customer_id, type: "hidden", value: @customer.id %><br>
<%= l.submit "Add" %>
<% end %>
<%= link_to "Home", root_path %>
You'd just set @location
depending on whether specific params have been sent: 您只需根据是否已发送特定的参数来设置@location
:
#app/controllers/customers_controller.rb
class CustomersController < ApplicationController
def show
@customer = Customer.find params[:id]
@location = params[:location_id] ? @customer.locations.find(params[:location_id]) : @customer.locations.new
end
end
This would allow you to populate the form as follows: 这将使您可以如下填写表格:
#app/views/customers/show.html.erb
Customer info:<br>
<%= content_tag :strong, @customer.name %>
<%= @customer.kind.name %>
<%= @customer.adres %>
<%= @customer.phone %>
<%= @customer.comment %>
<h3>Delivery location:</h3>
<% if @customer.locations %>
<ul>
<% @customer.locations.each do |loc| %>
<%= content_tag :li do %>
<%= loc.adres %>
<%= loc.phone %>
<%= loc.comment %>
<%= link_to "Edit", customer_path(@customer, location_id: loc.id) %>
<% end %>
<% end %>
</ul>
<% end %>
<%= form_for @location do |l| %>
<%= l.text_field :adres %><br>
<%= l.text_field :phone %><br>
<%= l.text_field :comment %><br>
<%= l.text_field :customer_id, type: "hidden", value: @customer.id %><br>
<%= l.submit %>
<% end %>
-- -
To manage the routes
(to pass location_id
), you'll be best creating a custom route: 要管理routes
(传递location_id
),最好创建一个自定义路线:
#config/routes.rb
resources :customers do
get ":location_id", on: :member, action: :show, as: :location #-> url.com/customers/:id/:location_id
end
This will mean you'll have to reference the other link in your view: 这意味着您必须在视图中引用其他链接:
<%= link_to "Edit", customer_location_path(@customer, loc.id) %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.