0

I am trying to send the current params @calc to a generate_csv action.

How can I pass the params, so I can access them within the generate_csv action ?

my view

- form_tag generate_csv_path(@calc) do
  = submit_tag 'Print CSV'

my routes:

match '/generate_csv', :to => "main#generate_csv", :via => :post
resources :main

Which creates:

generate_csv POST   /generate_csv(.:format)  {:controller=>"main", :action=>"generate_csv"}

my controller

def generate_csv
  ..

An Alternative:

my controller

def generate_csv(foobar)

my view

- form_tag generate_csv_path(@calc) do
  = submit_tag 'Print CSV'

the error

wrong number of arguments (0 for 1)

How can that be if I'm obviously passing it a param? It seems its not accepting it.

Trip
  • 26,756
  • 46
  • 158
  • 277
  • did you try to pass it as a hidden field? – apneadiving Jan 29 '11 at 21:28
  • There's no use.. even if it was, it still wouldn't pass because no params pass when its clicked.. – Trip Jan 29 '11 at 21:35
  • Great question. Its just a form_tag .. I might be semantically off. My scribbles above are literally the whole of most of the code in the form.. – Trip Jan 29 '11 at 21:47

3 Answers3

2

Use link_to:

<%= link_to 'Print Csv', generate_csv_path(:id => @calc) %>

But change you route, if your param is mandatory:

match '/generate_csv/:id', :to => "main#generate_csv"

Else:

match '/generate_csv/(:id)', :to => "main#generate_csv"
apneadiving
  • 114,565
  • 26
  • 219
  • 213
0

I think params[:calc] variable should help you within controller. Also, this question may help you. Within your view just use normal form tags.

Community
  • 1
  • 1
shybovycha
  • 11,556
  • 6
  • 52
  • 82
  • Nope, there's nothing in params. Its stark empty, except for the defaults. – Trip Jan 29 '11 at 21:35
  • well, are you sure you have soomething like `` inside your form? I'm sorry, i forgot to mention this: `params[whatever]` will give you access to the `whatever` variable, passed to your controller. So, you should define it before use, with GET (URL) or POST (form) methods. – shybovycha Jan 29 '11 at 21:44
  • yah the params are empty.. and the form is a default submit_tag where I am trying to send the instance_variable `@calc` which is available in the view the form is available in, and hoping to pass it to the `generate_csv` action.. – Trip Jan 29 '11 at 21:46
  • wait... you just said.... you are trying to send `@calc` to **form** or to **action**??? – shybovycha Jan 29 '11 at 21:51
  • Sorry for being unclear. I would like to take the instance variable @calc which is in the view that the form is in. And pass it through the form ( which is just a simple submit tag in a form tag ), to a controller action . – Trip Jan 29 '11 at 21:59
0

generate_csv_path(:calc => @calc) should generate link /generate_csv?calc=VALUE

In controller you can access this value via params hash params[:calc].

p.s: Recently I had a problem with params hash (ruby 1.9.2, rails 3.0.3) which didn't receive query value from URL. It had only controller and action values. Switching from mongrel 1.2.rc to webrick did the trick.

Voldy
  • 12,829
  • 8
  • 51
  • 67