davidg
davidg

Reputation: 168

Rails 3: update method redirection problem

Trying to build a CMS for a blog using rails 3.
In my routes.rb...

namespace :admin do  
  resources :posts  
  root :to => "home#index"  
end

In Admin::PostsController...

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

  respond_to do |format|
    if @post.update_attributes(params[:post])
      format.html { redirect_to(@post,
                  :notice => 'Post was successfully updated.')}
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @post.errors,
                    :status => :unprocessable_entity }
    end
  end
end

I had to change the first line of admin/_form.html.erb due to a previous 'undefined method' error that was driving me crazy. Was trying to point the browser to a nonexistent "post_path".

<%= form_for(@post, :url => admin_posts_path(@post)) do |f| %>

All other methods for posts are working as expected. Upon form submission (update) - the rails server...

Started POST "/admin/posts.1"
ActionController::RoutingError (No route matches "/admin/posts.1"):

First, curious as to why it is using POST instead of PUT for the update.

Second, I can't figure out why the URL is being interpreted as "/admin/posts.1" and how to fix it.

Has anyone else run into this problem? (and yes, I am following the rubyonrails.org guides closely to help me). Any help would be greatly appreciated.

EDIT:
Changed admin_posts_path(@post) to admin_post_path(@post) per theIV.

the rails server...
NoMethodError (undefined method 'post_url' for #<Admin::PostsController:0x00000102b26ff8>): app/controllers/admin/posts_controller.rb:55:in 'block (2 levels) in update' app/controllers/admin/posts_controller.rb:53:in 'update'

Upvotes: 0

Views: 2754

Answers (1)

theIV
theIV

Reputation: 25774

I believe you should be hitting admin_post_path(@post), not admin_posts_path(@post).

Look at the table that lists all of the helpers created for your routes on guides.rubyonrails.org.

EDIT: Also, have you tried the array style of urls? It's pretty convenient.

<%= form_for([:admin, @post]) do |f| %>

EDIT 2: My guess as to "undefined method post_url" is from your update action here:

format.html { redirect_to(@post,
              :notice => 'Post was successfully updated.')}

It needs to be namespaced as well:

format.html { redirect_to([:admin, @post],
              :notice => 'Post was successfully updated.')}

Upvotes: 4

Related Questions