【问题标题】:How do I convert this dropdown to a f.select in Rails?如何将此下拉列表转换为 Rails 中的 f.select?
【发布时间】:2013-08-30 01:25:26
【问题描述】:

我是 Rails 初学者,我正在研究一个已有的 Rails 2 项目。在我的应用程序中,我尝试将选择下拉字段转换为 form_handler f.select,但出现此错误:

    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

我已经使用这段代码更接近了:

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

问题是我想在值中包含城市、州和邮政编码,所有这些都用逗号分隔。关于如何做到这一点的任何想法?

    <%= 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" %>     

这行得通!

地图助手:

    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" %>     

【问题讨论】:

    标签: ruby-on-rails forms rails-activerecord


    【解决方案1】:

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

    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
    

    那么在你看来,你可以使用下面的

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

    【讨论】:

    • 我收到此错误ActionView::TemplateError (undefined method name for #&lt;ActiveRecord::Associations::BelongsToAssociation:CODE&gt;)。作为背景,newsavedmap belongs_to :itinerary 和 itinerary has_many :locations 。表单本身位于 matry.html.erb 视图中。 maptry has_many :newsavedmaps 。不确定这是否重要,但我在 Rails 2 中
    • 虽然我使用了不同的解决方案,但它是基于 jvnill 的输入。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多