How to select table column names in a view and pass to controller in rails?

Posted by zachd1_618 on Stack Overflow See other posts from Stack Overflow or by zachd1_618
Published on 2012-03-19T23:25:19Z Indexed on 2012/03/19 23:30 UTC
Read the original article Hit count: 159

Filed under:
|
|
|
|

So I am new to Rails, and OO programming in general. I have some grasp of the MVC architecture. My goal is to make a (nearly) completely dynamic plug-and-play plotting web server. I am fairly confused with params, forms, and select helpers.

What I want to do is use Rails drop downs to basically pass parameters as strings to my controller, which will use the params to select certain column data from my database and plot it dynamically. I have the latter part of the task working, but I can't seem to pass values from my view to controller.

For simplicity's sake, say my database schema looks like this:

                 --------------Plot---------------

                 |____x____|____y1____|____y2____|
                 |    1    |    1     |    1     |
                 |    2    |    2     |    4     |
                 |    3    |    3     |    9     | 
                 |    4    |    4     |    16    | 
                 |    5    |    5     |    25    | 
                 ...

and in my Model, I have dynamic selector scopes that will let me select just certain columns of data: in Plot.rb

class Plot < ActiveRecord::Base
scope :select_var, lambda {|varname| select(varname)}
scope :between_x, lambda {|x1,x2| where("x BETWEEN ? and ?","#{x1}","#{x2}")}

So this way, I can call:

irb>>@p1 = Plot.select_var(['x','y1']).between_x(1,3)

and get in return a class where @p1.x and @p1.y1 are my only attributes, only for values between x=1 to x=4, which I dynamically plot.

I want to start off in a view (plot/index), where I can dynamically select which variable names (table column names), and which rows from the database to fetch and plot. The problem is, most select helpers don't seem to work with columns in the database, only rows.

So to select columns, I first get an array of column names that exist in my database with a function I wrote.

Plots Controller
def index
  d=Plot.first
  @tags = d.list_vars
end

So @tags = ['x','y1','y2']

Then in my plot/index.html.erb I try to use a drop down to select wich variables I send back to the controller.

index.html.erb
<%= select_tag( :variable,     options_for_select(@plots.first.list_vars,:name,:multiple=>:true) )%>
<%= button_to 'Plot now!', :controller =>"plots/plot_vars", :variable => params[:variable]%>

Finally, in the controller again

Plots controller
...
def plot_vars
  @plot_data=Plot.select_vars([params[:variable]])
end

The problem is everytime I try this (or one of a hundred variations thereof), the params[:variable] is nill.

How can I use a drop down to pass a parameter with string variable names to the controller?

Sorry its so long, I have been struggling with this for about a month now. :-( I think my biggest problem is that this setup doesn't really match the Rails architecture. I don't have "users" and "articles" as individual entities. I really have a data structure, not a data object. Trying to work with the structure in terms of data object speak is not necessarily the easiest thing to do I think.

For background: My actual database has about 250 columns and a couple million rows, and they get changed and modified from time to time. I know I can make the database smarter, but its not worth it on my end. I work at a scientific institute where there are a ton of projects with databases just like this. Each one has a web developer that spends months setting up a web interface and their own janky plotting setups. I want to make this completely dynamic, as a plug-and-play solution so all you have to do is specify your database connection, and this rails setup will automatically show and plot which data you want in it. I am more of a sequential programmer and number cruncher, as are many people here. I think this project could be very helpful in the end, but its difficult to figure out for me right now.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about forms