Passing parameter from a dropdown.

63 views
Skip to first unread message

Avi

unread,
Dec 19, 2012, 11:53:51 PM12/19/12
to rubyonra...@googlegroups.com
Hello,

I have a dropdown in the UI. There are many oprions.
How do I send the parameter from the dropdown - the selected value to the controller?

I have tried :-

1. <%= select_tag 'name', options_for_select(@list, :selected) %>
2. <%= select("audit", "value",options_for_select([["User Registration", "USER_REGISTRATION"], ["Download", "USER_REPORT"]], :selected => params[:value]) %>

I am not getting any parameters passed to the controller.
     

Jim Ruther Nill

unread,
Dec 20, 2012, 12:41:29 AM12/20/12
to rubyonra...@googlegroups.com
make sure that the select tag above is part of the form you are submitting and that you
are actually submitting to the right action.  A quick look on your code raised no issues.
 
     

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/fk7PY0E_DrMJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
-------------------------------------------------------------
visit my blog at http://jimlabs.heroku.com

Avi

unread,
Dec 20, 2012, 12:45:14 AM12/20/12
to rubyonra...@googlegroups.com
I am not adding any form because there is no model for this page. 
Only there is controller and view.

Jim Ruther Nill

unread,
Dec 20, 2012, 1:20:37 AM12/20/12
to rubyonra...@googlegroups.com
On Thu, Dec 20, 2012 at 1:45 PM, Avi <aavinas...@gmail.com> wrote:
I am not adding any form because there is no model for this page. 
Only there is controller and view.

you need to add a form to pass the selected values to the controller.  It doesn't
matter whether you're accessing a model or not.
 
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ezhDSMxW09AJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Avi

unread,
Dec 21, 2012, 1:46:58 AM12/21/12
to rubyonra...@googlegroups.com
Sorry for late reply...

Can't we pass the param through ajax or js ?

Jim Ruther Nill

unread,
Dec 21, 2012, 1:49:19 AM12/21/12
to rubyonra...@googlegroups.com
On Fri, Dec 21, 2012 at 2:46 PM, Avi <aavinas...@gmail.com> wrote:
Sorry for late reply...

Can't we pass the param through ajax or js ?

yes you can. you have to read on ajax.
 
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/KHhmZn0tpCEJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Avi

unread,
Dec 21, 2012, 7:32:31 AM12/21/12
to rubyonra...@googlegroups.com
Somehow I did manage to pass the parameter from the dropdown to controller.
But the problem is here - I am getting it as string.

In Controller:----

class ReportController < ApplicationController
  
  REPORT1         = "type =?", "Box"
  REPORT2         = "action = ? and type =?", "create", "Square"


 def report
   param = params[:audit_report] // How to convert this string to an object or static variable ? Or what is the best way to do it ?
   @report_data              = (Audited::Adapters::ActiveRecord::Audit.where(param))
// If I am passing this param, it comes as a string not as static varaible in this controller.
 end

end

In View :----

<script type="text/javascript">
        function audit_options(selected_audit){
jQuery("#reports").load("/audit_report/report?audit_report=" + selected_audit);  
        }
</script>

<%= select_tag "reports_name", options_for_select([["User1", "REPORT1"], ["User2", "REPORT2"]]), :onchange =>"audit_options(this.value)" %>

<div id="reports">
</div>


-------------------

Jim Ruther Nill

unread,
Dec 21, 2012, 8:57:13 AM12/21/12
to rubyonra...@googlegroups.com, rubyonra...@googlegroups.com


Sent from my iPhone

On 21/12/2012, at 8:32 PM, Avi <aavinas...@gmail.com> wrote:

Somehow I did manage to pass the parameter from the dropdown to controller.
But the problem is here - I am getting it as string.

In Controller:----

class ReportController < ApplicationController
  
  REPORT1         = "type =?", "Box"
  REPORT2         = "action = ? and type =?", "create", "Square"


 def report
   param = params[:audit_report] // How to convert this string to an object or static variable ? Or what is the best way to do it ?
   @report_data              = (Audited::Adapters::ActiveRecord::Audit.where(param))
// If I am passing this param, it comes as a string not as static varaible in this controller.
 end

end

In View :----

<script type="text/javascript">
        function audit_options(selected_audit){
jQuery("#reports").load("/audit_report/report?audit_report=" + selected_audit);  
        }
</script>

<%= select_tag "reports_name", options_for_select([["User1", "REPORT1"], ["User2", "REPORT2"]]), :onchange =>"audit_options(this.value)" %>

<div id="reports">
</div>

You should read on action controller basics. And also on debugging using pry or debugger so you can experiment on what you want to achieve. Good luck!

To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/a6dHrQwgkoMJ.
Reply all
Reply to author
Forward
0 new messages