ruby on rails 3 - NoMethod error in create action -


i error, when submiting form (views/users/show.html.erb) should create farms through farmscontroller , userscontroller:

nomethoderror in farms#create

showing /media/rok/local disc/users/koko/documents/kmetije/kmetije/app/views/users/show.html.erb line #42 raised:

undefined method `farms' nil:nilclass

extracted source (around line #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:increate'

here problematic create action 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 

here userscontroller, issues show action, should show results in view:

class userscontroller < applicationcontroller    def show     @user = user.find(params[:id])     @farms = @user.farms.paginate(:page => params[:page])   end  end 

this view, has form add farms in (id="add"), , should show created farms in (id="show"):

<body>      <div id="add">         <h2>dodaj kmetijo</h2><br />         <%= form_for(:farm, :url => farms_path) |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> 

there connection (farms belong_to :user) in farm model:

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 

and in user model:

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 

thank time , effort!

define @user= current_user in create method of farms controller or can use current_user instead of @user in show.html.erb.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -