I'm not exactly sure what I'm missing, when I use the dev tools the form shows up properly in the html but the only value that is not being submitted it the one in the drop down.
I looked at my database and it's not submitting any value at all what am I doing wrong, I posted my code below.
******************the form********************
= 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'
******************Controller******************
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
You need to whitelist category_id
not category
.Change your menu_price_params
to
def menu_price_params
params.require(:menu_price).permit(:category_id, :price, :description, :serves)
end
In your view you have
= f.select :category_id
while in your MenuPricesController you have
params.require(:menu_price).permit(:category, ..)
Just change the :category
to :category_id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.