Ruby on rails 在未提交的表单中选择值

Ruby on rails 在未提交的表单中选择值,ruby-on-rails,forms,html-select,form-submit,Ruby On Rails,Forms,Html Select,Form Submit,我不确定我遗漏了什么,当我使用开发工具时,表单会正确地显示在html中,但唯一没有提交的值是下拉列表中的值 我查看了我的数据库,它根本没有提交任何值。我做错了什么,我在下面发布了我的代码 ******************形式******************** = form_for @menu_price do |f| - if @menu_price.errors.any? #error_explanation h2 = "#{pluralize(@menu_p

我不确定我遗漏了什么,当我使用开发工具时,表单会正确地显示在html中,但唯一没有提交的值是下拉列表中的值

我查看了我的数据库,它根本没有提交任何值。我做错了什么,我在下面发布了我的代码

******************形式********************

= form_for @menu_price do |f|
  - if @menu_price.errors.any?
    #error_explanation
      h2 = "#{pluralize(@menu_price.errors.count, "error")} prohibited this menu_price from being saved:"
      ul
        - @menu_price.errors.full_messages.each do |message|
          li = message

  .form-group
    = f.label Category
    = f.select :category_id,  options_for_select( @categories_options),{prompt: "Select a category"}
  .form-group
    = f.label :price
    = f.number_field :price
  .form-group
    = f.label :description
    = f.text_field :description
  .form-group
    = f.label :serves
    = f.text_field :serves

  = f.submit
  = link_to 'Back', menu_prices_path, class:'button'
class MenuPricesController < ApplicationController
  before_action :set_menu_price, only: [:show, :edit, :update, :destroy]

  def index
    @menu_prices = MenuPrice.all
  end


  def show
    @categories = Category.all
  end

  def new
    @menu_price = MenuPrice.new
    @categories_options = Category.all.map{|u| [ u.name, u.id ] }
  end

  def edit
    @categories_options = Category.all.map{|u| [ u.name, u.id ] }
  end


  def create
    @menu_price = MenuPrice.new(menu_price_params)

    respond_to do |format|
      if @menu_price.save
        format.html { redirect_to @menu_price, notice: 'Menu price was successfully created.' }
        format.json { render :show, status: :created, location: @menu_price }
      else
        format.html { render :new }
        format.json { render json: @menu_price.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @menu_price.update(menu_price_params)
        format.html { redirect_to @menu_price, notice: 'Menu price was successfully updated.' }
        format.json { render :show, status: :ok, location: @menu_price }
      else
        format.html { render :edit }
        format.json { render json: @menu_price.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @menu_price.destroy
    respond_to do |format|
      format.html { redirect_to menu_prices_url, notice: 'Menu price was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    def set_menu_price
      @menu_price = MenuPrice.find(params[:id])
    end

    def menu_price_params
      params.require(:menu_price).permit(:category, :price, :description, :serves)
    end
end
******************控制器******************

= form_for @menu_price do |f|
  - if @menu_price.errors.any?
    #error_explanation
      h2 = "#{pluralize(@menu_price.errors.count, "error")} prohibited this menu_price from being saved:"
      ul
        - @menu_price.errors.full_messages.each do |message|
          li = message

  .form-group
    = f.label Category
    = f.select :category_id,  options_for_select( @categories_options),{prompt: "Select a category"}
  .form-group
    = f.label :price
    = f.number_field :price
  .form-group
    = f.label :description
    = f.text_field :description
  .form-group
    = f.label :serves
    = f.text_field :serves

  = f.submit
  = link_to 'Back', menu_prices_path, class:'button'
class MenuPricesController < ApplicationController
  before_action :set_menu_price, only: [:show, :edit, :update, :destroy]

  def index
    @menu_prices = MenuPrice.all
  end


  def show
    @categories = Category.all
  end

  def new
    @menu_price = MenuPrice.new
    @categories_options = Category.all.map{|u| [ u.name, u.id ] }
  end

  def edit
    @categories_options = Category.all.map{|u| [ u.name, u.id ] }
  end


  def create
    @menu_price = MenuPrice.new(menu_price_params)

    respond_to do |format|
      if @menu_price.save
        format.html { redirect_to @menu_price, notice: 'Menu price was successfully created.' }
        format.json { render :show, status: :created, location: @menu_price }
      else
        format.html { render :new }
        format.json { render json: @menu_price.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @menu_price.update(menu_price_params)
        format.html { redirect_to @menu_price, notice: 'Menu price was successfully updated.' }
        format.json { render :show, status: :ok, location: @menu_price }
      else
        format.html { render :edit }
        format.json { render json: @menu_price.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @menu_price.destroy
    respond_to do |format|
      format.html { redirect_to menu_prices_url, notice: 'Menu price was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    def set_menu_price
      @menu_price = MenuPrice.find(params[:id])
    end

    def menu_price_params
      params.require(:menu_price).permit(:category, :price, :description, :serves)
    end
end
class菜单控件
在您的视图中

  = f.select :category_id
而在你的菜单控件中

params.require(:menu_price).permit(:category, ..)

只需将视图中的
:category
更改为
:category\u id

  = f.select :category_id
而在你的菜单控件中

params.require(:menu_price).permit(:category, ..)

只需将视图中的
:category
更改为
:category\u id

  = f.select :category_id
而在你的菜单控件中

params.require(:menu_price).permit(:category, ..)

只需将视图中的
:category
更改为
:category\u id

  = f.select :category_id
而在你的菜单控件中

params.require(:menu_price).permit(:category, ..)

只需将
:category
更改为
:category\u id

您需要将
category\u id
列为白名单,而不是
category
。将
菜单价格参数更改为

def menu_price_params
   params.require(:menu_price).permit(:category_id, :price, :description, :serves)
end

您需要将
类别\u id
而不是
类别
列入白名单。将
菜单\u价格\u参数
更改为

def menu_price_params
   params.require(:menu_price).permit(:category_id, :price, :description, :serves)
end

您需要将
类别\u id
而不是
类别
列入白名单。将
菜单\u价格\u参数
更改为

def menu_price_params
   params.require(:menu_price).permit(:category_id, :price, :description, :serves)
end

您需要将
类别\u id
而不是
类别
列入白名单。将
菜单\u价格\u参数
更改为

def menu_price_params
   params.require(:menu_price).permit(:category_id, :price, :description, :serves)
end

您是否收到任何错误或检查了控制台日志?
批量分配
回滚
问题可能存在。如果可能的话,您可以将控制台日志粘贴到这里吗?如何检查控制台日志中的错误?我提交表单时,它没有抛出任何异常。您是否收到任何错误或检查控制台日志?
批量分配
回滚
问题可能存在。如果可能的话,您可以将控制台日志粘贴到这里吗?如何检查控制台日志中的错误?我提交表单时,它没有抛出任何异常。您是否收到任何错误或检查控制台日志?
批量分配
回滚
问题可能存在。如果可能的话,您可以将控制台日志粘贴到这里吗?如何检查控制台日志中的错误?我提交表单时,它没有抛出任何异常。您是否收到任何错误或检查控制台日志?
批量分配
回滚
问题可能存在。如果可能的话,您可以将控制台日志粘贴到这里吗?如何检查控制台日志中的错误?当我提交表单时,它没有抛出任何异常。预期类别(#56521180),预期submitCategory(#56521180)时得到字符串(#19499820),预期submitCategory(#56521180)时得到字符串(#19499820),预期submitCategory(#56521180)时得到字符串(#19499820),预期submitCategory(#56521180)时得到字符串(#19499820)当你提交时,你得到了最好的答案,因为你首先得到了解决方案。你得到了最好的答案,因为你首先得到了解决方案。你得到了最好的答案,因为你首先得到了解决方案