2015-04-23 79 views
0

我得到这个错误,submiting形式(视图/用户/ show.html.erb)应该通过FarmsController和UsersController创建农场时:NoMethod错误创建行动

NoMethodError在农场#创建

未定义的方法`农场:

显示/媒体/其中行#42韩国升高/ LOCAL DISC /用户/科科/文档/ kMETIJE/kmetije /应用/视图/用户/ show.html.erb '为零: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> 

应用/视图/用户/ show.html.erb:42:在_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 

这里是UsersController,它issu ES展会的行动,应显示结果的看法:

class UsersController < ApplicationController 

    def show 
    @user = User.find(params[:id]) 
    @farms = @user.farms.paginate(:page => params[:page]) 
    end 

end 

这是视图,具有形式中添加农场(ID =“添加”),并应显示在创建农场在(ID = “秀”):

<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> 

还有一个连接(场belong_to:用户)在农场模型:

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

In Farms#create''users/show''需要参数中的用户ID才能找到'User.find(params [:id]':id,在线42 @user in'nil' –

回答

0

define @ user = current_user创建农场控制器的方法,或者您可以在show.html.erb中使用current_user而不是@user。

+0

谢谢,似乎是对的,但我仍然得到相同的错误 –