rails 3 undefined method `model_name' for ActiveRecord::Relation:Class.
by
at 2011-06-09 10:19:47
original http://hlee.iteye.com/blog/1073937
rails 3 form_for 的问题
undefined method `model_name' for ActiveRecord::Relation:Class.
def edit @office = Office.where("id = ? AND company_id = ?", params[:id], @company.id ) end
<%= simple_form_for @office, :url => settings_office_path, :html => { :class => "office_form" } do |f| %> <h1>Edit <%= @office.office_name %> Details</h1> <%= render :partial => 'form', :locals => { :f => f } %> <% end %>
需要修改
@office = Office.where(:company_id => @company.id).find(params[:id])@office = @company.offices.find(params[:id])
下面有个人和我类似
class Event < ActiveRecord::Base belongs_to :userattr_accessible :event_name, :Starts_at, :finish, :tracks end
class User < ActiveRecord::Base has_many :events, :dependent => :destroyattr_accessible :name, :event_attributes
accepts_nested_attributes_for :events, :allow_destroy => true
end
ActiveRecord::Schema.define(:version => 20101201180355) docreate_table "events", :force => true do |t|
t.string "event_name" t.string "tracks" t.datetime "starts_at" t.datetime "finish" t.datetime "created_at" t.datetime "updated_at" t.integer "user_id"
end end
引用
NoMethodError in Users#index
undefined method
events' for #<ActiveRecord::Relation:0x4177518>
<br>
<br>Extracted source (around line #10):
<br>
<br>7: <th><%= sortable "Tracks" %></th>
<br>
<br>8: </tr>
<br>
<br>
<br>10: <% @users.events.each do |event| %>
<br>
<br>11: <% debugger %>
<br>
<br>12: <tr>
<br>
<br>13: <td><%= event.starts_at %></td>
<br>
<br>Trace of template inclusion: app/views/users/index.html.erb
<br>
<br>Rails.root: C:/rails_project1/events_manager
<br>Application Trace | Framework Trace | Full Trace
<br>
<br>app/views/users/_event_user.html.erb:10:in
app_views_usersevent_user_html_erb412443848_34308540_1390678'
app/views/users/index.html.erb:7:in `app_views_users_index_html_erb___603337143_34316016_0'
问题解决:
@users = User.find(<conditions go here>)要变成
@users = User.where(<conditions go here>).all
或者如下
@users = User.where(:admin => true).where('created_at > ?', min_date).order('created_at').limit(10).all
<br><br>
<span style="color:red">
<a href="http://hlee.iteye.com/blog/1073937#comments" style="color:red">已有 <strong>0</strong> 人发表留言,猛击->><strong>这里</strong><<-参与讨论</a>
</span>
<br><br><br>
ITeye推荐