Iggy
Iggy

Reputation: 5251

Cannot create new object in rails

I am trying to create a basic Item creation in rails but I am having trouble creating new item. I want to create item's name, say Wash the dishes. These are the codes that I have:

Routes:

resources :items

ItemsController:

class ItemsController < ApplicationController
...
  def new
    @item = Item.new
  end

  def create
    @item = Item.new(item_params)

    if @item.save
      flash[:notice] = "Item was saved!"
      redirect_to @item
    else
      flash.now[:alert] = "ERROR. ERROR."
      render :new
    end
  end
...
  private

  def item_params
    params.require(:item).permit(:name, :list_id)
  end

end

items/new.html.erb

<%= form_for :item do |f| %>

    <%= f.text_field :name %>

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

Lastly, schema:

  create_table "items", force: :cascade do |t|
    t.string   "name"
    t.integer  "list_id"
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
  ...

I got several different error codes, but this is the one I am currently stuck at (other times it showed different error code or it would simply prints out "ERROR. ERROR." (alert that I setup when save fails)

Routing Error
No route matches [POST] "/items/new"

When I go to rake routes:

         POST  /items(.:format)                           items#create
new_item GET   /items/new(.:format)                      items#new

I followed suggestion from this SO post to check my routes and this is what I have:

2.2.2 :019 > r = Rails.application.routes
 => #<ActionDispatch::Routing::RouteSet:0x007fff323c3230> 
2.2.2 :020 > r.recognize_path "/items/new"
 => {:controller=>"items", :action=>"new"} 

I have also gone to rails c and I was able to create new item manually. (i = Item.new(name:"Test 123"); i.save)

What did I miss?

Upvotes: 0

Views: 528

Answers (2)

Arun Kumar Mohan
Arun Kumar Mohan

Reputation: 11905

The problem is with your form. To understand what's wrong, do the following:

  1. Start the rails server using rails s
  2. Go to http://localhost:3000/items/new
  3. Instead of filling in the form fields, view the source page
  4. Check the form tag. Its submitting the form data to /items/new. ie.the action attribute is set to /items/new. Why is that?

From the documentation:

When the model is represented by a string or symbol, if the :url option is not specified, by default the form will be sent back to the current url (We will describe below an alternative resource-oriented usage of form_for in which the URL does not need to be specified explicitly).

<form action="/items/new" accept-charset="UTF-8" method="post">

In your routes.rb, there's no route matching POST /items/new

So, modify your form to

<%= form_for :item, url: items_path do |f| %>
  <%= f.text_field :name %>
 <%= f.submit %>
<% end %>

This generates a form tag which posts the data to /itemsrather than /items/new.

Or replace your form with

<%= form_for @item do |f| %>
 <%= f.text_field :name %>
 <%= f.submit %>
<% end %>

Now, the form will be submitted to /items. The advantage of using the second version is you can dry out your form for creating a new object and updating an existing object into a single view(partial).

For more, see http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object

Upvotes: 1

Hasmukh Rathod
Hasmukh Rathod

Reputation: 1118

Try this in items/new.html.erb

<%= form_for @item do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

Upvotes: 0

Related Questions