我在提交应该通过FarmsController和UsersController创建服务器场的表单(views / users / show.html.erb)时出现此错误:
农场中的NoMethodError #create
显示/ media / rok / LOCAL DISC / Users / Koko / Documents / KMETIJE / kmetije / app / views / users / show.html.erb第42行引出:
未定义的方法`farm'代表nil:NilClass
提取的来源(第42行):
39:
40: <div id="show">
41: <h2>Seznam kmetij</h2>
42: <% unless @user.farms.empty? %>
43: <table id="farms">
44: <tr>
45: <th>Ime</th>
app / views / users / show.html.erb:42:in _app_views_users_show_html_erb___3046909201139407898_28545460_3894795499615530291'
app/controllers/farms_controller.rb:14:in
创建'
以下是FarmsController中有问题的创建操作:
class FarmsController < ApplicationController
def new
@farm = current_user.farms.build
end
def create
@farm = Farm.new(params[:farm])
if @farm.save
redirect_to 'users/show'
else
render 'users/show'
end
end
end
以下是发布show动作的UsersController,它应该在视图中显示结果:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@farms = @user.farms.paginate(:page => params[:page])
end
end
这是视图,它具有在(id =“add”)中添加服务器场的形式,还应该在(id =“show”)中显示创建的服务器场:
<body>
<div id="add">
<h2>Dodaj kmetijo</h2><br />
<%= form_for(:farm, :url => farms_path) do |f| %>
<%= f.label :name, "Ime kmetije" %><br />
<%= f.text_field :name %>
<%= f.label :region, "Območje" %><br />
<%= f.text_field :region %>
<%= f.label :north, "Geografska širina" %><br />
<%= f.text_field :north %>
<%= f.label :east, "Geografska dolžina" %><br />
<%= f.text_field :east %>
<%= f.label :description, "Opis" %><br />
<%= f.text_area :description, rows: "6" %><br />
<%= f.label :categories, "Kategorije" %><br />
<%= f.text_area :categories, rows: "1" %><br />
<%= f.label :products, "Izdelki" %><br />
<%= f.text_area :products, rows: "2" %><br />
<%= f.submit "Vstavi" %>
<% end %>
</div>
<div id="show">
<h2>Seznam kmetij</h2>
<% unless @user.farms.empty? %>
<table id="farms">
<tr>
<th>Ime</th>
<th>Regija</th>
<th>N</th>
<th>E</th>
<th>Opis</th>
<th>Kategorije</th>
<th>Izdelki</th>
<th>Izbris</th>
</tr>
<tr>
<td><%= farm.name %></td>
<td><%= farm.region %></td>
<td><%= farm.north %></td>
<td><%= farm.east %></td>
<td><%= farm.description %></td>
<td><%= farm.categories %></td>
<td><%= farm.products %></td>
<td><%= link_to "delete", farm, :method => :delete, :confirm => "Izbrišem?", :title => farm.name %></td>
</tr>
</table>
<%= will_paginate @farms %>
<% end %>
</div>
</body>
Farm模型中还有一个连接(farm belongs_to:user):
class Farm < ActiveRecord::Base
attr_accessible :name, :region, :north, :east, :description, :categories, :products
belongs_to :user
validates :name, :presence => true
validates :region, :presence => true
validates :north, :presence => true
validates :east, :presence => true
validates :description, :presence => true
validates :categories, :presence => true
validates :products, :presence => true
validates :user_id, :presence => true
default_scope :order => 'farms.region DESC'
end
还有用户模型:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :password, :password_confirmation
has_many :farms
validates :name, :presence => true,
validates :password, :presence => true,
:confirmation => true,
end
感谢您的时间和精力!
答案 0 :(得分:0)
在farm控制器的create方法中定义@ user = current_user,或者在show.html.erb中使用current_user而不是@user。