Reputation: 6311
So this is the scenario:
In the show view for my Partner model I need a link_to a delete method for another model, my RecommendPartner model.
But I need to return to my Partner show after the destroy method finishes.
To the code!!
My Partner show view (if this is terribly ugly I accept suggestions):
<div>
<%= link_to 'Destroy!', @partner.recommended?(current_user), confirm: 'Are your sure?', method: 'delete', :controller => "recommend_partners", :action => "destroy", :p_id => @partner.id %>
</div>
As you see I am trying to send a :p_id to the params, but with no success.
Here's my recommended? method in case you need to know, it just returns a RecommendPartner object. In my Partner.rb:
def recommended? (user)
RecommendPartner.find_by_user_id_and_partner_id(user.id,self.id)
end
Yes, RecommendPartner belongs to user and partner models.
Finally here's my recommend_partners_controler destroy method:
def destroy
@recommend_partner = RecommendPartner.find(params[:id])
partner = Partner.find(params[:p_id])
@recommend_partner.destroy
respond_to do |format|
format.html { redirect_to partner }
format.json { head :no_content }
end
end
So params[:p_id] is nil and I can't redirect back to the partner show that I was... =/
Any suggestions?
Thanks in advance.
Upvotes: 0
Views: 5160
Reputation: 6311
So thanks to @hakunin's comment this is the best solution
Change the controller so that I won't need the p_id param:
def destroy
@recommend_partner = RecommendPartner.find(params[:id])
@partner = @recommend_partner.partner
@recommend_partner.destroy
respond_to do |format|
format.html { redirect_to @partner }
format.json { head :no_content }
end
end
And then modify the link_to method to look like this:
<%= link_to 'Destroy!', @partner.recommended?(current_user), confirm: 'Are you sure?', method: :delete %>
Works Great! Cheers!
Upvotes: 2
Reputation: 1566
Can you try this?:
<%= button_to 'Destroy!', partner_path(@partner.recommended?(current_user), 'p_id' => @partner.id), :method => 'delete', :confirm => 'Are your sure?' %>
Upvotes: 1