如何在Rails中将此下拉列表转换为f.select? [英] How do I convert this dropdown to a f.select in Rails?

查看:100
本文介绍了如何在Rails中将此下拉列表转换为f.select?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails的初学者,并且正在从事现有的Rails 2项目.在我的应用程序中,我尝试将选择下拉列表字段转换为form_handler f.select,但出现此错误:

I'm a Rails beginner and I'm working on a preexisting Rails 2 project. In my application, I tried converting a select dropdown field to a form_handler f.select, but I'm getting this error:

    undefined method `location.masterlocation.name

这是我的尝试:

    <% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>

    <%= f.select(:start, options_from_collection_for_select(@itinerary.locations, "location.masterlocation.street_address, location.masterlocation.city, location.masterlocation.state, location.masterlocation.zip", "location.masterlocation.name"), :id => "startdrop")%>

这是原始的下拉字段:

    <select id="startdrop">
    <option value="">
    <% for location in @itinerary.locations %>
    <option value="<%= location.masterlocation.street_address %> <%= location.masterlocation.city %>, <%= location.masterlocation.state %>, <%= location.masterlocation.zip %>"><%= location.masterlocation.name %></option>
    <% end %>
    </select>

提前感谢您的帮助!

修改1

使用此代码,我已经走得更近了:

I've gotten much closer using this code:

    <%= f.select :start, options_for_select(@itinerary.locations.map{ |c| [c.masterlocation.name, c.masterlocation.street_address]}),{}, :id=>"startdrop", :name=>"startthere" %>     

问题是我想在值中包括城市,州和邮编,所有这些都用逗号分隔.有关如何执行此操作的任何想法?

The problem is that I want to include the city, state, and zip in the value, all separated by commas. Any ideas about how to do this?

    <%= f.select :start, options_for_select(@itinerary.locations.map{ |c| [c.masterlocation.inst_name, c.masterlocation.street_address AND , AND c.masterlocation.city AND , AND c.masterlocation.state AND, AND c.masterlocation.zip]}),{}, :id=>"startdrop", :name=>"startthere" %>     

这有效!

Maptry助手:

    module MaptryHelper

def options_for_select(locations)
  locations.map do |location|
    [location.masterlocation.name, location_string(location.masterlocation)]
  end
end

def location_string(masterlocation)
  "#{masterlocation.street_address}, #{masterlocation.city}, #{masterlocation.state}, #{masterlocation.zip}"
end

    end 

查看

    <%= f.select :start, options_for_select(@itinerary.locations),{}, :id=>"startdrop", :name=>"startthere" %>     

推荐答案

将以下内容放入帮助文件中

Place the following in a helper file

def select_options_for_locations(locations)
  locations.map do |location|
    [location_string(location.masterlocation), location.masterlocation.street_address]
  end
end

def location_string(masterlocation)
  "#{masterlocation.city}, #{masterlocation.state}, #{masterlocation.zip} #{masterlocation.name}"
end

然后在您的视图中,您可以使用以下

Then in your view, you can use the following

= f.select :start, select_options_for_locations(@itinerary.locations),  {}, :id => "startdrop"

这篇关于如何在Rails中将此下拉列表转换为f.select?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆