0

I'm using Rails and jqPlot to display some data on a webpage. The graph needs to be flexible by date (as in, the user can select a time range, and the graph will update itself). Here's my code so far (in my View):

<%= form_tag home_event_trends_path(params[:timePeriod]), :method => 'post' do %>
  <p>
    <%= select_tag :timePeriod, options_for_select(["2 Hours Ago", "1 Week Ago", "3 Months Ago"], :selected => params[:timePeriod]) %>
    <%= submit_tag "Refresh", :name => nil %>
  </p>
<% end %>

Home is my controller, event_trends is an action, I suppose. (The path is localhost:3000/home/event_trends.3102030)

This works fine. However, I need to also pass another parameter to my controller. This webpage is already having a parameter passed to it, params[:format] (this is the 3102030 in the URL, which changes based upon what event I want to display), and I need this passed along as well. I've tried:

home_event_trends_path(params[:timePeriod][:format])
home_event_trends_path(params[:timePeriod],[:format])
home_event_trends_path(params([:timePeriod][:format]))

among others.

How do I get my controller to see multiple parameters from my View?

webster
  • 3,902
  • 6
  • 37
  • 59
XML Slayer
  • 1,530
  • 14
  • 31
  • I'd suggest avoiding using a params[:format] as this is used by Rails to know what type to render (JSON, JS, XML, HTML ...). Also take a look at http://guides.rubyonrails.org/form_helpers.html. This will help you understand how forms work in Rails – Anthony Alberto Aug 01 '12 at 15:32
  • @AnthonyAlberto: I didn't pick :format, that's just what it turned out to be. I don't know how to change that. Also, I've been up and down that page, it's all over my head... – XML Slayer Aug 01 '12 at 15:36

2 Answers2

2

I see you are passing the old timePeriod using GET and the new timePeriod using POST. Is this really what you want? Why does the controller need the old timePeriod?

Since you want to display a graph - that is, you want to retrieve data, not to cause side effects - I think you should send only the new timePeriod using GET:

<%= form_tag home_event_trends_path(), :method => 'get' do %>
    <p>
        <%= select_tag :timePeriod, options_for_select(["2 Hours Ago", "1 Week Ago", "3 Months Ago"], :selected => params[:timePeriod]) %>
        <%= select_tag :format, options_for_select([...], :selected => params[:format]) %>
        <%= submit_tag "Refresh", :name => nil %>
    </p>
<% end %>

Also, I agree with Anthony - you should try to change the format parameter - just look in the controller to see where it is used, and change the name.

Idan Arye
  • 12,402
  • 5
  • 49
  • 68
  • I don't understand what you mean by OLD and NEW timePeriod? When the user selects a new option from the dropdown menu and hits 'refresh', the browser sends `timePeriod` to the server-side Rails, at which point it queries my database within the new time range, and returns those entries which fit. I was using POST because I wanted to avoid having the parameters in the URL. – XML Slayer Aug 01 '12 at 15:46
  • You are sending `params[:timePeriod]` to `home_event_trends_path` - that means that `params[:timePeriod]` is part of the form's target path. That function is called during the rendering of the HTML page - that means that the HTML form sent to the user already contains that path with `params:[timePeriod]` - and that happens **before** the user picks the time period in the form - hence it is the **old** `timePeriod`. About the sending method, it's your choice really, but at any rate the easiest way is to send all the parameters as form elements. – Idan Arye Aug 01 '12 at 17:57
  • Okay, that makes sense. I've changed everything to GET after re-working some things. I've also renamed (I think) `:format` to `:issue`. Your code actually created another dropdown for me. This isn't necessary, as the user shouldn't be able to select the issue from the page. I just want the current issue (as defined in the URL) to be passed on as well. Also, I've managed to change the URL so that it is `/home/event_trends?issue=123456`. When I click refresh, this issue parameter is replace with `/home/event_trends?utf8=✓&timePeriod=3+Months+Ago`. I need both still! – XML Slayer Aug 01 '12 at 20:29
  • If you don't want the user to select the `issue`(at this form. At some earlier point the user does need to choose it somehow...), you can use the [hidden field tag](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-hidden_field_tag) to add that parameter as a form element. You can also send it as the form's action path(like you tried to send `timePeriod` in the code example you put in the question), but it tends to create uglier code, and is not really necessary unless you need to mix GET and POST. – Idan Arye Aug 01 '12 at 21:31
1

Nothing against Idan's answer, but this was what I ended up doing (for future Googlers...)

In my View:

<%= form_tag home_event_trends_path, :method => 'get' do %>
  <p>
    <%= select_tag :timePeriod, options_for_select(["2 Hours Ago", "1 Week Ago", "3 Months Ago"], :selected => params[:timePeriod]) %>
    <%= text_field_tag :issue, params[:issue] %>
    <%= submit_tag "Refresh", :name => nil %>
  </p>
<% end %>

And then in my Home Controller:

def event_trends
  @eventTrend = Home.eventTrend(params[:timePeriod], params[:issue])
end

And finally, the Model:

def self.eventTrend(date, id)
  # dirty work
end

This seems to work best for me. I decided to allow the URL to change for the GET request, and I'm immediately storing the issue parameter in the search box, for use later on.

Hope this helps anyone in the future!

XML Slayer
  • 1,530
  • 14
  • 31