Johnny Woo
Johnny Woo

Reputation: 1036

NoMethodError when rendering a form Rails

routes.rb:

resources :shops

shop_controller.rb:

def new
    @shop=Shop.new
end

new.html.erb:

<%= form_for(@shop) do |f| %>
....
<% end %>

error: undefined method `shops_path' for:

<%= form_for(@shop) do |f| %>

The problem is that I already specify the shop resources in the routes file. Why still get such kind of error?

Any help will be appreciated, thanks

Upvotes: 0

Views: 188

Answers (3)

Serge Balyuk
Serge Balyuk

Reputation: 3462

Make sure you have these lines in your rake routes output:

   shops GET    /shops(.:format   {:action=>"index", :controller=>"shops"}
         POST   /shops(.:format)  {:action=>"create", :controller=>"shops"}

OR

   shops POST   /shops(.:format)   {:action=>"create", :controller=>"shops"}

If they aren't present, look carefully at your routes.rb for possible with_options, scope or any other scoping that can affect your resources :shops in such a way that it doesn't generate default url helpers.

Upvotes: 1

Prasad Surase
Prasad Surase

Reputation: 6574

since u havent specified the method in the form tag, i guess it is going as a GET request. Try adding the method to ur form

<%= form_for(@shop), :method => :post do |f| %>

Upvotes: 0

Mikhail Nikalyukin
Mikhail Nikalyukin

Reputation: 11967

You should use ShopsController not ShopController due to Rails naming convention.

Upvotes: 1

Related Questions