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.
<% 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
> 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.
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?
> 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
First 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."
The 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 ID
Specifically, 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.
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.
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
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.
> -- > 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@googlegroups.com. > 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.
On 26 September 2011 14:40, Di Zou <li...@ruby-forum.com> 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.
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.
> 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
> 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
That 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.