[英]Saving to another Model database, with current Form record ID - Rails
I'm trying to get the text from a text_area
field in a form to save to a database in a different Model with the current Model's ID. 我正在尝试从表单的
text_area
字段中获取文本,以使用当前模型的ID将其保存到其他模型中的数据库中。
Currently, this works but only will save integers. 目前,此方法有效,但只会保存整数。 If I put text into the 'Notes' field, then its saves it as a '0'.
如果我在“注释”字段中输入文字,则将其另存为“ 0”。 I suspect this is working correctly but I'm missing a piece to my puzzle.
我怀疑这可以正常工作,但我的拼图缺少了一块。 This is because I only want the 'Ticket' to save the note_id because I will have multiple 'Notes' per 'Ticket.
这是因为我只希望'Ticket'保存note_id,因为每个'Ticket'将有多个'Notes'。
How can I get the Note to save in the Note Model, with an ID, and associate that note_id
with this specific ticket? 如何获取带有ID的Note并保存在Note模型中,并将该
note_id
与该特定票证相关联?
Form - /app/views/tickets/_form.html.erb 表格-/ app/views/tickets/_form.html.erb
<%= form_for(@ticket) do |f| %>
<% if @ticket.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@ticket.errors.count, "error") %> prohibited this ticket from being saved:</h2>
<ul>
<% @ticket.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.fields_for :notes do |u|%>
<%= u.label :note %>
<%= u.text_area :note, :size => "101x4", :placeholder => "Leave notes here." %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Tickets_controller.rb Tickets_controller.rb
class TicketsController < ApplicationController
# GET /tickets
# GET /tickets.json
def index
@tickets = Ticket.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @tickets }
end
end
# GET /tickets/1
# GET /tickets/1.json
def show
@ticket = Ticket.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @ticket }
end
end
# GET /tickets/new
# GET /tickets/new.json
def new
@ticket = Ticket.new
@ticket.notes.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @ticket }
end
end
# GET /tickets/1/edit
def edit
@ticket = Ticket.find(params[:id])
end
# POST /tickets
# POST /tickets.json
def create
@ticket = Ticket.new(params[:ticket])
respond_to do |format|
if @ticket.save
format.html { redirect_to @ticket, notice: 'Ticket was successfully created.' }
format.json { render json: @ticket, status: :created, location: @ticket }
else
format.html { render action: "new" }
format.json { render json: @ticket.errors, status: :unprocessable_entity }
end
end
end
# PUT /tickets/1
# PUT /tickets/1.json
def update
@ticket = Ticket.find(params[:id])
respond_to do |format|
if @ticket.update_attributes(params[:ticket])
format.html { redirect_to @ticket, notice: 'Ticket was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @ticket.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tickets/1
# DELETE /tickets/1.json
def destroy
@ticket = Ticket.find(params[:id])
@ticket.destroy
respond_to do |format|
format.html { redirect_to tickets_url }
format.json { head :no_content }
end
end
end
Note.rb 注意.rb
class Note < ActiveRecord::Base
belongs_to :ticket
attr_accessible :note, :ticket_id
end
Ticket.rb Ticket.rb
class Ticket < ActiveRecord::Base
attr_accessible :notes_attributes
accepts_nested_attributes_for :notes
end
It is because note_id
is an integer
type. 这是因为
note_id
是integer
类型。
Use nested models
: Refer this for Nested Models 使用
nested models
: nested models
请参考
Model: 模型:
class Ticket < ActiveRecord::Base
has_many :notes
attr_accessible :note_id, :notes_attributes
accepts_nested_attributes_for :notes
end
View: 视图:
<%= form_for(@ticket) do |f| %>
<%= f.fields_for :notes do |u|%>
<%= u.label :note %>
<%= u.text_area :note %>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
What you have is a nested association, with Ticket
as the "parent". 您拥有的是一个嵌套关联,其中
Ticket
作为“父”。 The association is governed by the link from note_id
in the Note
model to the id
(primary key) of the Ticket
. 关联由从
Note
模型中的note_id
到Ticket
的id
(主键)的链接控制。 What you're presently doing right now is manually manipulating that numeric association. 您当前正在做的是手动操作该数字关联。 Rails, knowing that the
note_id
column is supposed to be an integer, is taking the text you're trying to insert and turning it in to a number (zero in this case). Rails知道
note_id
列应该是整数,因此将您要插入的文本转换为数字(在这种情况下为零)。 You've probably got a bunch of orphaned rows right now because of this. 因此,您现在可能有一堆孤立的行。
Ultimately, in order to accomplish what you're trying to do, your form will need to provide fields for that associated model. 最终,为了完成您要执行的操作,您的表单将需要为该关联的模型提供字段。 One way you can handle this is by using the
accepts_nested_attributes_for
in your Ticket
model. 处理此问题的一种方法是在
Ticket
模型中使用accepts_nested_attributes_for
。 Like so: 像这样:
class Ticket < ActiveRecord::Base
has_many :notes
accepts_nested_attributes_for :notes
end
And in your form, you can easily create a nested form like so: 在您的表单中,您可以轻松地创建一个嵌套表单,如下所示:
<%= form_for(@ticket) do |f| %>
<div class="field">
<%= f.fields_for :notes do |f_notes|%>
<%= f_notes.label :note %><br />
<%= f_notes.text_area :note, :size => "101x4", :placeholder => "Please leave notes here."%>
<% end %>
</div>
<% end %>
Edit Almost forgot: Check out this Railscast from Ryan Bates dealing with Nested Attributes 编辑几乎忘了: 从Ryan Bates处处理嵌套属性的Railscast
Edit 2 As codeit pointed out, you don't need the attr_accessible :note_id
in Ticket. 编辑2正如代码专家指出的那样,您在Ticket中不需要
attr_accessible :note_id
。 Since you've indicated that a Ticket
has many Notes
, and that Note
belongs to Ticket
, the foreign key column will appear in the Note
model as ticket_id
, which you already have. 既然你已经表示,一
Ticket
有许多Notes
,并Note
属于Ticket
,外键列将出现在Note
模型ticket_id
,你已经有了。 Having note_id in the ticket model is useless, and also nonsensical since has_many describes a plural relationship (which can't be expressed with a single column). 在票证模型中使用note_id是没有用的,而且也是荒谬的,因为has_many描述了复数关系(不能用单列表示)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.