SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [rails] select helper problem

    hi all,

    i have the following code:

    Code:
            <select name="mileage[vehicle_id]">
              <% @vehicles.each do |vehicle| %>
                <option value="<%= vehicle.id %>"> 
                  <%= vehicle.make + ' ' + vehicle.model + ', ' + vehicle.license %>
                </option>
              <% end %>
            </select>
    that i want to translate into the select helper. the problem is that i don't know how to handle to the do loop within the helper function.

    i tried...

    [code]
    <%= select("mileage",
    "vehicle_id",
    @vehicles.each do |vehicle|
    vehicle.id +
    ' ' +
    (vehicle.make +
    ' ' +
    vehicle.model +
    ', ' +
    vehicle.license) +
    ' '
    end)
    %>

    i want to send the vehicle_id while displaying a combination of the make, model and license (ie, "toyota corolla, abc123").

    this obviously didn't work, hence, i'm here.

    the error i received is:

    "String can't be coerced into Fixnum" and displays the select helper code.

    any guidance is appreciated.

    tia...

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    on a related note, how do i get...

    Code:
    <&#37;= select("mileage", "submitted", %w{False True}) %>
    ...to default to either True or False (whichever is desired - the point is to get the code to force the html selected value)?

    i've done plenty of searching and this simple functionality seems to be buried somewhere pretty deep.

    even AWDWR_1 gets it wrong. they say the following code will highlight Dave as the selected value...

    Code:
    <%= select(:variable, :name, %w{ Andy Bert Chas Dave Eric Fred }) %>
    ...when it doesn't. rather, it highlights Andy as the selected value (it is the first value).

    now that i think about it, do i need to code so that my intended highlighted value is the first value in the array in order to hack it so that it gets displayed?

    there has to be a better way, no?

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skeeterbug View Post
    on a related note, how do i get...

    Code:
    <%= select("mileage", "submitted", %w{False True}) %>
    ...to default to either True or False (whichever is desired - the point is to get the code to force the html selected value)?

    i've done plenty of searching and this simple functionality seems to be buried somewhere pretty deep.

    even AWDWR_1 gets it wrong. they say the following code will highlight Dave as the selected value...

    Code:
    <%= select(:variable, :name, %w{ Andy Bert Chas Dave Eric Fred }) %>
    ...when it doesn't. rather, it highlights Andy as the selected value (it is the first value).

    now that i think about it, do i need to code so that my intended highlighted value is the first value in the array in order to hack it so that it gets displayed?

    there has to be a better way, no?
    i think this is doing the trick for me...

    View:
    Code:
              <%= select("mileage", "submitted", [ ["False", "false"],
                                                    ["True", "true"] 
                                                  ],
                        {:selected => @submitted_value}
                        )
              %>
    Controller:
    Code:
        @mileage = Mileage.find(@params["id"])
        @vehicles = Vehicle.find_all
        if @mileage.submitted?
          @submitted_value = "true"
        else
          @submitted_value = "false"
        end
        if @mileage.reimbursed?
          @reimbursed_value = "true"
        else
          @reimbursed_value = "false"
        end
    it doesn't appear to be so difficult once one gets it working.

    i may be able to parlay this success to help solve the initial problem in this thread - i'll continue trying tomorrow. even so, please don't hold back any hints that may help.

    tia...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •