我有一个用于模拟房屋的rails应用程序。有一个房屋模型,有许多参数,has_many rooms
。一个房间有一个house_id
和一个名字。我还使用了http://github.com/ryanb/complex-form-examples来允许许多灯光和small_appliances
添加到房间。 complex-form-example使用RJS和partials来实现这一目标。
有一个名为calculator的控制器,用户将使用它来访问应用程序。当按下计算器上的提交按钮时,它会重定向到add_rooms
页面(位于app/views/calculator/add_rooms.html.erb
)页面,用户可以add rooms
到该房子。 add_rooms页面使用app/views/rooms/_room_form.html.erb
的部分内容。我无法显示,因为rails总是在app / views / calculator文件夹中查找内容。
我该如何显示?另请注意,我需要在保存房间时保存房屋的ID。
以下是所有相关代码(我希望):
如果我注释掉两个add_child_link
。页面呈现。但是,当我单击提交时,我收到一条新的错误消息:
ActiveRecord::AssociationTypeMismatch in CalculatorController#add_room SmallAppliance(#49096610) expected, got Array(#1560620) RAILS_ROOT: C:/Users/ryan/Downloads/react Application Trace | Framework Trace | Full Trace C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/associations/association_proxy.rb:263:in `raise_on_type_mismatch' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/associations/association_collection.rb:320:in `replace' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/associations/association_collection.rb:320:in `each' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/associations/association_collection.rb:320:in `replace' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/associations.rb:1322:in `small_appliances=' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2744:in `send' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2744:in `attributes=' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2740:in `each' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2740:in `attributes=' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2438:in `initialize' C:/Users/ryan/Downloads/react/app/controllers/calculator_controller.rb:31:in `new' C:/Users/ryan/Downloads/react/app/controllers/calculator_controller.rb:31:in `add_room'
如果我删除了small_application部分,那么光会发生同样的事情。我认为它与房间模型中的accepts_nested_attributes_for
有关。我已添加以下代码。我也添加了house.rb代码。
class Room < ActiveRecord::Base
belongs_to :house
has_many :lights, :dependent => :destroy
has_many :small_appliances, :dependent => :destroy
validates_presence_of :name
accepts_nested_attributes_for :lights, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true
accepts_nested_attributes_for :small_appliances, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true
end
class House < ActiveRecord::Base
has_many :rooms
# validation code not included
def add_room(room)
rooms << room
end
end
class CalculatorController < ApplicationController
def index
end
def save_house
@house = House.new(params[:house])
respond_to do |format|
if @house.save
format.html { render :action => 'add_rooms', :id => @house }
format.xml { render :xml => @house, :status => :created, :location => @house }
else
format.html { render :action => 'index' }
format.xml { render :xml => @house.errors, :status => :unprocessable_entity }
end
end
end
def add_rooms
@house = House.find(params[:id])
@rooms = Room.find_by_house_id(@house.id)
rescue ActiveRecord::RecordNotFound
logger.error("Attempt to access invalid house #{params[:id]}")
flash[:notice] = "You must create a house before adding rooms"
redirect_to :action => 'index'
end
def add_room
@house = House.find(params[:id])
@room = Room.new(params[:room])
respond_to do |format|
if @room.save
@house.add_room(@room)
@house.save
flash[:notice] = "Room \"#...@room.name}\" was successfully added."
format.html { render :action => 'add_rooms' }
format.xml { render :xml => @room, :status => :created, :location => @room }
else
format.html { render :action => 'add_rooms' }
format.xml { render :xml => @room.errors, :status => :unprocessable_entity }
end
end
rescue ActiveRecord::RecordNotFound
logger.error("Attempt to access invalid house #{params[:id]}")
flash[:notice] = "You must create a house before adding a room"
redirect_to :action => 'index'
end
def report
flash[:notice] = nil
@house = House.find(params[:id])
@rooms = Room.find_by_house_id(@house.id)
rescue ActiveRecord::RecordNotFound
logger.error("Attempt to access invalid house #{params[:id]}")
flash[:notice] = "You must create a house before generating a report"
redirect_to :action => 'index'
end
end
<div id="addRooms">
<p>House id is <%= @house.id %></p>
<h3>Your rooms:</h3>
<% if @house.rooms %>
<ul>
<% for room in @house.rooms %>
<li>
<%= h room.name %> has <%= h room.number_of_bulbs %>
<%= h room.wattage_of_bulbs %> watt bulbs, in use for
<%= h room.usage_hours %> hours per day.
</li>
<% end %>
</ul>
<% else %>
<p>You have not added any rooms yet</p>
<% end %>
<%= render :partial => 'rooms/room_form' %>
<br />
<%= button_to "Continue to report", :action => "report", :id => @house %>
</div>
_room_
form.html.erb <% form_for :room, :url => { :action => :add_room, :id => @house } do |form| %>
<%= form.error_messages %>
<p>
<%= form.label :name %><br />
<%= form.text_field :name %>
</p>
<h3>Lights</h3>
<% form.fields_for :lights do |light_form| %>
<%= render :partial => 'rooms/light', :locals => { :form => light_form } %>
<% end %>
<p class="addLink"><%= add_child_link "[+] Add new light", form, :lights %></p>
<h3>Small Appliances</h3>
<% form.fields_for :small_appliances do |sm_appl_form| %>
<%= render :partial => 'rooms/small_appliance', :locals => { :form => sm_appl_form } %>
<% end %>
<p class="addLink"><%= add_child_link "[+] Add new small appliance", form, :small_appliances %></p>
<p><%= form.submit "Submit" %></p>
<% end %>
module ApplicationHelper
def remove_child_link(name, form)
form.hidden_field(:_delete) + link_to_function(name, "remove_fields(this)")
end
def add_child_link(name, form, method)
fields = new_child_fields(form, method)
link_to_function(name, h("insert_fields(this, \"#{method}\", \"#{escape_javascript(fields)}\")"))
end
def new_child_fields(form_builder, method, options = {})
options[:object] ||= form_builder.object.class.reflect_on_association(method).klass.new
options[:partial] ||= method.to_s.singularize
options[:form_builder_local] ||= :form
form_builder.fields_for(method, options[:object], :child_index => "new_#{method}") do |form|
render(:partial => options[:partial], :locals => { options[:form_builder_local] => form })
end
end
end
谢谢,
瑞恩
答案 0 :(得分:3)
非常奇怪 - 如果你写<%= render :partial => 'room_form' %>
而不是rails会假设它是app/views/calculator/_room_form.html.erb
,但是如果是&lt;%= render:partial =&gt; 'rooms / room_form'%&gt;它会假设它是app/views/rooms/_room_form.html.erb
观察您的日志 - 您将看到哪些部分已呈现
答案 1 :(得分:1)
我知道这个问题已经过时但我遇到了同样的问题,并设法解决了这个问题。
具体而言,我也是关注ryan bates复杂形式的例子。
我的表单没有呈现相同的错误:
undefined method `reflect_on_association' for NilClass:Class
我注释掉了add_child_link帮助器和呈现的表单,但在提交后我得到了:
ChildModel expected, got Array
经过多次头疼后,我做了一个简单的修改,修复了一切:
-- form_for :project
++ form_for @project
没错,只需切换实例变量的符号就可以了。这就行了。
希望这有助于其他任何陷入困境的人
答案 2 :(得分:0)
问题是,当您尝试添加子链接时,没有定义room
对象。
render :partial => 'rooms/room_form', :object => Room.new