Rails call destroy method from within another controller - ruby-on-rails-3

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 %>

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

params of the model not being returned rails 3

I have made a few posts before this regarding how to add a favourite recipe to a user..I have an app where you can upload recipes once logged in, users can search the entire table for all recipes and view their own recipes in a member area..
Now I want users to be able to save their favourite recipes, so far I can save a favourite recipe as such, the output that I get is
[#<Favourite id: 1, user_id: 8, recipe_id: nil, created_at: "2012-11-06 19:25:34", updated_at: "2012-11-06 19:25:34">,
so i am getting the correct user_id but no params for the actual recipe, ie dish name, country of origin.
My models are like so
User
class User < ActiveRecord::Base
has_many :recipes
has_many :favourites
Recipe
has_many :ingredients
has_many :preperations
has_many :favourites
Favourite
belongs_to :user
belongs_to :recipe
My favourite controller looks like so
def create
#favourite = current_user.favourites.new(params[:recipe])
if #favourite.save
redirect_to my_recipes_path, :notice => "Recipe added to Favourites"
end
end
Add to favourites link
<%= link_to "Add to favorites", {:controller => 'favourites', :action => 'create'}, {:method => :post } %>
I hope I haven’t missed anything out, any help appreciated
You need to add extra information in the link and modify the create action
# View
<%= link_to "Add to favorites", favorite_path(:recipe_id => #recipe.id), {:method => :post } %>
# Controller
def create
#favourite = current_user.favourites.new(recipe_id: params[:recipe_id)
if #favourite.save
redirect_to my_recipes_path, :notice => "Recipe added to Favourites"
end
end
The problem is you are sending nothing to the controller in the param params[:recipe]
NOTE: remember the attr_accessible :user_id, :recipe_id inside Favorite model.
as stated
<%= link_to "Add to favorites", favorite_path(:recipe_id => #recipe.id), {:method => :post } %>
BUT this all depends on what #recipe is defined as in your controller - for example, if you have
#recipes = Recipie.all
And in the view you have
#recipes.all do |recipe|
Then in your link (within the block) you need to have:
<%= link_to "Add to favorites", favorite_path(:recipe_id => recipe.id), {:method => :post } %>
Does that help?
You're not sending any parameters through in the link.
<%= link_to "Add to favorites", {:controller => 'favourites', :action => 'create'}, {:method => :post } %>
This isn't enough to add a recipe to the favourites. What you'll need to do is pass through a recipe's id along with this link:
<%= link_to "Add to favorites", {:controller => 'favourites', :action => 'create', :recipe_id => recipe.id}, {:method => :post } %>
Or you could make this much shorter by using a routing helper:
<%= link_to "Add to favorites", add_to_favorites_path(:recipe_id => recipe), {:method => :post } %>
Defining that routing helper inside your config/routes.rb like this:
post '/favorites' => "favorites#create", :as => "add_to_favorites"
Then just find the recipe with params[:recipe_id] inside the controller and do what you need to do with it.

generate an real url with rails for an e-mail

We want to send an autogenerated E-Mail with an full path to the profile. How do we this
<%=link_to('Link', :controller => 'mycontroller', :action => 'show', :id => #mycontroller.id )%>
The link_to command only builds the link in the mail like this
mycontroller/show/id
we need an link like this structure
http://www.server.tld/mycontroller/show/id
Can everyone help us please?
you can pass :only_path => false
<%= link_to('Link', :controller => 'mycontroller', :action => 'show', :id => #mycontroller.id, :only_path => false ) %>

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.

Rails3 Routing Error. :method => :delete, It will be "no route matches"

I want to call a destroy action. It is already define in controller.
The model that I want to destroy is nested resource. But route is existing in rake routes result.
new_content_model GET /contents/:content_id/model/new(.:format) {:controller=>"models", :action=>"new"}
edit_content_model GET /contents/:content_id/model/:id/edit(.:format) {:controller=>"models", :action=>"edit"}
content_model PUT /contents/:content_id/model/:id(.:format) {:controller=>"models", :action=>"update"}
DELETE /contents/:content_id/model/:id(.:format) {:controller=>"models", :action=>"destroy"}
And I call destroy action from this url
<%= link_to "destroy nested model", content_model_path( #content.id, #model.id ), :confirm => "are you sure?", :method => :delete %>
It is no route matches.
Routing Error
No route matches "/contents/1/model/1"`
Please tell me some solutions.
Already wrote <%= javascript_include_tag :all %> in layouts/application.html.erb
Rails 3 link_to (:method => :delete) not working
I solved this problem.
It was wrong that I deleted rails.js and prototype.js.
Thank you for your attending. Sorry for lack of information.