保存到另一个Model数据库,使用当前的Form记录ID - Rails

时间:2013-04-08 17:51:09

标签: ruby-on-rails helper model-associations

我正在尝试从表单中的text_area字段获取文本,以使用当前模型的ID保存到不同模型中的数据库。

目前,这有效但只能保存整数。如果我将文本放入“Notes”字段,则将其保存为“0”。我怀疑这是正常的,但我错过了我的拼图。这是因为我只想要'Ticket'来保存note_id,因为每个'Ticket会有多个'Notes'。

如何使用ID在Note模型中保存注释,并将note_id与此特定票证相关联?

表格 - /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

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

class Note < ActiveRecord::Base
belongs_to :ticket
attr_accessible :note, :ticket_id
end

Ticket.rb

class Ticket < ActiveRecord::Base
attr_accessible :notes_attributes
accepts_nested_attributes_for :notes
end

2 个答案:

答案 0 :(得分:4)

这是因为note_idinteger类型。

使用nested modelsRefer this for Nested Models

<强>型号:

class Ticket < ActiveRecord::Base
  has_many :notes
  attr_accessible :note_id, :notes_attributes
  accepts_nested_attributes_for :notes
end

查看:

<%= form_for(@ticket) do |f| %>
    <%= f.fields_for :notes do |u|%>
      <%= u.label :note %>
      <%= u.text_area :note %>
    <% end %>
    <%= f.submit 'Submit' %>
<% end %>

答案 1 :(得分:2)

您拥有的是嵌套关联,Ticket为“父级”。该关联受note_id模型中的Noteid的{​​{1}}(主键)之间的链接约束。您现在正在做的是手动操作该数字关联。知道Ticket列应该是一个整数的Rails正在获取您尝试插入的文本并将其转换为数字(在这种情况下为零)。由于这个原因,你现在可能已经收到了一堆孤立的行。

最终,为了完成您要完成的任务,您的表单需要为该关联模型提供字段。您可以使用note_id模型中的accepts_nested_attributes_for来解决此问题。像这样:

Ticket

在您的表单中,您可以轻松创建嵌套表单,如下所示:

class Ticket < ActiveRecord::Base
    has_many :notes
    accepts_nested_attributes_for :notes
end

修改几乎忘了:查看此Railscast from Ryan Bates处理嵌套属性

编辑2 正如codeit指出的那样,您不需要Ticket中的<%= 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 %> 。由于您已指出attr_accessible :note_id有多个Ticket,并且Notes属于Note,因此外键列将显示在Ticket模型中Note,你已经拥有了。在票证模型中使用note_id是无用的,也是无意义的,因为has_many描述了一个复数关系(不能用单个列表示)。