I am using Rails 2.3.8. I have a view with a list of mobile carriers. I want to assign a country to each mobile carrier. I do this by having a drop down list with available countries for the user to select. This is my form: <table> <tbody> <% form_for :HhActiveCarrier, @carriers, :url => { :action => "update" } do |f| %> <% for carrier in @carriers %> <tr> <%= render :partial => "summary_detail", :locals => {:carrier => carrier, :f => f} %> </tr> <% end %> </tbody> </table> <%= submit_tag "Update" %> <% end %> With my partial: <td class="tn"><%= h(carrier.name.to_s()) -%></td> <td class="sc"><%= h(carrier.country.to_s()) -%></td> <td class="sc"><%= select(:carrier, "country", @countries) -%></td> This is the controller where I define the variables: class ActiveCarriersController < ApplicationController def index @carriers = HhActiveCarrier.find(:all) for carrier in @carriers country = carrier["country"] if country.nil? carrier["country"] = "none" end end @countries = ["USA", "UK", "Canada"] end def update carrier = HhActiveCarrier.find(params[:name]) redirect_to( :action => "index" ) end When I click the "Update" button, I want to have the "country" of each HHActiveCarrier to be set. Right now this is the error I get: Couldn''t find HhActiveCarrier without an ID How do I solve this problem? Thanks. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 26 September 2011 14:40, Di Zou <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I am using Rails 2.3.8. I have a view with a list of mobile carriers. I > want to assign a country to each mobile carrier. I do this by having a > drop down list with available countries for the user to select. > > This is my form: > > <table> > <tbody> > <% form_for :HhActiveCarrier, @carriers, :url => { :action => "update"It is not legal html to have a form inside a table, unless it is entirely within one cell of the table. You may put the complete table inside the form.> } do |f| %> > <% for carrier in @carriers %> > <tr> > <%= render :partial => "summary_detail", :locals => {:carrier => > carrier, :f => f} %> > </tr> > <% end %> > </tbody> > </table> > <%= submit_tag "Update" %> > <% end %> > > With my partial: > <td class="tn"><%= h(carrier.name.to_s()) -%></td> > <td class="sc"><%= h(carrier.country.to_s()) -%></td> > <td class="sc"><%= select(:carrier, "country", @countries) -%></td> > > This is the controller where I define the variables: > > class ActiveCarriersController < ApplicationController > > def index > @carriers = HhActiveCarrier.find(:all) > for carrier in @carriers > country = carrier["country"] > if country.nil? > carrier["country"] = "none" > end > end > @countries = ["USA", "UK", "Canada"] > end > > def update > carrier = HhActiveCarrier.find(params[:name]) > > redirect_to( :action => "index" ) > end > > > When I click the "Update" button, I want to have the "country" of each > HHActiveCarrier to be set. Right now this is the error I get: > > Couldn''t find HhActiveCarrier without an IDThat means that you have attempted a find on that table without specifying an id. Look at the line of code the error points to to find out why. You can look in log/development.log to see what params are being passed. If you still cannot see it the have a look at the Rails Guide on debugging, in particular you could use ruby-debug to break into your code and inspect the data and follow the flow. 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Di Zou wrote in post #1023800:> I am using Rails 2.3.8. I have a view with a list of mobile carriers. I > want to assign a country to each mobile carrier. I do this by having a > drop down list with available countries for the user to select.Hum, where to begin?> This is my form: > > <table> > <tbody> > <% form_for :HhActiveCarrier, @carriers, :url => { :action => "update"The "form_for" helper used with a collection doesn''t make any sense. Take a look at the examples in the Rails docs. Do you see any mention of using form_for with a collection of model instances? Example: <%= form_for @offer do |f| %>> } do |f| %> > <% for carrier in @carriers %> > <tr> > <%= render :partial => "summary_detail", :locals => {:carrier => > carrier, :f => f} %> > </tr> > <% end %> > </tbody> > </table> > <%= submit_tag "Update" %> > <% end %> > > With my partial: > <td class="tn"><%= h(carrier.name.to_s()) -%></td> > <td class="sc"><%= h(carrier.country.to_s()) -%></td> > <td class="sc"><%= select(:carrier, "country", @countries) -%></td> > > This is the controller where I define the variables: > > class ActiveCarriersController < ApplicationController > > def index > @carriers = HhActiveCarrier.find(:all) > for carrier in @carriers > country = carrier["country"] > if country.nil? > carrier["country"] = "none" > end > end > @countries = ["USA", "UK", "Canada"] > endFirst of all, this is too much logic for a Rails controller action method. You should consider moving some of this logic into the model class. Second. In a typical Rails application the index action is accessed via a GET request, which should be consider "safe." See the link below for an explanation the meaning of "Safe Methods." http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html> def update > carrier = HhActiveCarrier.find(params[:name]) > > redirect_to( :action => "index" ) > endThe redirect used here is going to lose all context, which means the "find" that assigns the local variable "carrier" is useless. Even if the context was not reset by the redirection, the scope of "carrier" is local to the update method, which means you are attempting to find something and then doing nothing with the result.> When I click the "Update" button, I want to have the "country" of each > HHActiveCarrier to be set. Right now this is the error I get: > > Couldn''t find HhActiveCarrier without an IDSpecifically, this error is because "form_for" is intended to be used with an instance of a model object not a collection of model objects, and the update action in a Rails controller is intended to update the one model instance that is reference in the URL. Also, you are using the find method, which expects an id for finding the carrier referenced in the URL. If you were attempting to find a carrier (or carriers) by a name column then take a look at the dynamic finders provided by Rails. Such as "find_by_name(params[:name])" However, this is not going to help you in this scenario. PUT: http://example.com/hh_active_carrier/1 where the "1" is the ID of the carrier to be updated.> How do I solve this problem? Thanks.There''s too much to explain for me to even attempt to tell you how to fix this. My recommendation is to go back and study the Rails guides on how to use the various action methods and form helpers in a Rails application. http://guides.rubyonrails.org/index.html -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
So what I have is a table called HHActiveCarrier with three columns, an "Name", "ID", and "Country". Each row in the table is a carrier. I''m trying to make something that lets you update the country of each carrier all at once. I went back in my code and changed a bunch of stuff. My controller is still generally the same. I''m going to leave the code in ActiveCarrierController.index the same for now. Here is what I have now: class ActiveCarriersController < ApplicationController def index @carriers = HhActiveCarrier.find(:all) for carrier in @carriers country = carrier["country"] if country.nil? carrier["country"] = "none" end end @countries = ["USA", "UK", "Canada"] end def update redirect_to( :action => "index" ) end end This is what my form looks like now: <table> <thead> <tr> <th><%= "Active Carrier" %></th> <th><%= "" %></th> <th><%= "Country" %></th> </tr> </thead> <tbody> <%= form_tag :action => ''update'', :id => params[:id] %> <% for @carrier in @carriers %> <% fields_for "carrier[]" do |f| %> <tr> <td class="tn"><%= f.text_field :name, :readonly => true %></td> <td class="tn"><%= f.hidden_field :id %></td> <td class="tn"><%= f.select :country, @countries %></td> </tr> <% end %> <% end %> </tbody> </table> <%= submit_tag ''Update'' %> <%= end_form_tag %> If I make a selection in the drop down list and I click "Update", this is what my request_parameters look like: request_parameters: {"commit"=>"Update", "action"=>"update", "carrier"=> {"6"=>{"name"=>"Sprint Nextel", "country"=>"USA", "id"=>"6"}, "7"=>{"name"=>"Verizon", "country"=>"USA", "id"=>"7"}, "2"=>{"name"=>"T-Mobile US", "country"=>"UK", "id"=>"2"}, "4"=>{"name"=>"AT&T", "country"=>"USA", "id"=>"4"}, "5"=>{"name"=>"Sprint Nextel", "country"=>"USA", "id"=>"5"}}, "controller"=>"active_carriers"} How do I take the data in the request_parameters and update each row in my database? Thanks. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I solved this. This is what I am doing in my update function now: @params[:carrier].each do |id, data| carrier = HhActiveCarrier.find(id) carrier.update_attributes(data) I also removed: for carrier in @carriers country = carrier["country"] if country.nil? carrier["country"] = "none" end end from my index function and changed my select to this: <td class="tn"><%= f.select :country, @countries, { :include_blank => true } %></td> -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 28 September 2011 18:40, Di Zou <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> ... > This is what my form looks like now: > > <table> > <thead> > <tr> > <th><%= "Active Carrier" %></th> > <th><%= "" %></th> > <th><%= "Country" %></th> > </tr> > </thead> > <tbody> > <%= form_tag :action => ''update'', :id => params[:id] %>As I mentioned before it is not legal html to have a form in a table like this. Try copying the complete html of the page (View > Page Source or similar in your browser) and paste it into the w3c html validator. This means that even though it may appear to work ok you are relying on the browser attempting the make the best of a bad job and different browsers may display your page differently. In addition, even if you test it with all known browsers the next release of a browser may do something different. Colin> <% for @carrier in @carriers %> > <% fields_for "carrier[]" do |f| %> > <tr> > <td class="tn"><%= f.text_field :name, :readonly => true > %></td> > <td class="tn"><%= f.hidden_field :id %></td> > <td class="tn"><%= f.select :country, @countries %></td> > </tr> > <% end %> > <% end %> > </tbody> > </table> > <%= submit_tag ''Update'' %> > <%= end_form_tag %> > > If I make a selection in the drop down list and I click "Update", this > is what my request_parameters look like: > > request_parameters: {"commit"=>"Update", > "action"=>"update", > "carrier"=> > {"6"=>{"name"=>"Sprint Nextel", "country"=>"USA", "id"=>"6"}, > "7"=>{"name"=>"Verizon", "country"=>"USA", "id"=>"7"}, > "2"=>{"name"=>"T-Mobile US", "country"=>"UK", "id"=>"2"}, > "4"=>{"name"=>"AT&T", "country"=>"USA", "id"=>"4"}, > "5"=>{"name"=>"Sprint Nextel", "country"=>"USA", "id"=>"5"}}, > "controller"=>"active_carriers"} > > How do I take the data in the request_parameters and update each row in > my database? Thanks. > > -- > 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@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- gplus.to/clanlaw -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.