0

In the code sample posted below, I am trying to allow the user of the page to filter out which projects should be displayed by user. I am able to successfully filter the results by manually modifying the parameters passed in the address bar. But when I click the Filter button on the page it does nothing at all. It should refresh the page with the new parameters being passed when the button is pressed. Any ideas as to why this happens? Do I need to add anything in my controller?

View:

= form_tag projects_path, :method => :get do
  Include:
- @all_users.each do |user|
  = user
  = check_box_tag "users[#{user}]", 1, @all_users.include?(user), :id => "users_#{user}"
= submit_tag 'Filter', {:id => "users_submit"}

Console After Loading Main Page:

[2014-10-13 23:32:55] INFO  WEBrick 1.3.1
[2014-10-13 23:32:55] INFO  ruby 1.9.3 (2013-06-27) [i686-linux]
[2014-10-13 23:32:55] INFO  WEBrick::HTTPServer#start: pid=2718 port=3000


Started GET "/projects" for 127.0.0.1 at 2014-10-13 23:32:58 -0300
Connecting to database specified by database.yml
Processing by ProjectsController#index as HTML
Project Load (0.2ms)  SELECT DISTINCT user FROM "projects" 
Redirected to http://localhost:3000/projects?users%5BCoulson%5D=1&users%5BFitz%5D=1&users%5BMay%5D=1&users%5BSimmons%5D=1&users%5BWard%5D=1
Project Load (0.2ms)  SELECT "projects".* FROM "projects" ORDER BY id
Completed 302 Found in 9ms (ActiveRecord: 0.4ms)


Started GET "/projects?users%5BCoulson%5D=1&users%5BFitz%5D=1&users%5BMay%5D=1&users%5BSimmons%5D=1&users%5BWard%5D=1" for 127.0.0.1 at 2014-10-13 23:32:59 -0300
Processing by ProjectsController#index as HTML
Parameters: {"users"=>{"Coulson"=>"1", "Fitz"=>"1", "May"=>"1", "Simmons"=>"1", "Ward"=>"1"}}
Project Load (0.2ms)  SELECT DISTINCT user FROM "projects" 
Project Load (0.2ms)  SELECT "projects".* FROM "projects" ORDER BY id
Rendered projects/index.html.haml within layouts/application (11.8ms)
Completed 200 OK in 61ms (Views: 59.3ms | ActiveRecord: 0.4ms)
[2014-10-13 23:32:59] WARN  Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-10-13 23:32:59 -0300
Served asset /jquery.js - 304 Not Modified (5ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-10-13 23:32:59 -0300
Served asset /application.css - 304 Not Modified (1ms)
[2014-10-13 23:32:59] WARN  Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true
[2014-10-13 23:32:59] WARN  Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-10-13 23:32:59 -0300
Served asset /application.js - 304 Not Modified (4ms)


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-10-13 23:32:59 -0300
Served asset /jquery_ujs.js - 304 Not Modified (1ms)

[2014-10-13 23:32:59] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

[2014-10-13 23:32:59] WARN  Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true

After clicking the submit button, nothing happens in the console, or does the browser seem to do anything. Rails 3.2.14 Ubuntu 12.04 LTS

Appleblomb
  • 45
  • 1
  • 6
  • Is your code not properly indented just here? I mean, is the check_box_tag indented inside form_tag in your real application? Can you paste the server log after clicking submit? – Dario Barrionuevo Oct 14 '14 at 02:24
  • The code is indented differently in the app, I wasn't sure how to preserve it since 4 spaces determines a code block here. When clicking submit the console produces no output, so I added the main page being loaded to see if that would help. Is there anywhere else I can go for a more detailed log? – Appleblomb Oct 14 '14 at 02:50
  • Code formatting appears as in file now. – Appleblomb Oct 14 '14 at 03:01
  • Ok, your form is working just as you instructed it: It's making a GET request to the index action of your ProjectsController with this parameters: {"users"=>{"Coulson"=>"1", "Fitz"=>"1", "May"=>"1", "Simmons"=>"1", "Ward"=>"1"}} So, I believe the issue is on a mismatch between how you're expecting the parameters in the controller, and how you're sending them. When you say you manually modify parameters in the URL, how do you do that? Check how the request sends params in your manual edited version, vs the form version. Is that clear? – Dario Barrionuevo Oct 14 '14 at 03:05
  • Whenever I click the filter button, it does not show a GET request in the console. The output I posted was from loading the page the first time as stated above. My address bar looks like this: http://localhost:3000/projects?users[Coulson]=1&users[Fitz]=1&users[May]=1&users[Simmons]=1&users[Ward]=1 If I remove one of the parameters like &users[Ward]=1 the box will be unchecked and the user's projects will not be shown in the list. This is intentional, however when I uncheck the box for Ward and click filter it does not remove it and trigger a refresh with the new parameters. – Appleblomb Oct 14 '14 at 03:25
  • Ok, I'll insist on the indentation, because you say it doesn't do anything when you click submit, it doesn't sound good. The submit button is not indented inside the form, is it in your app? – Dario Barrionuevo Oct 14 '14 at 03:46
  • I started viewing the HTML generated by the page, apparently it is closing the form tag after "Includes:" rather than the end of the form. I did a live edit and put it in the proper place then the page started working as intended. Now just to fix the HAML to generate it properly. Thanks for the help! – Appleblomb Oct 14 '14 at 04:02

1 Answers1

0

Code was fixed with proper indentation:

= form_tag movies_path, :id => "ratings_form", :method => :get do
  Include: 
  - @all_ratings.each do |rating|
    = rating
    = check_box_tag "ratings[#{rating}]", 1, @ratings.include?(rating), :id => "ratings_#{rating}"
  = submit_tag "Refresh", :id => "ratings_submit"
Appleblomb
  • 45
  • 1
  • 6