Check box tag binding between controller and the view

I am new to RoR. The problem i am facing is that that i have a few check
box and initially when i run my app for the first time i want them all
to be selected while for every next time i want it to have the last
checked boxes checked. To do this, In my controller i have

 @all_ratings = Movie.all_ratings
 @selected_ratings = []
 if !params[:ratings].nil?
   params[:ratings].each_key do |key|
     @selected_ratings << key
   @selected_ratings = @all_ratings

While my model looks something like

class Movie < ActiveRecord::Base
  def self.all_ratings
    @all_rating = ['G', 'PG', 'PG-13', 'R']

and the form looks something like this

  - @all_ratings.each do |rating|
    = rating
    = check_box_tag "ratings[#{rating}]", 1,

= submit_tag ‘Refresh’, :id => “ratings_submit”

Now what i am trying to do in the controller is that that i wanted to
see if the param is empty if it is then i want to check all the values
so i create an instance variable @selected_ratinngs and pass all values
of rating into. If i have some values selected from the the checkbox
then i save those values in the selected_ratings.

In the view I am planning to use the selected_rating and in the
check_box_tag want to compare the selected_rating with rating to see if
its already there then the checked parameter is true otherwise.

After such a lenghty description, i want to add that this does not work
as planned any ideas.

On 21 October 2012 05:42, Muhammad S. [email protected] wrote:


Now what i am trying to do in the controller is that that i wanted to
as planned any ideas.
First have a look at the Rails Guide on Debugging. That will give you
ideas on how to debug your code in order to work out what is going
wrong. Then you should be able to see which bit of your code is not
working. Come back once you have narrowed it down to a particular bit
of code if it is still not working.


this worked for me

= check_box_tag "ratings[#{rating}]", 1, (@selected_ratings.include?