Rails check_box_tag

rails newbie need help with form.
I have a movie application. In the index I list all the movies and in
addition i have ratings check boxes for (R, PG, PG-13, G)
basically when you check a box and submit, the page should refresh
with movies with the chosen rating. So if you check R,PG; only movies
rated R and PG should be displayed.
At the moment, when i check any box and submit the query seems to work
but no movies are returned. When i checked the log this is what i get.

Parameters: {“utf8”=>“✓”, “ratings”=>{“R”=>“on”}}
Movie Load (0.1ms) SELECT “movies”.* FROM “movies” WHERE (rating
LIKE’—

  • R
  • ‘‘on’’
    ')

here’s my movie.rb model code.
def self.checkBoxTest(ratings)
if ratings
where(‘rating LIKE?’, ratings)
else
scoped
end
end

note: using haml for views:
index.html.haml
%h1 All Movies
= form_tag movies_path, :method => :get do
Include:

  • @all_ratings.each do |rating|
    ="#{rating}"
    = check_box_tag “ratings[#{rating}]”, params[:ratings]
    = submit_tag ‘Refresh’, :name => nil
    %table#movies

def index
@movies = Movie.order(sort_column)
@movies = Movie.checkBoxTest(params[:ratings])
@all_ratings = Movie.find_all_rating
end
here’s my controller index method

On Mar 13, 2012, at 12:21 PM, dasibre wrote:

Movie Load (0.1ms) SELECT “movies”.* FROM “movies” WHERE (rating
LIKE’—

  • R
  • ‘‘on’’
    ')

This looks very much like it’s trying to compare the field with YAML.
What is the field type for rating, and have you added serialize to it
anywhere? (I don’t see that in your model, but you haven’t quoted all of
it.)

here’s my movie.rb model code.
def self.checkBoxTest(ratings)
if ratings
where(‘rating LIKE?’, ratings)
else
scoped
end
end

Instead of LIKE, try IN. You’re trying to compare an array with a single
value, if I’m reading this correctly.

If only the R is checked, your params[:ratings] will equal [‘R’]. If R
and PG are checked, it will look like [‘R’,‘PG’]. So a query like this
in raw SQL would look like SELECT * FROM movies WHERE rating IN
(‘R’,‘PG’);

Walter

Thanks for the quick response Walter.
Rating is a text field.
“SERIALIZE” don’t have it in my model, don’t know what it is…will
look it up.

As you can tell by the method name “checkBoxTest” I’m testing the
feature one check box at a time, but
you make a good point about using LIKE, IN to compare. I will try
that.

I’m not sure if this is right but, I think the check_box_tag passes an
extra parameter “on” in addition to the Ratings and i’m not sure how
to deal with that.

Thank you very much for your help.

On Mar 13, 2012, at 12:54 PM, dasibre wrote:

Thanks for the quick response Walter.
Rating is a text field.
“SERIALIZE” don’t have it in my model, don’t know what it is…will
look it up.

As you can tell by the method name “checkBoxTest” I’m testing the
feature one check box at a time, but
you make a good point about using LIKE, IN to compare. I will try
that.

I meant to use IN rather than LIKE. LIKE is “case-insensitive text
comparison” and IN is “does this value occur in that array”.
params[:rating] is a hash, but it will get converted to an array if you
pass it into the where() method the way you are doing it. I’m still not
clear why it’s being converted to YAML the way it is in your LIKE
clause, but I guess that’s the only string-like thing that would
preserve the meaning of the hash.

It’s not as neat as your HAML, but the following ERB:

<%= check_box_tag "ratings[G]", 'G', (params[:ratings].include? 'G') %> <%= label_tag "ratings[G]", "G" %>

<%= check_box_tag "ratings[PG]", 'PG', (params[:ratings].include? 'PG') %> <%= label_tag "ratings[PG]", "PG" %>

<%= check_box_tag "ratings[R]", 'R', (params[:ratings].include? 'R') %> <%= label_tag "ratings[R]", "R" %>

<%= check_box_tag "ratings[NC-17]", 'NC-17', (params[:ratings].include? 'NC-17') %> <%= label_tag "ratings[NC-17]", "NC-17" %>

<%= check_box_tag "ratings[X]", 'X', (params[:ratings].include? 'X') %> <%= label_tag "ratings[X]", "X" %>

<%= check_box_tag "ratings[NR]", 'NR', (params[:ratings].include? 'NR') %> <%= label_tag "ratings[NR]", "NR" %>

Expands to this HTML:

G

PG

R

NC-17

X

NR

And when that form is submitted, it will appear at the controller as
params[:ratings]. If you pass it into your where clause, using IN
instead of LIKE, you should get a query like the one I outlined. As a
bonus, the checkboxes will maintain their state through a form request.

Walter

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs