I keep getting an error when i try to create a new item for a database that displays ActionController::ParameterMissing in StocksController#create
param is missing or the value is empty: stock.
It says the source is at
private
def stock_params
params.require(:stock).permit(:product_name, :brand_name, :item_id, :upc_code, :color, :department, :size, :condition, :fabric_type, :shipping_weight, :sku, :asin, :quantity, :cost_price, :sell_price, :key_product_features, :product_description, :search_terms, :status, :listing_in_usa, :listing_in_canada, :listing_in_mexico)
end
Under my stockscontroller file on the terminal it says:
Started POST "/stocks" for 107.15.253.59 at 2017-05-22 16:38:14 +0000
Cannot render console from 107.15.253.59! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by StocksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"poJrp+PWBi7bxgeauJ37VNh/Shyk0q6iCNA+5Y4UBIZ/rf5dXYVBOgtPpVPUbfzjLsn9Kcz/Fn6kHKXHG0dUuQ==", "stocks"=>{"product_name"=>"", "brand_name"=>"", "item_id"=>"", "upc_code"=>"", "color"=>"", "department"=>"", "size"=>"", "condition"=>"", "fabric_type"=>"", "shipping_weight"=>"", "sku"=>"", "asin"=>"", "quantity"=>"", "cost_price"=>"", "sell_price"=>"", "key_product_feature"=>"", "product_description"=>"", "search_terms"=>"", "status"=>"", "listing_in_usa"=>"", "listing_in_canada"=>"", "listing_in_mexico"=>""}, "commit"=>"Save Stocks"}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: stock):
app/controllers/stocks_controller.rb:48:in `stock_params'
app/controllers/stocks_controller.rb:15:in `create'
I basically repeated a code that is similar to this that works that i already created, but i can't find why this is wrong. I have 2 types of databases, Vendors and Stocks, but the Stocks database isn't working.
This is the code for stockscontroller/concerns/controllers/apps
class StocksController < ApplicationController
def show
#stock = Stock.find(params[:id])
end
def index
#stocks = Stock.all
end
def new
#stock = Stock.new
end
def create
#stock = Stock.new(stock_params)
if #stock.save
redirect_to #stock
else
render 'new'
end
end
def edit
#stock = Stock.find(params[:id])
end
def update
#stock = Stock.find(params[:id])
if #stock.update(stock_params)
redirect_to #stock
else
render 'edit'
end
end
def destroy
#stock = Stock.find(params[:id])
#stock.destroy
redirect_to stock_path
end
end
private
def stock_params
params.require(:stock).permit(:product_name, :brand_name, :item_id, :upc_code, :color, :department, :size, :condition, :fabric_type, :shipping_weight, :sku, :asin, :quantity, :cost_price, :sell_price, :key_product_features, :product_description, :search_terms, :status, :listing_in_usa, :listing_in_canada, :listing_in_mexico)
end
code for new/stocks/views/apps
<body>
<div class = "head5">
<h1>New Inventory</h1>
<div class = "header5">
<h3> Apparel Inventory</h3>
</div>
</div>
</body>
<%= form_for :stocks, url: stocks_path do |f| %>
<p>
<%= f.label :product_name %><br>
<%= f.text_field :product_name %>
</p>
<p>
<%= f.label :brand_name %><br>
<%= f.text_field :brand_name %>
</p>
<p>
<%= f.label :item_id %><br>
<%= f.text_field :item_id %>
</p>
<p>
<%= f.label :upc_code %><br>
<%= f.text_field :upc_code %>
</p>
<p>
<%= f.label :color %><br>
<%= f.text_field :color %>
</p>
<p>
<%= f.label :department %><br>
<%= f.text_field :department %>
</p>
<p>
<%= f.label :size %><br>
<%= f.text_field :size %>
</p>
<p>
<%= f.label :condition %><br>
<%= f.text_field :condition %>
</p>
<p>
<%= f.label :fabric_type %><br>
<%= f.text_field :fabric_type %>
</p>
<p>
<%= f.label :shipping_weight %><br>
<%= f.text_field :shipping_weight %>
</p>
<p>
<%= f.label :sku %><br>
<%= f.text_field :sku %>
</p>
<p>
<%= f.label :asin %><br>
<%= f.text_field :asin %>
</p>
<p>
<%= f.label :quantity %><br>
<%= f.text_field :quantity %>
</p>
<p>
<%= f.label :cost_price %><br>
<%= f.text_field :cost_price %>
</p>
<p>
<%= f.label :sell_price %><br>
<%= f.text_field :sell_price %>
</p>
<p>
<%= f.label :key_product_feature %><br>
<%= f.text_field :key_product_feature %>
</p>
<p>
<%= f.label :product_description %><br>
<%= f.text_field :product_description %>
</p>
<p>
<%= f.label :search_terms %><br>
<%= f.text_field :search_terms %>
</p>
<p>
<%= f.label :status %><br>
<%= f.text_field :status %>
</p>
<p>
<%= f.label :listing_in_usa %><br>
<%= f.text_field :listing_in_usa %>
</p>
<p>
<%= f.label :listing_in_canada %><br>
<%= f.text_field :listing_in_canada %>
</p>
<p>
<%= f.label :listing_in_mexico %><br>
<%= f.text_field :listing_in_mexico %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', stocks_path %>
Code for the model
class Stock < ApplicationRecord
end
my routes/locals/config/app file
Rails.application.routes.draw do
get 'welcome/index'
resources :vendors
resources :stocks
root 'welcome#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
my schema/migrate/db/config/apps
ActiveRecord::Schema.define(version: 20170522154349) do
create_table "stocks", force: :cascade do |t|
t.string "product_name"
t.string "brand_name"
t.integer "item_id"
t.integer "upc_code"
t.string "color"
t.string "size"
t.string "department"
t.string "condition"
t.string "fabric_type"
t.string "shipping_weight"
t.string "sku"
t.string "asin"
t.integer "quantity"
t.string "cost_price"
t.string "sell_price"
t.string "key_product_feature"
t.text "product_description"
t.text "search_terms"
t.string "status"
t.string "listing_in_usa"
t.string "listing_in_canada"
t.string "listing_in_mexico"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "vendors", force: :cascade do |t|
t.string "company"
t.string "contact_name"
t.string "phone"
t.string "email"
t.string "moq"
t.string "cost_per_item"
t.string "payment_method"
t.string "terms"
t.string "turnover"
t.string "returns"
t.text "notes"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
The new error says:
ActiveRecord::RecordNotFound in StocksController#show
Couldn't find Stock with 'id'=5
At the source on app/controllers/stocks_controller.rb:
class StocksController < ApplicationController
def show
#stock = Stock.find(params[:id])
end
def index
With the terminal saying:
ActiveRecord::RecordNotFound (Couldn't find Stock with 'id'=5):
app/controllers/stocks_controller.rb:3:in `show'
You're not returning a :stock key in your params.
Change
<%= form_for :stocks, url: stocks_path do |f| %>
Into
<%= form_for :stock, url: stocks_path do |f| %>
This seems to be a plural vs. sigular issue.
Your code is requiring a stock param, but it's getting stocks.
Try changing this line:
<%= form_for :stocks, url: stocks_path do |f| %>
to
<%= form_for :stock, url: stocks_path do |f| %>
Related
So I have this challenge that I'm having trouble figuring out how to declare the controllers so it works properly, I've been having a diverse kind of errors but my major issue is that, the way it is now I can't save a new recipe or update the 'Tipo de Receita' which is associated with the recipe_type model used on a drop down, bear in mind that recipe_type.name will be populated beforehand
that's the edit.hmtl.erb that do the editing
<%= form_for #recipe do |f| %>
<%= f.label :title, 'Título' %>
<%= f.text_field :title %>
<%= f.label :recipe_type, 'Tipo da Receita' %>
<%= collection_select(:recipe, :recipe_type_id, RecipeType.all, :id, :name) %>
<%= f.label :cuisine, 'Cozinha' %>
<%= f.text_field :cuisine %>
<%= f.label :difficulty, 'Dificuldade' %>
<%= f.text_field :difficulty %>
<%= f.label :cook_time, 'Tempo de Preparo' %>
<%= f.number_field :cook_time %>
<%= f.label :ingredients, 'Ingredientes' %>
<%= f.text_area :ingredients %>
<%= f.label :cook_method, 'Como Preparar' %>
<%= f.text_area :cook_method %>
<%= f.submit 'Enviar' %>
<% end %>
similarly that's the new.hmtl.erb that registers the new recipes
<%= form_for #recipe do |f| %>
<%= f.label :title, 'Título' %>
<%= f.text_field :title %>
<%= f.label :recipe_type, 'Tipo da Receita' %>
<%= collection_select(:recipe, :recipe_type_id, RecipeType.all, :id, :name) %>
<%= f.label :cuisine, 'Cozinha' %>
<%= f.text_field :cuisine %>
<%= f.label :difficulty, 'Dificuldade' %>
<%= f.text_field :difficulty %>
<%= f.label :cook_time, 'Tempo de Preparo' %>
<%= f.number_field :cook_time %>
<%= f.label :ingredients, 'Ingredientes' %>
<%= f.text_area :ingredients %>
<%= f.label :cook_method, 'Como Preparar' %>
<%= f.text_area :cook_method %>
<%= f.submit 'Enviar' %>
<% end %>
models/recipe_type which is the one used in the drop down
class RecipeType < ApplicationRecord
has_many :recipes
validates :name, presence: true
end
models/recipe which receives the recipe_type model
class Recipe < ApplicationRecord
belongs_to :recipe_type
validates :title, :cuisine, :difficulty, :cook_time,
:ingredients, :cook_method, presence: true
def cook_time_min
"#{cook_time} minutos"
end
end
recipes.controller.rb
class RecipesController < ApplicationController
def index
#recipes = Recipe.all
end
def show
#recipe = Recipe.find(params[:id])
end
def new
#recipe = Recipe.new
#recipe_type = RecipeType.all
end
def create
#recipe_type = RecipeType.all
#recipe = Recipe.new(recipe_params)
if #recipe.save
redirect_to #recipe
else
flash[:alert] = 'Você deve informar todos os dados da receita'
render :new
end
end
def edit
#recipe_type = RecipeType.all
#recipe = Recipe.find(params[:id])
#recipe_type = RecipeType.all
end
def update
#recipe = Recipe.find(params[:id])
if #recipe.update(recipe_params)
redirect_to #recipe
else
flash[:alert] = 'Você deve informar todos os dados da receita'
render :edit
end
end
private
def recipe_params
params.require(:recipe).permit(:title, :cuisine, :difficulty,
:cook_time, :ingredients, :cook_method, :name)
end
end
and that's the schema
ActiveRecord::Schema.define(version: 2020_03_26_013134) do
create_table "recipe_types", force: :cascade do |t|
t.string "name"
t.integer "recipe_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["recipe_id"], name: "index_recipe_types_on_recipe_id"
end
create_table "recipes", force: :cascade do |t|
t.string "title"
t.string "cuisine"
t.string "difficulty"
t.integer "cook_time"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "ingredients"
t.text "cook_method"
t.integer "recipe_type_id"
t.index ["recipe_type_id"], name: "index_recipes_on_recipe_type_id"
end
end
One problem is that your not building the input from the form builder. You want to use:
<%= f.label :recipe_type_id, 'Tipo da Receita' # key has to match input for accessibility %>
<%= f.collection_select(:recipe_type_id, RecipeType.all, :id, :name) %>
collection_select is a bare bones input helper that will just create a select tag that's not bound to a form builder and thus not to your model instance. So if the record is invalid or you are updating an existing record it won't have anything selected.
f.collection_select is a method on the form builder and will properly set the recipe_type_id value from the model instance wrapped by the form.
You also need to permit the param in your whitelist:
def recipe_params
params.require(:recipe)
.permit(
:title, :cuisine, :difficulty,
:cook_time, :ingredients, :cook_method,
:name, :recipe_type_id
)
end
you need to add recipe_type_id in recipe_params:
def recipe_params
params.require(:recipe).permit(:recipe_type_id ...)
end
Rails noob here, how do I get a Rails form to save a boolean checkbox to postgres? I've tried dozens of things I've found online and none of it is working. No matter what I do, the entry in the Postgres DB is "Null". Here is my
Model(schema):
ActiveRecord::Schema.define(version: 20171227200151) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "notes", force: :cascade do |t|
t.string "note_title"
t.string "note_text"
t.boolean "note_scratch", default: false, null: false
t.boolean "note_info"
t.boolean "note_reminder"
t.boolean "note_task"
t.string "note_tags"
t.date "note_remind_date"
t.time "note_remind_time"
t.integer "note_archived"
t.date "note_archived_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
view:
<%= form_with scope: :note, url: notes_path, local: true do |f| %>
<p>
<%= f.label :note_title %><br>
<%= f.text_field :note_title %>
</p>
<p>
<%= f.label :note_text %><br>
<%= f.text_area :note_text %>
</p>
<p>
<%= f.label :notes_scratch, "Scratch" %>
<%= f.check_box :notes_scratch %>
<%= f.label :notes_info, "Info" %>
<%= f.check_box :notes_info %>
<%= f.label :notes_reminder, "Reminder" %>
<%= f.check_box :notes_reminder %>
<%= f.label :notes_task, "Task" %>
<%= f.check_box :notes_task %>
</p>
<p>
<%= f.label :note_tags %><br>
<%= f.text_field :note_tags %>
</p>
<p>
<%= f.label :note_remind_date %><br>
<%= f.date_field :note_remind_date %>
</p>
<p>
<%= f.label :note_remind_time %><br>
<%= f.time_field :note_remind_time %>
</p>
<%= f.submit %>
<% end %>
Controller:
class NotesController < ApplicationController
def new
end
def create
# for testing
# render plain: params[:note].inspect
#note = Note.new(note_params)
#note.save
redirect_to #note
end
private
def note_params
params.require(:note).permit(:note_title, :note_text, :note_scratch, :note_info, :note_reminder, :note_task, :note_tags, :note_remind_date, :note_remind_time, :note_archived, :note_archived_date)
end
end
If I just render the form results, it will show checked boxes as 1s, but it always comes up Null in the database, even if I set the datatype to Integer (hoping it would store the 1). All of the other fields record just fine. It's only the checkboxes that don't work.
I appreciate any assistance you can lend!
-Brick
Review your note_params (and schema.rb):
def note_params
params.require(:note).permit(:note_title, :note_text, :note_scratch, :note_info, :note_reminder, :note_task, :note_tags, :note_remind_date, :note_remind_time, :note_archived, :note_archived_date)
end
In your view: notes_* needs to be singular as your schema is note_*.
I've looked stackoverflow questions regarding to this question(also followed all the instructions given). I am trying to add an new item but I am encountering this error..
Question: Why I am getting this error? What is it?
Controller
class ItemsController < ApplicationController
def index
end
def addItem
end
def create
#item = Item.new(post_params)
#item.save
redirect_to #item
end
def show
#item = Item.find(params[:id])
end
private
def post_params
params.require(:item).permit(:item, :description, :price)
end
end
Add Item view
<%= form_for :post, url: items_create_path do |f| %>
<p>
<%= f.label :Item %><br>
<%= f.text_field :item %>
</p>
<p>
<%= f.label :Description %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.label :Price %><br>
<%= f.text_field :price %>
</p>
<p>
<%= f.submit :Submit %>
</p>
<% end %>
<%= link_to 'BACK', items_path %>
Routes
Rails.application.routes.draw do
# URL Controller#method
get '/items/addItem' => 'items#addItem'
get '/items/' => 'items#index'
post '/items/create' => 'items#create'
end
My Table
ActiveRecord::Schema.define(version: 20170323165252) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "items", force: :cascade do |t|
t.string "name"
t.text "description"
t.integer "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
Your items table does not have item field. You are using the name item for name field. So replace item with name and it should work.
Controller
params.require(:item).permit(:name, :description, :price)
Add Item view
<p>
<%= f.label :Item %><br>
<%= f.text_field :name %>
</p>
I have two models: Entreprise & Prospect
create_table "entreprises", force: :cascade do |t|
t.string "name"
t.text "adresse"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "siret"
t.string "numtva"
t.string "tel"
t.integer "prospect_id"
end
create_table "prospects", force: :cascade do |t|
t.string "full_name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "entreprise_id"
end
Prospects belongs_to entreprises, and Entreprise has_many Prospects.
-
My issue :
I have two buttons : Create a new entreprise or reate a new prospect.
I have two way to create a new prospect :
Under my entreprise because an entreprise has_many prospects
Directly from navbar with "create prospect" button
So I have two forms :
If we click on "create prospect" (create method in my controller):
<%= form_for :prospects, url: prospects_path do |f| %>
<p>
<%= f.label :entreprises %><br><br>
<%= f.collection_select :entreprise_id, Entreprise.order(:name),:id,:name, include_blank: true %>
</p><br>
<p>
<%= f.label :full_name %> <br>
<%= f.text_field :full_name %><br>
</p><br>
<p>
<%= f.label :email %> <br>
<%= f.text_field :email %><br>
</p><br>
<p>
<%= f.submit %>
</p>
<% end %>
If we create prospect under entreprise ("directcreate" method in my controller) :
<%= form_for([#entreprise, #entreprise.prospects.build]) do |f| %>
<p>
<%= f.label :full_name %> <br>
<%= f.text_field :full_name %><br>
</p><br>
<p>
<%= f.label :email %> <br>
<%= f.text_field :email %><br>
</p><br>
<p>
<%= f.submit %>
</p>
<% end %>
My Controller for Prospects :
class ProspectsController < ApplicationController
def index
#prospects = Prospect.all.limit(15).order('created_at DESC')
end
def new
#prospect = Prospect.new
end
def directcreate
#prospect = Prospect.new(entreprise_params)
if #prospect.save
redirect_to #prospect
else
render 'new'
end
end
def create
#entreprise = Entreprise.find(params[:entreprise_id])
#prospect = #entreprise.prospects.create(params[:prospect].permit(:full_name, :email))
redirect_to entreprise_path(#entreprise)
end
def destroy
#entreprise = Entreprise.find(params[:entreprise_id])
#prospect = #entreprise.prospects.find(params[:id])
#prospect.destroy
redirect_to entreprise_path(#entreprise)
end
end
My problème is that the "create" method in my controller is the only one who works.. and not "directcreate" method.
--
QUESTION : How can I do to choose the controller I want according to the form ?
My question is probably not well ask.
FYI I try this in my new.html.erb :
<%= render 'formdirect', :controller => "directcreate" %>
but it doesn't work.
You can try something like this. You just have to explicitly choose your controller action.
<%= form_for([#entreprise, #entreprise.prospects.build], :url => { :action => : directcreate }) do |f| %>
<p>
<%= f.label :full_name %> <br>
<%= f.text_field :full_name %><br>
</p><br>
<p>
<%= f.label :email %> <br>
<%= f.text_field :email %><br>
</p><br>
<p>
<%= f.submit %>
</p>
<% end %>
I noob in Rails. I use rails 4.2.4. I have a problem with my contact form. I have an error: First argument in form cannot contain nil or be empty on the 3 line of this code (contact_form.html.erb):
<div class="all-banners-width">
<figure class="green-background">
<%= form_for #contact_form do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :phone %><br>
<%= f.text_field :phone %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_field :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</figure>
</div>
My contact_form_controller.rb is:
class ContactFormController < ApplicationController
def contact_form
#contact_form = ContactForm.new
end
def create
#contact_form = ContactForm.new(contact_form_params)
#contact_form.save
redirect_to root_path, notice: "Saved"
end
private
def contact_form_params
params.require(:contact_form).permit(:name, :phone, :email, :text)
end
end
My DB migration file is:
class CreateContactForms < ActiveRecord::Migration
def change
create_table :contact_forms do |t|
t.string :name
t.string :phone
t.string :email
t.string :text
t.timestamps null: false
end
end
end
If I change 3rd line from contact_form.html.erb to the next:
<%= form_for :contact_form do |f| %>
then I have not an error, but anyway contact form does not work and there are not any errors I can see.
Can somebody help me? I have not idea, where is problem.
You miss action new in controller:
def new
#contact_form = ContactForm.new
end
your template is rendered with #contact_form = nil.