简体   繁体   English

如何在其他模型的显示视图上编辑模型

[英]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. 我需要,当用户单击位置编辑链接时,他将重定向以showCustomer模型”视图,但是表单已经包含单击位置的数据。 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.

相关问题 如何从另一个关联模型的视图编辑模型的属性 - How to edit a model's attribute from the view of another associated model 在模型视图中查看其他模型的列 - view column of other model in a view of a model 如何在视图中显示来自关联模型的数据? - How to show data from associated model in a view? 如何在视图中显示模型错误? - How can I show the model errors in the view? 如何在Rails中的另一个模型的“显示”页面中呈现“编辑”表单部分 - How to render a “Edit” form partial in the “Show” page of another model in Rails 使用启用/禁用在显示路线中编辑模型 - Edit a model in the show route using enable/disable 不能在嵌套模型编辑页面的_form视图中显示图像 - can't show an Image in my _form view in a nested model edit page 验证用户模型上的密码在编辑视图中不起作用 - validates password on user model not working in edit view 在Rails中,如何将一个模型的“新”视图从另一模型移到“显示”视图? - In Rails, how can one move a “new” view for one model to a “show” view from another model? RoR:如何在桌上显示/打印其他模型的数据? - RoR: How to show/print data from other model on a table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM