R. Yanchuleff
R. Yanchuleff

Reputation: 323

Adding a Route to a Rails 3 Controller

I'm still getting the hang of Rails, and I've come across a problem that I think should be pretty easy. Hoping someone can show me the error of my ways.

I read through this other post: How to add a custom RESTful route to a Rails app? which has a ton of good info (for Rails 2, I'm using Rails 3), but I can't seem to get my code to work. Here's where I'm at:

I have appended my routes.rb as follows:

  resources :proposals do
    member do
      put :change_status
    end
  end

I have appended my proposals_controller.rb as follows:

  def change_status
    @proposal = Proposal.find(params[:id])

    respond_to do |format|
      if @proposal.change_status
        format.html { redirect_to(@proposal, :notice => 'Proposal status was changed.'') }
        format.xml  { head :ok }
      else
        format.html { redirect_to(@proposal, :error => 'Proposal failed to transition.') }
        format.xml  { render :xml => @proposal.errors, :status => :unprocessable_entity }
      end
    end
  end

And finally, in my view, I'm accessing it as follows:

<%= link_to "Deliver to Client", change_status_proposal_path(@proposal) %>

But when I access my site by going to

http://localhost:3000/proposals/4/change_status

I get the following error:

Routing Error

No route matches "/proposals/4/change_status"

I assume I'm doing something stupid here, because this should be really basic, but I just can't seem to get past it. I apologize for asking such a basic question, but if anyone has any advice it would be a huge help.

Thanks in advance!

Upvotes: 3

Views: 521

Answers (1)

Wukerplank
Wukerplank

Reputation: 4176

This is because you used a put as the verb in the route. So the link would have to look like this:

<%= link_to "Deliver to Client", change_status_proposal_path(@proposal), :method=>:put %>

You won't be able to access this route by just putting the url into a browser, because the request needs to be POSTed. Putting the url into the browser counts as GET.

Upvotes: 2

Related Questions