Blankman
Blankman

Reputation: 267280

Why is the form posting to the wrong url?

My form looks like:

<%= form_for [:admin, @post] do |f|%>
<div style="width:660px;">
    <%= f.text_field  :title, :size => 150 %>
    <br/>
    <%= f.text_area :body, :id => "body", :rows => 15 %>
    <br/>
    <%= f.submit %>
</div>
<% end %>

the url currently is:

http://localhost:3000/admin/posts/21/edit

my rake routes for the admin post edit is:

edit_admin_post GET    /admin/posts/:id/edit(.:format)

for some reason the edit_admin_post_path is returning:

/admin/post/the-post-title/edit

so I manually changed the post title to the id.

when I perform the update, i redirect:

if @post.update_attributes(params[:post])
      redirect_to edit_admin_post_path @post 
    end   

But again it is redirecting with the 'post-title' instead of the id.

why is this?

this is rails 3

NOTE:

For the show url, I wanted /post/my-post-title and not /post/234 so I'm not sure where I changed that b/c I see no reference for it in my code!

Upvotes: 0

Views: 192

Answers (1)

ctide
ctide

Reputation: 5257

It sounds like you have something along these lines defined in your post model:

class Post < ActiveRecord::Base

def to_param
  #{name}"
end

This will cause it to return just the name instead of the ID. Remove any to_params you have defined in your post model and see if that resolves it.

Change it to something like this:

def to_param
  "#{id}-#{name}".downcase.gsub(/\W+/, "-").gsub(/^[-]+|[-]$/,"").strip
end

This will give you fairly clean URLs, such as: http://localhost:3000/admin/posts/21-my-post-title/edit and Post.find(21-my-post-title) works the same, essentially, as Post.find(21).

Upvotes: 5

Related Questions