Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Problem with drop down in form
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Di Zou  
View profile  
 More options Sep 26 2011, 9:40 am
From: Di Zou <li...@ruby-forum.com>
Date: Mon, 26 Sep 2011 15:40:01 +0200
Local: Mon, Sep 26 2011 9:40 am
Subject: Problem with drop down in form
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Walker  
View profile  
 More options Sep 28 2011, 12:06 am
From: Robert Walker <li...@ruby-forum.com>
Date: Wed, 28 Sep 2011 06:06:45 +0200
Local: Wed, Sep 28 2011 12:06 am
Subject: Re: Problem with drop down in form
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| %>

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

--
Posted via http://www.ruby-forum.com/.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Di Zou  
View profile  
 More options Sep 28 2011, 1:40 pm
From: Di Zou <li...@ruby-forum.com>
Date: Wed, 28 Sep 2011 19:40:59 +0200
Local: Wed, Sep 28 2011 1:40 pm
Subject: Re: Problem with drop down in form
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Di Zou  
View profile  
 More options Sep 28 2011, 2:24 pm
From: Di Zou <li...@ruby-forum.com>
Date: Wed, 28 Sep 2011 20:24:42 +0200
Local: Wed, Sep 28 2011 2:24 pm
Subject: Re: Problem with drop down in form
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Colin Law  
View profile  
 More options Sep 29 2011, 3:59 am
From: Colin Law <clan...@googlemail.com>
Date: Thu, 29 Sep 2011 08:59:24 +0100
Local: Thurs, Sep 29 2011 3:59 am
Subject: Re: [Rails] Re: Problem with drop down in form
On 28 September 2011 18:40, Di Zou <li...@ruby-forum.com> 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

--
gplus.to/clanlaw

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Colin Law  
View profile  
 More options Sep 26 2011, 9:54 am
From: Colin Law <clan...@googlemail.com>
Date: Mon, 26 Sep 2011 14:54:32 +0100
Local: Mon, Sep 26 2011 9:54 am
Subject: Re: [Rails] Problem with drop down in form
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.

> 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.

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.

Colin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »