jeudi 23 avril 2015

NoMethod error in create action

I get this error, when submiting a form (views/users/show.html.erb) that should create farms through the FarmsController and UsersController:

NoMethodError in Farms#create

Showing /media/rok/LOCAL DISC/Users/Koko/Documents/KMETIJE/kmetije/app/views/users/show.html.erb where line #42 raised:

undefined method `farms' for 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 is the 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 is the UsersController, which issues the show action, that should show the results in the view:

class UsersController < ApplicationController

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

end

This is the view, that has the form to add farms in (id="add"), and should also show the created farms in the (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>

There is also a connection (farms belong_to :user) in the 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 also in the 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 you for your time and effort!

Aucun commentaire:

Enregistrer un commentaire