用于belongs_to关联导轨的嵌套属性 [英] Nested attributes for belongs_to association rails

查看:162
本文介绍了用于belongs_to关联导轨的嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,投诉和公司。对公司和公司 has_many 抱怨 belongs_to acceptions_attributes 抱怨。

 #Models 

class抱怨< ActiveRecord :: Base
attr_accessible:投诉,:日期,:已解决

belongs_to:user,:class_name => 'User',:foreign_key => 'id'
belongs_to:company,:class_name => 'Company',:foreign_key => 'id'
has_many:回复

accept_nested_attributes_for:company

end

class公司< ActiveRecord :: Base
attr_accessible:name

has_many:complaints,:class_name => '抱怨',:foreign_key => 'id'
has_many:branches,:class_name => 'Branch',:foreign_key => 'id'
belongs_to:行业

结束

投诉控制器我尝试在新方法中构建公司。

 #投诉控制器

类ComplaintsController< ; ApplicationController
...
def new
@complaint = Complaint.new
@ complaint.build_company

respond_to do | format |
format.html#new.html.erb
format.json {render json:@complaint}
end
end
...
end

在表格中,我添加了一个用于向公司添加名称属性的字段。

b
$ b

 #投诉表格

<%= form_for(@complaint)do | f | %GT;
<%如果@ complaint.errors.any? %GT;
< div id =error_explanation>
< h2><%= pluralize(@ complaint.errors.count,error)%>禁止保存此投诉:< / h2>

< ul>
<%@ complaint.errors.full_messages.each do | msg | %GT;
< li><%= msg%>< / li>
<%end%>
< / ul>
< / div>
<%end%>

< div class =field>
<%= f.label:complaint%>< br />
<%= f.text_area:complaint,:rows => 5%>
< / div>
< div class =field>
<%= f.label:date%>< br />
<%= f.datetime_select:date%>
< / div>

<%if current_user.try(:admin?)%>
< div class =field>
<%= f.label:resolved%>< br />
<%= f.check_box:已解决%>
< / div>
<%end%>

<%= fields_for:company do | company | %GT;
< div class =field>
<%= company.label:name,'公司'%>
<%= company.text_field:name%>
< / div>
<%end%>

< div class =actions>
<%= f.submit%>
< / div>
<%end%>

表单提交但仅保存投诉。公司的用户输入被忽略。为什么不能创建一个新的公司?

解决方案

我的错误在于形式。我在 fields_for:company



<$之前错过了 f。 p $ p> <%= f.fields_for:company do | company | %GT;


I have two models, Complaint and Company. Complaint belongs_to and accepts_nested_attributes for Company, and Company has_many Complaints.

# Models

class Complaint < ActiveRecord::Base
  attr_accessible :complaint, :date, :resolved

  belongs_to :user, :class_name => 'User', :foreign_key => 'id'
  belongs_to :company, :class_name => 'Company', :foreign_key => 'id'
  has_many :replies

  accepts_nested_attributes_for :company

end

class Company < ActiveRecord::Base
  attr_accessible :name

  has_many :complaints, :class_name => 'Complaint', :foreign_key => 'id'
  has_many :branches, :class_name => 'Branch', :foreign_key => 'id'
  belongs_to :industry

end

In the Complaint Controller I try build a Company in the new method.

# Complaint Controller

class ComplaintsController < ApplicationController
...
def new
    @complaint = Complaint.new
    @complaint.build_company

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @complaint }
    end
  end
...
end

In the form I have added a field for adding a name attribute to the Company.

# Complaint Form

<%= form_for(@complaint) do |f| %>
  <% if @complaint.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@complaint.errors.count, "error") %> prohibited this complaint from being saved:</h2>

      <ul>
      <% @complaint.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :complaint %><br />
    <%= f.text_area :complaint, :rows => 5 %>
  </div>
  <div class="field">
    <%= f.label :date %><br />
    <%= f.datetime_select :date %>
  </div>

  <% if current_user.try(:admin?) %>
    <div class="field">
      <%= f.label :resolved %><br />
      <%= f.check_box :resolved %>
    </div>
  <% end %>

  <%= fields_for :company do |company| %>
    <div class="field">
      <%= company.label :name, 'Company' %>
      <%= company.text_field :name %>
    </div>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

The form submits but only the Complaint is saved. The user input for Company is disregarded. Why won't this create a new Company?

解决方案

My mistake was in the form. I missed the f. before the fields_for :company

<%= f.fields_for :company do |company| %>

这篇关于用于belongs_to关联导轨的嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆