将关系 ID 分配给创建时的记录,而不是更新时 [英] Assign relation id to record on create, but not on update

查看:40
本文介绍了将关系 ID 分配给创建时的记录,而不是更新时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:页面有很多节.我没有使用嵌套资源,因为还有第三种模型.在我的页面#show 中,我有一个链接可以创建属于当前页面的新部分:

<%= link_to 'New Section', new_section_path(:page_id => @page) %>

在我的部分视图表单中,我有:

<%= form_for(@section) do |f|%>...<div class="field"><%= f.label :title%><br/><%= f.text_field :title %>

<%= f.hidden_​​field :page_id, :value =>参数[page_id"] %><div class="actions"><%= f.submit %>

问题当然是这样正确地将page_id传递到了新的Section表单中,但是当用户通过编辑路径返回表单时,params["page_id"]为空.我想在创建时设置 page_id 一次,并且永远不要再更改它(也许有一种方法可以创建一个条件来从表单中删除该隐藏字段,或者更简洁的方法?).

非常感谢您的帮助.

解决方案

您不想从参数中获取 page_id,而是从资源中获取.为此,您可以将页面 ID 分配给控制器中的新资源.

在您的控制器中:

def new# 实际上你可能会以不同的方式创建新的部分,# 但你懂的.## 在其他情况下,page_id 可能来自父级#(在嵌套路由中)或其他一些未由# 参数.@section = Section.new(page_id: params[:page_id])结尾

然后在您的模板中,让 page_id 自然填充:

<%= f.hidden_​​field :page_id %>

编辑表单已经可以工作了,因为记录已经有一个 page_id(只要所有部分都有页面 id,我假设它们都有).

<小时>

旁注:作为一般规则,您希望避免在视图中使用请求参数,因为这是一个混合问题(控制器 <-> 视图)并且会导致脆弱性&其他问题(如您所见).相反,让控制器收集您的资源并将它们分配给模板变量或您拥有的东西.

I have two models: Page has many Sections. I'm not using nested resources because there's a third model also. In my page#show I have a link to create a new Section belonging to the current Page:

<%= link_to 'New Section', new_section_path(:page_id => @page) %>

And in my Section view's form I have:

<%= form_for(@section) do |f| %>
.
.
.
<div class="field">
  <%= f.label :title %><br />
  <%= f.text_field :title %>
</div>
<%= f.hidden_field :page_id, :value => params["page_id"] %>
<div class="actions">
  <%= f.submit %>
</div>

The problem, of course, is that this passes the page_id into the new Section form correctly, but when the user goes back to the form through the edit path, params["page_id"] is empty. I'd like to set the page_id once on create, and never let it be changed again (perhaps there's a way to create a conditional that removes that hidden field from the form, or an even cleaner way?).

Thanks so much for your help.

解决方案

You don't want to get the page_id from the params, but rather from the resource. You'd do this by assigning the page id to the new resource in the controller.

In your controller:

def new
  # In reality you might create the new Section differently,
  # but you get the idea.
  #
  # In other scenarios the page_id might come from the parent
  # (in a nested route) or some other source not determined by
  # params.
  @section = Section.new(page_id: params[:page_id])
end

Then in your template, let the page_id populate naturally:

<%= f.hidden_field :page_id %>

The edit form will already work, as the record already has a page_id (as long as all sections have page ids, which I assume they do).


Side note: as a general rule, you want to avoid using request params in views, as this is a mix of concerns (controller <-> view) and will lead to fragility & other issues (as you've found). Rather, let the controller do your resource gathering and assign them to template variables or what have you.

这篇关于将关系 ID 分配给创建时的记录,而不是更新时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆