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:
<% 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 %>
<%= render :partial => “summary_detail”, :locals => {:carrier =>
carrier, :f => f} %>
<% end %>
<%= submit_tag "Update" %>
<% end %>
With my partial:
<%= h(carrier.name.to_s()) -%> |
<%= h(carrier.country.to_s()) -%> |
<%= select(:carrier, “country”, @countries) -%> |
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.”
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
def update
carrier = HhActiveCarrier.find(params[:name])
redirect_to( :action => "index" )
end
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.
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