Okay. Either I''m missing something WAY basic or Rails and its API are lying to me (experience tells me it''s the former). Here is the basic setup for a check_box tag in the API: <%= check_box(''object'', ''method'') %> Right? So here''s what I type in. <%= check_box(''todoitems'', ''done'') %> "todoitems" is my table, and "done" is one column in that table. Hence, it is todoitem''s method. But Rails keeps giving me this error: undefined method `done'' for #<Array:0x416fb18> Now, I think that this has something to do with me making these checkboxes in a for...in loop. But I don''t understand what I''m doing wrong. Anybody care to enlighten me? -- Posted via 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sean Colquhoun wrote:> Okay. Either I''m missing something WAY basic or Rails and its API are > lying to me (experience tells me it''s the former). Here is the basic > setup for a check_box tag in the API: > > <%= check_box(''object'', ''method'') %> > > Right? > So here''s what I type in. > > <%= check_box(''todoitems'', ''done'') %> > > "todoitems" is my table, and "done" is one column in that table. Hence, > it is todoitem''s method. But Rails keeps giving me this error: > > undefined method `done'' for #<Array:0x416fb18> > > Now, I think that this has something to do with me making these > checkboxes in a for...in loop. But I don''t understand what I''m doing > wrong. Anybody care to enlighten me?You''ve set @todoitems to either a has_many association or the result of a find(:all,..,) -- a collection or an array of ActiveRecord model objects. However Rails'' AR form helpers expect their first argument to be the name of an instance variable that holds a single ActiveRecord object. So you want <% for @todoitem in @todoitems %> <%= check_box :todoitem, :done %> <% end %> -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Great. Thanks, Mark. That stops it from breaking. But I''m left with the bigger problem now. Writing it as <%= check_box :todoitem, :done %> means that every check box comes out in HTML the same. Like this: <input id="todoitem_done" name="todoitem[done]" type="checkbox" value="1" /><input name="todoitem[done]" type="hidden" value="0" /> Each one of these checkboxes has to be unique so that the function I''m going to write for them to change them into checked checkboxes knows which record to modify, right? Like this one that I found in a tutorial on the web: <%= check_box :todoitem, :done, "onclick" => "document.location.href=''/todoitems/toggle_check/#{t.id}''" %> The guy is basically hacking together a URL and strapping it to an onclick event. But this way only lets you send one value with it: the id of the checkbox item. I need to send the id of the project that it''s associated with as well. Like this maybe: <%= check_box :todoitem, :done, "onclick" => "document.location.href=''/todoitems/toggle_check/#{t.id}&projectid=#{@thisproject.id}''" %> But Rails complains that it can''t find the value in params[:projectid] when I do this. Is there a way to do this? -- Posted via 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
API says - check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0") Also give the following example: check_box("puppy", "gooddog", {}, "yes", "no") <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" /> <input name="puppy[gooddog]" type="hidden" value="no" /> in light of that you can use the following: <% for todoitem in @todoitems %> <%= check_box :todoitem, todoitem.done, {}, todoitem.id, nil %> <% end %> This should set the id as the checked value and nil to the unchecked vaue, but you can use whatever column you want. Cam On Jun 6, 5:01 pm, Sean Colquhoun <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Great. Thanks, Mark. That stops it from breaking. > > But I''m left with the bigger problem now. Writing it as <%= check_box > :todoitem, :done %> means that every check box comes out in HTML the > same. Like this: > > <input id="todoitem_done" name="todoitem[done]" type="checkbox" > value="1" /><input name="todoitem[done]" type="hidden" value="0" /> > > Each one of these checkboxes has to be unique so that the function I''m > going to write for them to change them into checked checkboxes knows > which record to modify, right? Like this one that I found in a tutorial > on the web: > > <%= check_box :todoitem, :done, "onclick" => > "document.location.href=''/todoitems/toggle_check/#{t.id}''" %> > > The guy is basically hacking together a URL and strapping it to an > onclick event. But this way only lets you send one value with it: the id > of the checkbox item. I need to send the id of the project that it''s > associated with as well. Like this maybe: > > <%= check_box :todoitem, :done, "onclick" => > "document.location.href=''/todoitems/toggle_check/#{t.id}&projectid...-YUIi3IGNGSIHEdyIqKOTxg@public.gmane.org}''" > %> > > But Rails complains that it can''t find the value in params[:projectid] > when I do this. > > Is there a way to do this? > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sean Colquhoun wrote:> Great. Thanks, Mark. That stops it from breaking. > > But I''m left with the bigger problem now. Writing it as <%= check_box > :todoitem, :done %> means that every check box comes out in HTML the > same. Like this: > > <input id="todoitem_done" name="todoitem[done]" type="checkbox" > value="1" /><input name="todoitem[done]" type="hidden" value="0" /> > > Each one of these checkboxes has to be unique so that the function I''m > going to write for them to change them into checked checkboxes knows > which record to modify, right? Like this one that I found in a tutorial > on the web:If the todoitems already exist in the database, you can have their ids sent in a normal form post by using <%= check_box ''todoitem[]'', :done %> The project_id should either be a part of the form url, or set in a form hidden field. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks guys. Working now! -- Posted via 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---