Wahtever
Wahtever

Reputation: 3678

rails Routing Error No route matches [POST] "/controller/new"

I am new to rails and i am trying to create a simple bookmark table:

  def up
create_table :bookmarks do |t|
    t.string  :path,        :null => false
    t.integer :user_id,     :null => false

    t.timestamps
end

add_index :bookmarks, :user_id
add_index :bookmarks, :path
  end

then i have this in my contoller:

    # GET bookmark/new
# GET bookmark/new.json
def new
    @bookmark = Bookmark.new
end

# POST bookmark/new
# POST bookmark/new.json
def create
    @bookmark = Bookmark.new(params[:bookmark])
    if @bookmark.save
        flash[:success] = "Bookmark Saved"
    else
        flash[:failure] = "Failed !"
    end
end

and my view is this:

<%= form_for :bookmark do |bookmark| %>
<%= bookmark.label :path %>
<%= bookmark.text_field :path %>

<%= bookmark.label :user_id %>
<%= bookmark.text_field :user_id %>

<%= bookmark.submit "Add bookmark" %>
<% end %>

finally running rake routes gets this list of routes:

          bookmark_index GET    /bookmark(.:format)            bookmark#index
                     POST   /bookmark(.:format)            bookmark#create
        new_bookmark GET    /bookmark/new(.:format)        bookmark#new
       edit_bookmark GET    /bookmark/:id/edit(.:format)   bookmark#edit
            bookmark GET    /bookmark/:id(.:format)        bookmark#show
                     PUT    /bookmark/:id(.:format)        bookmark#update
                     DELETE /bookmark/:id(.:format)        bookmark#destroy

and when i try to submit the form i get this error:

Routing Error

No route matches [POST] "/bookmark/new"

Try running rake routes for more information on available routes. 

Edit:

Changing :bookmark to @bookmark throws this error:

    NoMethodError in Bookmark#new

Showing /media/wahtver/600415AD27D78282/3pces/pces/app/views/shared/_bookmark_form.html.erb where line #1 raised:

undefined method `bookmarks_path' for #<#<Class:0x00000003a48398>:0x007f1034b6b908>

Extracted source (around line #1):

1: <%= form_for @bookmark do |bookmark| %>
2:  <%= bookmark.label :path %>
3:  <%= bookmark.text_field :path %>
4: 

what is the problem?

Thanks

Upvotes: 0

Views: 1133

Answers (2)

Alfonso
Alfonso

Reputation: 759

How did you declare your routes? Did you have on your route.rb file resource :bookmark or resources :bookmarks?

If you see your controller in your create method you have this:

# POST bookmark/new
# POST bookmark/new.json
def create

Pay attention to the comments that rails generate automatically above each method when you use scaffold (i guess you generated the controller with scaffold), it should be POST /bookmarks or POST /bookmark. If you used scaffold for some reason rails generated bad that route.

Show your routes.rb file.

Upvotes: 0

Robin
Robin

Reputation: 21884

<%= form_for @bookmark do |bookmark| %>

and not

<%= form_for :bookmark do |bookmark| %>

Look more closely at the error message. It's using POST, not GET.

edit: Your routes should be resources :bookmarks.

When you run rake routes it should give you:

bookmarks GET /bookmarks(.:format) bookmarks#index

Upvotes: 1

Related Questions