3

Although the question at 'Passing Multiple Parameters in a form_tag' provides some information regarding how one can pass parameters within a form_tag the poster ultimately decided to pursue a different means of achieving said ends (specifically using hidden form fields).

When attempting to use a line of code in a view like:

form_tag(movies_path({:foo = "bar"}), {:id => "ratings_form", :method => :get}) do

it seems as though RoR will not make the value of 'foo' available to the controller; is there another syntax which should be used to pass a parameter as part of the path in such a scenario?

If one wishes to pass the parameter as part of the URL one would think such an approach would be correct (and, in fact, the HTML generated indicates as much since the resultant <form> tag contains an action="/movies?foo=bar" statement)... but the controller evaluates params[:foo] as '' and not bar?)

Community
  • 1
  • 1
Rishi Chopra
  • 45
  • 1
  • 9

2 Answers2

2

I think that there are two basic issues at work here...

The first is the actual parameter passing itself. The specification designates that with the GET method the form data set is appended to the URI and with a POST method, the form data set is included in the body of the form. Submitting a form generates the parameters from the form fields, even if it is a GET request (as you have specified) and URL parameters have been specified (the specified URL parameters are not part of the URI). Unless you want "linkable" state, like for a search form, you are better off sticking with a POST for form submissions.

The second problem, as I see it, is fairly common. The Rails routing system is hard to understand, at first, for many people. I am going to assume, going forward, that movies_path is from a corresponding resources :movies line in your routes.rb file. If so, the route is not set up to pass information along in the URL, which will be /movies. There will also be a movie_path that is set up to pass information along to the controller via the URL. A common usage would be to use an instance of the type of object the resource is modeling as an argument, like movie_path(@movie). This type of usage will run to_params on the passed in object, which by default returns the id of the object (but can be easily overridden). This results in params[:id] being available in the controller. If you just want to send arbitrary information not backed by a resource along as part of the URL, to become available in the controller, you probably just want to define a custom route to do so, like this:

match '/movies/:foo' => 'movies#index', :as => :movies

Which would make params[:foo] == 'bar' in the controller, when used like this:

movies_path('bar')

Anyway, hope this helps, good luck.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
  • Dude, much appreciated! Based on the information which was given to our class (CS169.x1@edX) I couldn't tell what the problem was as we set up (in the first part of the homework assignment associated with this problem) a means of passing parameters via a call like 'movies_path(:sort_by => "title")'... but the statement was not included within the scope of a 'form_tag' HAML element; in this case linkable state was intended so I had assumed that the method I had tried would be sufficient (we only get to passing parameters via 'session' in the next part of the assignment). – Rishi Chopra Nov 08 '12 at 23:01
  • BTW, I noticed that adding the `routes.rb` line which you mentioned does add a route... but that the output from the `resources :movies` line which generates the standard set of CRUD actions is affected as the first column of the output indicates that "movies" is associated with the new index action; testing, however, indicates that the index route still works. PS: I see a spelling error at the end of your post ("reource") but I can't fix it 'cause the edit screen wants six changes when I try... – Rishi Chopra Nov 08 '12 at 23:03
  • 1
    Yeah, routes are processed in the order they are defined (top down), and will hit on the first match it comes across. You can have "overlapping" routes, but only the first one will actually route. – Brad Werth Nov 08 '12 at 23:12
0

Couldn’t you just add a hidden field?

hidden_field_tag 'foo', 'bar'
Buck Doyle
  • 6,333
  • 1
  • 22
  • 35
  • The question was prefaced to indicate that such a solution has already been identified in existing posts (and, as such, is not being solicited); if there's a better way to express the same in this community please don't hesitate to let me know. – Rishi Chopra Nov 07 '12 at 18:00
  • Oops, I’m sorry! I didn’t see that you’d dismissed this as a solution. – Buck Doyle Nov 07 '12 at 22:40
  • No worries; I realize the ends can be achieved in lots of ways... but in this particular case there is an epistemological question which is at issue and it seems difficult (if not impossible) to address that question without understanding the specific RoR/HAML problem... BTW, any ideas? I tied running the debugger and indeed everything one would expect to be passed in params is there except "foo"... – Rishi Chopra Nov 08 '12 at 19:23
  • I don’t think this has anything to do with Rails or HAML. I just tried it outside Rails and it still doesn’t work. Various voices on the web [agree](http://www.google.ca/search?q=form+action+url+with+parameters). You could rig up some Javascript hack, but why bother? – Buck Doyle Nov 09 '12 at 05:28
  • Yes, it seems like Brad has addressed the issue in the first part of his reply... but I don't entirely agree that the problem has nothing to do with Rails or HAML... HAML is a templating language and Rails is a framework: as such, it's not entirely unreasonable to expect that something can be done with your code behind the scenes that makes it formally correct (even if what you've specified is not precisely so); the Java compiler performs all sorts of optimizations for the code one writes (e.g. [link]http://stackoverflow.com/questions/2391219/java-compiler-optimization) – Rishi Chopra Nov 09 '12 at 17:17
  • and something like JSP is compiled as a servelet behind-the-scenes (just as the Ruby interpreter processes poetry). As a beginner with both HTML and RoR/HAML I can only rely on what I know (familiarity with six or seven other languages)... but as a student with many hours of completed coursework I know that blindly trusting the instructors of the course to have laid out a sequence of exercises which illucidate a reasoned subset of concepts makes sense (especially if a course is new and is the subject of ongoing refinement); – Rishi Chopra Nov 09 '12 at 17:18
  • since the specific issue is not something which I had previously encountered in my experience with HTML and the skeleton code provided had specified `GET` (with parameter passing via the path construction helper method being introduced in the preceding exercise) the approach which I initially tried seemed natural and hence the decision to post... – Rishi Chopra Nov 09 '12 at 17:18