no route matches action in link_to - ruby-on-rails-3

I am a little confused on how to set the route for a custom action. I have the following link in my view:
<%= link_to 'mark done', finish_task_path(task.id), :method => :post %>
In my tasks_controller I have:
def finish
#task = Task.find(params[:id])
new = {:status => "done"}
#task.update_attributes(new)
redirect_to :action => "index"
end
In my routes file I have:
match '/tasks/:id/finish', :to => 'tasks#finish'
I have also tried the following in my view:
<%= link_to 'mark done', finish_task_path(task.id), :method => :post %>
Which also has not worked. How do I set the route correctly?

You've created a route, but it is not named. Does this work?
match '/tasks/:id/finish', :to => 'tasks#finish', :as => 'finish_task'
Have a look at the output of rake routes to ensure your routes are being declared as you want.

Related

unable to update role and stripe plan rails

I'm attempting to update my stripe subscription via and edit page but it doesn't seem to like whatever i throw at it.
Here is my routes.rb
get 'subscribe' => 'subscribe#stepone'
get 'subscribe/sliding-scale' => 'subscribe#steptwo'
get 'subscribe/sliding-scale/subscriber' => 'subscribe#subscriber'
get 'subscribe/sliding-scale/supporter' => 'subscribe#supporter'
get 'subscribe/sliding-scale/sustainer' => 'subscribe#sustainer'
post 'subscribe/sliding-scale/:type' => 'subscribe#createSubscription'
get 'subscribe/edit' => 'subscribe#edit', :as => :edit_subscription
match '/subscribe/edit', to: 'subscribe#deleteCard', via: :delete
match '/subscribe/edit', to: 'subscribe#updateSubscription', via: :post
post 'subscribe/edit/changeSubscription' => 'subscribe#changeSubscription', :as => :change_subscription
My subscription controller:
def changeSubscription
customer = Stripe::Customer.retrieve(#user.stripeCustomerId)
#plan = params[:plan]
#user.update_subscription(:plan => #plan, :prorate => true)
current_role = #user.roles.first.name
#user.remove_role current_role
current_user.add_role params[#plan]
end
and lastly my edit view:
<h2>Change My Subscription</h2>
<h3>Your current plan is: <%= current_user.roles.first.name %>
<%= select_tag #plan, options_for_select([['Subscriber'], ['Sustainer'], ['Supporter']]) %>
<%= link_to "Update", change_subscription_path, :confirm => "You sure?", :plan => #plan %>
I'm attempting to send the new plan to the controller to update both the local role and the plan but it seems not to be able to find the proper route to do so. Not sure what i'm doing wrong in this regard.
Thanks for your help!
You need to perform a POST request to change_subscription_path, but you're requesting the page with a simple link_to so it's a GET request.
Add :method => :post in your link_to options:
<%= link_to "Update", change_subscription_path, :confirm => "You sure?", :method => :post, :plan => #plan %>
Alternatively, you can:
use a form instead of a link and submit it with the POST method
change the method for the changeSubscription action from POST to GET in routes.rb

Working through Rails3 routes

I have the following when I do rake routes
next_post /users/next_post/:index(.:format) {:controller=>"users", :action=>"next_post"}
then, when I have this code in my ERB file:
<%= link_to "next", :next_post %>
I get the following error, and I just can't figure it out
No route matches {:controller=>"users", :action=>"next_post"}
What am I doing wrong here? The route itself looks like this
match '/users/next_post/:index',
:controller => "users",
:action => 'next_post',
:as => :next_post
You should use this
<%= link_to "next", next_post_path(#post.id + 1) %>

no route matches even after adding route to routes.rb

This is the code in the view.
<% form_tag({:controller => 'users',
:action => 'test'}) do %>
<%= text_field_tag(:search_options, params[:search_options])%>
<%= submit_tag("Display text!")%>
<% end -%>
I have a file test.html.erb and have also added get "users/test" to routes.rb still i'm getting Error: No route matches "/users/test"
The form_tag method creates a form to be send using HTTP POST by default. You state that the route you define in your routes.rb is a GET. So you have two options to fix this problem:
Change your route to POST "users/test"
Change your form_tag call to: form_tag({:controller => 'users', :action => 'test'}, :method => :get)

How to change the controller path of a link in rails 3?

How do I get:
<%= link_to 'Back', originalcontrollers_path %>
to be:
<%= link_to 'Back', modifiedcontrollers_path %>
I already have my route updated with:
get "modifiedcontrollers", :to => "originalcontrollers#index"
So the "/modifiedcontroller" url works the same as "/originalcontroller". When creating links I need it to point to the new url. How would I do this?
I am not sure I understand you, but try this:
get "modifiedcontrollers", :to => "originalcontrollers#index", :as => :modifiedcontrollers
get "modifiedcontrollers/new", :to => "originalcontrollers#new", :as => :new_modifiedcontroller
...
so if you need all stack of routes:
resources :modifiedcontrollers, :controller => :originalcontrollers
so now this will work as expected
<%= link_to 'Back', modifiedcontrollers_path %>

Rails Routing Error

Rails is giving me a route error even though the route appears to
be in the route list.
The form is doing a Post to try to hit the update route on the Admin::ProductsController.
The edit route, index route, and show route work fine.
Using Rails 3.0.5 and ruby 1.9.2
Anyone have an idea? I can't seem to see the problem.
Error
No route matches "/admin/products/2039"
Code from ERB File that is generating the form
<%= form_for :product, #product, :url => { :action => "update" } do |f| %>
Products Controller method at this point is just a stub of
def update
puts params.inspect
end
Routes File
Analytics::Application.routes.draw do
match 'login' => 'Authentication#login', :via => [:get, :post]
namespace :admin do
# Directs /admin/products/* to Admin::ProductsController
# (app/controllers/admin/products_controller.rb)
root :to => 'AdminInterface#index', :via => :get
resources :products
resources :publishers, :only => [:edit, :update]
match 'publishers/query/:subset' => 'Publishers#index', :as => :publishers_subset, :via => [:get, :post]
end
end
According to your routes, shouldn't that be
= form_for [:admin, #product] do |f|
Your form_for can just be:
<%= form_for #product do |f| %>
If #product is an existing object then it will automatically know to go the update action of the ProductsController.