3

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!

Community
  • 1
  • 1
R. Yanchuleff
  • 323
  • 1
  • 15

1 Answers1

2

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.

Wukerplank
  • 4,156
  • 2
  • 28
  • 45
  • Ahh, thank you for clarifying that. I'm still trying to get the hang of the difference between PUT and GET as Rails uses it. In what I'm trying to do here, is GET appropriate? I'm technically affecting a change on the object with this action which I took to mean that I should use PUT, but I may have understood it wrong. Thanks again either way for your help! – R. Yanchuleff Mar 02 '11 at 14:48
  • Yes, I'd say PUT is appropriate! You are absolutely on the right track: GET to retrieve, POST to create, PUT to alter and DELETE to destroy stuff. – Wukerplank Mar 02 '11 at 14:51