Forum: Ruby on Rails Edit not working with named routes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Gareth Townsend (Guest)
on 2007-08-01 02:37
(Received via mailing list)
HELP! I'm a relatively new rails user and I'm stuck. I'm trying to
build a relatively simple blog and I decided that I liked the way
named routes look in the code. So I added

map.resources :posts

to my routes.db file. I then went through and updated my controller to
take advantage of the named routes that were generated. I also updated
all of my views and everything was working nicely. Except when I try
to edit an existing post. For some reason it just doesn't want to do
anything. Below are the relevant (I think) parts of the
PostsController:

class PostsController < ApplicationController
  layout "blog"
  before_filter :login_required, :except => [:index, :show]
  before_filter :find_post, :except => [:index, :new, :create]

  def edit
  end

  def update
    if @post.update_attributes(params[:post])
      flash[:notice] = "Post Updated"
      redirect_to post_url(@post)
    else
      render edit_post_url
    end
  end

  protected

  def find_post
    @post = Post.find(params[:id])
  end
end

Here is edit.hrtml

<h1>New Post</h1>
<% form_for(:post, @post, :url => post_url(@post), :html => {:method
=> 'put'}) do |f| %>
  <%= render :partial => "form", :object => f %>
<% end %>

And the  form partial...

<p>
    <label for='post_title'>Title</label><br />
    <%= form.text_field :title %>
</p>
<p>
    <label for='post_permalink'>Permalink</label><br />
    <%= form.text_field :permalink %>
</p>
<p>
    <label for='post_abstract'>Abstract</label><br />
    <%= form.text_area :abstract %>
</p>
<p>
    <label for='post_body'>Body</label><br />
    <%= form.text_area :body %>
</p>
<p>
    <%= submit_tag %>
</p>

Have I done something stupid? I get the feeling I probably have and
it's making the edits not work.
This topic is locked and can not be replied to.