加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails:成分的未定义方法`map’

发布时间:2020-12-17 02:26:27 所属栏目:百科 来源:网络整理
导读:尝试入门的完整铁轨新手. 我有两个课程,成分和单元.有三个单位,磅,加仑和几十个,每个成分只有一个单位.我想我已正确设置了关联/路由. 在创建新配料时,我需要用户从这三个设置单位. 我用另一个问题来解决这个问题:Drop Down Box – Populated with data from
尝试入门的完整铁轨新手.

我有两个课程,成分和单元.有三个单位,磅,加仑和几十个,每个成分只有一个单位.我想我已正确设置了关联/路由.
在创建新配料时,我需要用户从这三个设置单位.
我用另一个问题来解决这个问题:Drop Down Box – Populated with data from another table in a form – Ruby on Rails

成分模型:

class Ingredient < ActiveRecord::Base
  belongs_to :unit
end

单位模型:

class Unit < ActiveRecord::Base
end

路线:

map.resources :ingredients,:has_many => :unit_conversions
  map.resources :units,:has_many => :ingredients

配料新控制器

def new
    @ingredient = Ingredient.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @ingredient }
    end
  end

成分新视图:

<h1>New ingredient</h1>

<% form_for(@ingredient) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :needsDefrosting %><br />
    <%= f.check_box :needsDefrosting %>
  </p>
  <p>
   <%= f.label :baseName %>
   <%= f.collection_select :unit_id,@ingredient,:id,:baseName,:prompt => "Select a Base Measurement"%>
   <br />
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back',ingredients_path %>

错误是

NoMethodError in Ingredients#new

Showing app/views/ingredients/new.html.erb where line #16 raised:

undefined method `map' for #<Ingredient:0x3dae1c0>

Extracted source (around line #16):

13:   </p>
14:   <p>
15:    <%= f.label :baseName %>
16:    <%= f.collection_select :unit_id,:prompt => "Select a Base Measurement"%>
17:    <br />
18:   </p>
19:   <p>

RAILS_ROOT: C:/Users/joan/dh

我只有三天左右的RoR,所以我怀疑它很简单!

解决方法

collection_select需要一系列选项,你传递的是一种成分.将@ingredient更改为Unit.all应该修复它.

%= f.collection_select :unit_id,Unit.all,:prompt => "Select a Base Measurement"%>

作为旁注,如果你只有3种类型的单位,那么创建一个常量而不是单位表就更有意义了.这将减少连接数并使整个模型更简单一些.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读