unable to update role and stripe plan rails - ruby-on-rails-3

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

Related

how to pass id to controller in rails 3?

I have used link_to to call controller action.
<%= link_to "Inactive", {:controller => :leave_details, :action => :deactivate}, {:method => :post } %>.
but my requirement is to pass id also to controller action but when i try to pass id(:id=>leave_detail.id) it is not showing in params. my params look like
{"_method"=>"post", "authenticity_token"=>"3vfyGQ5V6GQXRt2aQt+DOT0b3eGgP7B401uclnIGLUU=", "action"=>"deactivate", "controller"=>"leave_details"}.
In route file i have added
resources :leave_details do
post :deactivate, :on => :collection
end
can anyone tell me what is going wrong in this code or what i am missing.
thnks

Rails call destroy method from within another controller

I have a friendships_controller but I would like to call its create and destroy actions from inside the users_controller. Actually, the way I have things set up, the create method works fine, but destroy does not.
users/index
<%= button_to "+ Add Friend", :controller => "friendships", :action => 'create', :method => "post", :id => user.id %>
<%= button_to "- Unfriend", {:controller => "friendships", :action => 'destroy'}, :confirm => "Are you sure you want to unfriend #{user.username}?", :method => :delete, :id => user.id %>
If I click the Unfriend button I get the follow rails exception:
ActiveRecord::RecordNotFound in FriendshipsController#destroy
Couldn't find User with ID=destroy
This is the destroy action within friendships_controller:
def destroy
#accepting_user = User.find(params[:id])
#friendship = Friendship.find_by_accepting_user_id_and_requesting_user_id(#accepting_user.id, current_user.id)
#friendship.destroy
flash[:notice] = "You unfriended #{#friendship.accepting_user.username}."
redirect_to(:back)
end
Anyone have any thoughts on this? Thanks.
UPDATE
Friendship routes:
rake routes | grep friendship
friendships_index GET /friendships/index(.:format) {:controller=>"friendships", :action=>"index"}
friendships GET /friendships(.:format) {:controller=>"friendships", :action=>"index"}
POST /friendships(.:format) {:controller=>"friendships", :action=>"create"}
new_friendship GET /friendships/new(.:format) {:controller=>"friendships", :action=>"new"}
edit_friendship GET /friendships/:id/edit(.:format) {:controller=>"friendships", :action=>"edit"}
friendship GET /friendships/:id(.:format) {:controller=>"friendships", :action=>"show"}
PUT /friendships/:id(.:format) {:controller=>"friendships", :action=>"update"}
DELETE /friendships/:id(.:format) {:controller=>"friendships", :action=>"destroy"}
Got it working, just had to move where I was passing in the :id
Final result looks like:
<%= button_to "- Unfriend", {:controller => "friendships", :action => 'destroy', :id => user.id}, :confirm => "Are you sure you want to unfriend #{user.username}?", :method => :delete %>

Change password with RESTful authentication

OK, I've been having relative success with RESTful authentication. I followed this tutorial to add the capability to change the password. The problem is that tutorial is written for rails 2.3 and I'm using rails 3.
The code used in my controller and view are exactly as they are in the tutorial
I added to routes.rb:
match '/change_password' => 'users#change_password', :as => :change_password
resources :users, :controller => 'users', :collection => {:change_password_update => :put}
Now i get this error: undefined method `change_password_update_user_path'
the comments to the tutorial mentioned that exact error, and they said the solution is in the routing, but given the routing differences between 2.3 and 3, I'm really just guessing what needs to go in there. Any idea how i can get this working?
all i had to do is change
<% form_tag change_password_update_user_path(current_user), :method => :put do |f| %>
to
<% form_tag '/change_password_update', :method => :put do |f| %>
and put this in my routes.rb
match '/change_password' => 'users#change_password', :as => :change_password
match '/change_password_update' => 'users#change_password_update', :as => :change_password_update
resources :users, :controller => 'users', :collection => {:change_password_update => :put}

Rails 3 - link for delete item

I have list of images and for every image I have link for deleting. That link looks this:
<%= link_to 'delete image', {:controller => 'shops', :action => 'delimg', :imgid => u.id}, :confirm => 'Really?' %>
def logo
puts params[:imgid]
...
end
And I am getting an error Couldn't find Shop with ID=logo and app/controllers/shops_controller.rb:17:in `show' - I tried to add puts 'IN SHOW and it really looks, that after click on that link is called method show. I have no idea, how it is possible...
Could someone help me, please, where is the problem?
This is probably how I would have done it:
#routes.rb
resources :shops do
delete :delimg, :on => :member
end
By adding that, there will be a defined route to the delimg action mapped to the delete method. And that makes it possible to do the following in the view:
<%= link_to 'delete image', delimg_shop_path(u.id), :method => :delete %>
delimg_shop_path is a path helper that exists because of what was added in the routes.rb
you are displaying params[:ingid] in the logo method but in the link to action u have specified delimg??
modiefy your link to as
<%= link_to 'delete image', {:controller => 'shops', :action => 'logo', :imgid => u.id}, :confirm => 'Really?' %>
then it will work

no route matches action in link_to

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.