Reputation: 2866
The custom routing:
resources :blog, controller: 'posts'
How do I rewrite this line <%= simple_form_for(@post, blog_path) do |f| %>
to get rid of the below error?
TypeError in Posts#edit
ActionView::Template::Error (no implicit conversion of Symbol into Integer)
I also tried <%= simple_form_for(blog_path(@post)) do |f| %>
, which gets rid of the error, but then if I want to edit the form the inputs are emptied of their saved data.
posts_controller
def new
@post = Post.new
respond_with(@post)
end
def edit
end
def create
@post = Post.new(post_params)
if current_user.admin
@post.save
respond_with(@post)
else
flash[:success] = 'Get out of here.'
redirect_to root_url
end
end
Upvotes: 2
Views: 242
Reputation: 882
It can take a hash options, including url, so something like this:
Edit: changed blog_path
to blogs_path
. The blog_path
is the show action, not the create action and therefore requires an id (and isn't a post path anyway). Try it out this way.
<%= simple_form_for(@post, url: blogs_path) do |f| %>
Upvotes: 4
Reputation: 76774
Don't know if this applies, but a really cool feature I found the other day was .becomes
- where you can change the "class" of your object so that Rails treats it in a different way:
This can be used along with record identification in Action Pack to allow, say, Client < Company to do something like render partial:
@client.becomes(Company)
to render that instance using the companies/company partial instead of clients/client.
So...
If you had a Blog
model, and wanted each @post
to be treated as such (again, I don't know if this is your setup at all), you could do the following:
<%= simple_form_for @post.becomes(Blog) do |f| %>
I'll delete if inappropriate; it's come in handy quite a lot for me.
Update
If you'd like your posts_path
to be blog (IE url.com/blog/1
), you'll want to look at using the path
option for the routes generator:
#config/routes.rb
resources :posts, path: "blog", as: :blog # -> url.com/blogs/2
Upvotes: 0