verrom
verrom

Reputation: 429

ArgumentError in StaticPages#manager

I have a problem with item creation. I have next error:

ArgumentError in StaticPages#manager

Showing /home/verevkinra/apps/yurta24/app/views/items/_new.html.erb where line #2 raised:

First argument in form cannot contain nil or be empty

Extracted source (around line #2):
<h1>Items manage</h1>
<%= form_for @item do |f| %>
   <%= f.text_field :variable1 %>
   <%= f.text_field :variable2 %>
   <%= f.text_field :variable3 %>
   <%= f.text_field :variable4 %>

Trace of template inclusion: app/views/static_pages/manager.html.erb

Rails.root: /home/verevkinra/apps/yurta24

First argument in form cannot contain nil or be empty on the second line of this code (app/view/items/new.html.erb):

<h1>Items manage</h1>
<%= form_for @item do |f| %>
   <%= f.text_field :variable1 %>
   <%= f.text_field :variable2 %>
   <%= f.text_field :variable3 %>
   <%= f.text_field :variable4 %>
   <%= f.text_field :value1 %>
   <%= f.text_field :value2 %>
   <%= f.text_field :value3 %>
   <%= f.text_field :value4 %>
   <%= f.text_field :comment %>

   <%= f.submit %>
<% end %>

My Items_controller.rb is:

class ItemsController < ApplicationController

      def new
       @item = Item.new
    end
    def create
       @item = Item.new item_params
       @item.save
    end

    def destroy
         @item = Item.find(params[:id])
         @item.destroy
      end

    private

    def item_params
       params.require(:item).permit(:variable1, :variable2, :variable3, :variable4, :value1, :value2, :value3, :value4, :comment)
    end
end

My DB migration file is:

class CreateItems < ActiveRecord::Migration
  def change
    create_table @item do |t|
      t.string :variable1
      t.string :variable2
      t.string :variable3
      t.string :variable4
      t.string :value1
      t.string :value2
      t.string :value3
      t.string :value4
      t.string :comment

      t.timestamps null: false
    end
  end
end

My routes.rb has next line:

resources :items

Thanks

My static_pages_controller.rb is:

class StaticPagesController < ApplicationController
  def home
    @contact_form = ContactForm.new 
  end

  def manager
    @contact_messages = ContactForm.all
    @item = Item.new
  end
end

Line @item = Item.new I've add right now and there is another error:

NoMethodError in StaticPages#manager

Showing /home/verevkinra/apps/yurta24/app/views/items/_new.html.erb where line #8 raised:

undefined method `value2' for #<Item:0xb3ba6504>

Extracted source (around line #8):
   <%= f.text_field :variable4 %>
   <%= f.text_field :value1 %>
   <%= f.text_field :value2 %>
   <%= f.text_field :value3 %>
   <%= f.text_field :value4 %>
   <%= f.text_field :comment %>

Trace of template inclusion: app/views/static_pages/manager.html.erb

app/views/static_pages/manager.html.erb is:

<%= render 'contact_forms/new' %>
<%= render 'items/new' %>

Upvotes: 2

Views: 117

Answers (3)

Pavan
Pavan

Reputation: 33542

First argument in form cannot contain nil or be empty

You are rendering that partial(app/views/items/_new.html.erb) in app/views/static_pages/manager.html.erb, so you should be having @item = Item.new in manager method of static_pages_controller.rb

#static_pages_controller.rb
def manager
  @item = Item.new
end

undefined method `value2' for Item:0xb3ba6504

That attribute value2 not find in items table. Create a migration with the below command

rails g migration add_value2_to_items value2:string

Change create_table @item do |t| to create_table :items do |t| in the migration file and do rake db:migrate

Upvotes: 1

Richard Peck
Richard Peck

Reputation: 76774

First argument in form cannot contain nil or be empty

Your variable @item is not populated.


I started writing this before you updated, so I'll add the fixes afterwards...

app/views/static_pages/manager.html.erb

This tells us the error is likely originating from the manager action of StaticPagesController. Specifically, you'll probably not have @item defined there...

So the first thing is to do the following:

#app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
   def manager
      @item = Item.new
   end
end

--

With your updated code, here's the new problem:

undefined method `value2' for #

This means you've not got an attribute called value2 for your Item model.

There are several solutions to this; the underlying fix is to get your attribute names sorted out properly.

t.string :variable1
t.string :variable2
t.string :variable3
t.string :variable4
t.string :value1
t.string :value2
t.string :value3
t.string :value4
t.string :comment

Since when did a model have variable1, variable2, etc?

Ruby / Rails has the unrivalled capacity to call objects, which means you'll be able to do the following: @item.name etc... and yet you call @item.variable1?

These may be what you need, but if you're adding them for the sake of it, you need to rethink the construct of your model.

That is, you need to rename your attributes to be more fitting to what data they're going to hold. You'll improve as a developer & code architect as a result.


Partial

Finally, don't call @instance_variables in partials.

Partials are meant to be called throughout your application, and should not be bound to specific data-structures. Rails can pass local variables to your partials using the following code:

<%= render 'items/new', locals: {item: @item} %>

#app/views/items/_new.html.erb
<%= form_for item do |f| %>

Upvotes: 1

EdvardM
EdvardM

Reputation: 3072

You should show StaticPages controller and especially method manager, which is missing here. Also, try to minimize the problem so that you still get the error.

Also having the stacktrace would help.

Update: at least your migration has an error. It's create_table :items, or create_table "items".

Upvotes: 0

Related Questions