how to pass id to controller in rails 3? - ruby-on-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

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

url_for adding controller and action to querystring in rails 3.2

I am trying to generate a url in an actionmailer template. An example if the url I want to generate is
http://0.0.0.0:3000/users/confirm/lNbQxzFukYtEEw2RMCA
Where the last segment is a hash to identify the user
However when I use this
<%= url_for(:controller => 'users', :action => 'confirm', :id => #user.confirmhash, :only_path => false) %>
It generates this
http://0.0.0.0:3000/assets?action=confirm&controller=users&id=ZOR3dNMls8533T8hJUfCJw
How can I get it to correctly format? I have no idea where 'assets' is coming from.
Is there an easier way to use named routes that I am missing?
I've found the answer. As I'm still learning I've missed the option to create a named route. So this this the path I've taken.
In config/routes.rb
match 'user/confirm/:id' => 'users#confirm', :as => :confirm_account
Then in my action mailer template I've used
<%= link_to "Confirm your account", confirm_account_url(#user.confirmhash) %>
Which passes the :id into the controller action.

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)

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

Rendering the Devise edit Password Form

I'm trying to render the Devise edit password form within another view because I don't want to duplicate the edit pw logic.
I've tried the following (after generating the Devise views):
<%= render 'devise/passwords/edit' %>
<%= render 'devise/passwords/form' %>
And a number of other variations on render that all seem to give me the same error:
"ActionView::MissingTemplate in foo#foo
Missing partial devise/passwords/edit..."
This variation:
<%= render :file => 'devise/passwords/edit.html.erb' %>
Gave me some hope but the following error:
"undefined local variable or method `resource' for #<#:0x47ef0e0>"
around this line:
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
That makes me think I'm close (as that is code from the form that I want) but shouldn't that template be using the correct logic from the hidden Devise controller? Or do I need to do something in the routes file to get this to work?
Am I way off?
Try this:
<%= render :template => 'devise/passwords/edit',
:locals => {
:resource => my_user_model_variable,
:resource_name => my_user_model_name } %>
Where:
my_user_model_variable could be current_user
my_user_model_name could be "User"