2016-06-07 49 views
1

我对编码一般都很陌生。我试图通过创建一个网络应用程序来支持我的技能,该应用程序允许人们对特定菜肴排列他们最喜爱的餐馆(例如汉堡包的顶级餐厅)。菜品中的NoMethodError#new

我目前正在致力于连接表,链接餐厅和菜肴。我正在尝试添加select_tags以从菜盘餐厅表中选择一个菜。我得到一个NoMethodError。希望对我的错误有所了解。谢谢!

错误:

NoMethodError in Dishings#new Showing /Users/frederick7/code/Appdev/pear_review/app/views/dishings/new.html.erb

其中行#27提出:

undefined method `map' for #

提取的源(围绕线#27):

</label> 

<%= select_tag(:restaurant_id, options_from_collection_for_select(@dishing, 'id', 'restaurant_id', @dishing.id)) %> 
</div> 

<!-- Label and input for dish_id --> 

以下是在视图/ dishings相关的代码/new.html.erb:

<!-- Label and input for restaurant_id --> 
    <div class="form-group"> 
    <label for="restaurant_id" class="control-label"> 
     Restaurant 
    </label> 
    <%= select_tag(:restaurant_id, options_from_collection_for_select(@dishing, 'id', 'restaurant_id', @dishing.id)) %> 
    </div> 

    <!-- Label and input for dish_id --> 
    <div class="form-group"> 
    <label for="dish_id" class="control-label"> 
     Dish 
    </label> 
    <%= select_tag(:dish_id, options_from_collection_for_select(@dishing, 'id', 'dish_id', @dishing.id)) %> 
    </div> 

这可能是相关的代码dishings_controller.rb:

def new 
    @dishing = Dishing.new 
end 

def create 
    @dishing = Dishing.new 
    @dishing.restaurant_id = params[:restaurant_id] 
    @dishing.dish_id = params[:dish_id] 

    if @dishing.save 
    redirect_to "/dishings", :notice => "Dishing created successfully." 
    else 
    render 'new' 
    end 
end 
+0

'.map'是一个集合,'options_from_collection_for_select'需要一个集合中,并呼吁'它.map',但'@ dishing'不一个集合 – lusketeer

回答

0

当我看到你正在使用@dishing这不是一个数组,options_from_collection_for_select需要数组或集合。

还有一件事我注意到你正在为新动作创建视图,并且可能在控制器中使用@dishing = Dishing.new。

,如果你是获取restaurant_id和dish_id不是不可能

+0

这个命令是否创建了碟形数组?:'def create @dishing = Dishing.new' –

+0

这个命令不会创建任何数组。这只会创建一个Dishing类的新对象。 –

相关问题