Muhammad Salman
2012-Oct-21 04:42 UTC
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
end
elsif
@selected_ratings = @all_ratings
end
While my model looks something like
class Movie < ActiveRecord::Base
def self.all_ratings
@all_rating = [''G'', ''PG'',
''PG-13'', ''R'']
end
end
and the form looks something like this
Include:
- @all_ratings.each do |rating|
= rating
= check_box_tag "ratings[#{rating}]", 1,
rating==@selected_ratings.each{|s_rating|}
= 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.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
On 21 October 2012 05:42, Muhammad Salman <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 > end > elsif > @selected_ratings = @all_ratings > end > > While my model looks something like > > class Movie < ActiveRecord::Base > def self.all_ratings > @all_rating = [''G'', ''PG'', ''PG-13'', ''R''] > end > end > > and the form looks something like this > > Include: > - @all_ratings.each do |rating| > = rating > = check_box_tag "ratings[#{rating}]", 1, > rating==@selected_ratings.each{|s_rating|} > = 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.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. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Muhammad Salman
2012-Oct-21 15:19 UTC
Re: check box tag binding between controller and the view
this worked for me
= check_box_tag "ratings[#{rating}]", 1,
(@selected_ratings.include?
rating)
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.