Jimmy
Jimmy

Reputation: 9815

Rails form helper and RESTful routes

I have a form partial current setup like this to make new blog posts

<% form_for([@current_user, @post])  do |f| %>

This works great when editing a post, but when creating a new post I get the following error:

undefined method `user_posts_path' for #<ActionView::Base:0x6158104>

My routes are setup as follows:

map.resources :user do |user|
 user.resources :post
end

Is there a better way to setup my partial to handle both new posts and editing current posts?

Upvotes: 0

Views: 493

Answers (1)

zed_0xff
zed_0xff

Reputation: 33217

map.resources :user do |user|
  user.resources :posts
end

pluralize your model names in routes declaration. as you can see it says resourc-es, so you must use user-s and post-s too.

Controllers should be named UsersController and PostsController

Models should be named User and Post.

if above example still does not work for you, try a this one

map.resources :users do |u|
  u.resources :posts
end

Upvotes: 1

Related Questions