Topic: Pass the instance variable from view to controller

Is there any way to pass the instance variable from link_to command in view to a controller.

I tried

# code in view 

for am in @am
...

...
end
<%= link_to 'Download Results', :controller => 'reporting', :action => 'download_results', :params=> {:app_name => <%= @am %>} %>


it did not work.

Re: Pass the instance variable from view to controller

Don't wrap it in a params hash:

<%= link_to 'Download Results', :controller => 'reporting', :action => 'download_results', :app_name => @am %>

You can then access it in the download_results action like this:

params[:app_name]

Railscasts - Free Ruby on Rails Screencasts

Re: Pass the instance variable from view to controller

Hi Ryan,
I tried that but @am object now acts as string and I cannot access any methods on it.

Just to give a little background

@am is passed into view from the controller

@am = Am.find :all, :conditions => ['name like ?', params[:dev_name]+ '%']

In view I display the list and on the same page I have the option to download those results. I wanted to use the same @am so I can parse it and allow the user to download the results.

When I do as you suggested and the download_results gives me

 NoMethodError in ReportingController#download_results

undefined method `business_application_name' for "10250":String

RAILS_ROOT: C:/InstantRail/rails_apps/portfolio/config/..
Application Trace | Framework Trace | Full Trace

#{RAILS_ROOT}/app/controllers/reporting_controller.rb:81:in `download_results'
#{RAILS_ROOT}/app/controllers/reporting_controller.rb:80:in `each'
#{RAILS_ROOT}/app/controllers/reporting_controller.rb:80:in `download_results'
-e:3:in `load'
-e:3

C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `send'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `perform_action_without_filters'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
C:/InstantRail/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:in `perform_action'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in `send'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in `process_without_filters'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:in `process'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in `dispatch'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/rails.rb:73:in `process'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:551:in `process_client'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:in `each'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:in `process_client'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in `initialize'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in `new'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in `initialize'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in `new'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:956:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:in `each'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:127:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/command.rb:199:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:235
C:\InstantRail\ruby\bin\mongrel_rails:18:in `load'
C:\InstantRail\ruby\bin\mongrel_rails:18

#{RAILS_ROOT}/app/controllers/reporting_controller.rb:81:in `download_results'
#{RAILS_ROOT}/app/controllers/reporting_controller.rb:80:in `each'
#{RAILS_ROOT}/app/controllers/reporting_controller.rb:80:in `download_results'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `send'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in `perform_action_without_filters'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:in `perform_action_without_benchmark'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
C:/InstantRail/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in `perform_action_without_rescue'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:in `perform_action'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in `send'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in `process_without_filters'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:in `process_without_session_management_support'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:in `process'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in `dispatch'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/rails.rb:73:in `process'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:551:in `process_client'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:in `each'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:550:in `process_client'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in `initialize'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in `new'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:636:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in `initialize'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in `new'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:625:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:956:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:in `each'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel.rb:955:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:127:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/lib/mongrel/command.rb:199:in `run'
C:/InstantRail/ruby/lib/ruby/gems/1.8/gems/mongrel-0.3.13.3-mswin32/bin/mongrel_rails:235
C:\InstantRail\ruby\bin\mongrel_rails:18:in `load'
C:\InstantRail\ruby\bin\mongrel_rails:18
-e:3:in `load'
-e:3

Request

Parameters: {"app_name"=>["10250", "10084", "10132"]}

Show session dump

---
flash: !map:ActionController::Flash::FlashHash {}


Response
Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}


If the download_results method in controller helps you... it looks like this

 def download_results
     headers['Content-disposition'] = 'attachment;filename=list.csv'
      @columns = []
      [0,1,2,5].each do |c|
            @columns << Am.content_columns[c].human_name + ','
       end   
    @am = params[:app_name]
     for am_application in @am
      @columns << am_application.business_application_name + ','
      @columns << am_application.maintained_by + '\n'
     end
    end

Re: Pass the instance variable from view to controller

Oh, I had assumed @am was a string. Unfortunately you can't pass entire models through the URL. You can store them in session[] but I usually use that as a last resort. Since it looks like you are fetching the @am models through params[:dev_name], you can just pass this and fetch the models in the same way:

<%= link_to 'Download Results', :controller => 'reporting', :action => 'download_results', :dev_name => params[:dev_name] %>

You will then need to set the @am variable the same way as you did in the show action. You can do this in a before filter or something to remove the duplication.

Railscasts - Free Ruby on Rails Screencasts