2016-08-17 47 views
1

我有这个问题第一个元素不能为空

.container.weekdays 
    .row 
    .col-sm-3 
     .panel-default data-day="#{@weekdays[0]}" 
     .h3 
     |Today's menu.    
     [email protected][0] 
     br 
     =link_to "Today's menu", admin_menu_path("#{@weekdays[0]}"), class: 'today', remote: true 
     hr 
     hr 
    fieldset 
     legend Update menu here 
     =form_for [:admin, @menu] ,method: :patch, remote: true do |f| 
     p 
      = f.select(:type, options_for_select(["", :First, :Second, :Drink], include_blank: true)) 
     p 
      =f.label 'Name' 
      =f.text_field :name 
     p 
      =f.label 'Price' 
      =f.number_field :price    
     = f.submit 

和菜单控制,其中显示行动工作的权利

class Admin::MenusController < Admin::BaseController 
    before_action :set_menu , only:[:update] 
    def show 
    @menu = Menu.where(day: params[:id]) 
    end 

    def update 
    @menu = Menu.where(day: DateTime.now.strftime("%A"))  
    @item = menu_params.type.constantize.new(name: menu_parms.name, price: params[:menu][:price], menu: @menu) 
    @item.save 
    end 

    private 
    def menu_params 
    params.require(:menu).permit(:type, :name, :price) 
    end 
end 

我recieving第一个参数cn't在形式上更新,需要一些帮助。我做这已了解代码是可怕的,但我仍然是一个newcommer应该先工作比我会尽力refact

为了更好地理解这里的问题是菜单模式

class Menu < ApplicationRecord 
    has_many :dishes 

    delegate :firstmeals, :secondmeals, :drinks, to: :dishes 

    validates :day, presence: true 
    validates :day, uniqueness: true 

    def self.get_menu day 
    Menu.where(day: day).first 
    end 

    private 
    def to_param 
    day 
    end 

所以我实际上并不需要更新菜单,我只需要创建一个菜。但是,同样的problemm仍然存在

fieldset 
     legend Update menu here 
     =form_for [:admin, @dish], method: :patch, remote: true do |f| 
     p 
      = f.select(:type, options_for_select(["", :First, :Second, :Drink], include_blank: true)) 
     p 
      =f.label 'Name' 
      =f.text_field :name 
     p 
      =f.label 'Price' 
      =f.number_field :price    
     = f.submit 
+1

你的'新'行动在哪里?在这个动作中你需要做'@menu = Menu.new'。目前'@ menu'是'nil'。 –

+0

其实我不需要创建菜单,它们只创建一次(7菜单),然后只更新 –

+0

那么表单是要更新的? –

回答

2

include_blank应该是options_for_select

= f.select(:type, options_for_select([:First, :Second, :Drink]), { include_blank: true }) 

外部。如果你逝去的include_blank选项你并不需要添加空字符串手动

文档select

+0

错误OP说不是来自“选择”列表。 –

相关问题