getting "ActionController::ParameterMissing in HomepageController#newbands" - ruby-on-rails

I am trying to print out a form using the simple_form gem but I am getting this error:
ActionController::ParameterMissing in HomepageController#newbands
param is missing or the value is empty: band
homepage_controller.rb
class HomepageController < ApplicationController
def index
end
def bands
end
def newbands
#band = Band.new
#band = Band.create(band_params)
if #band.save
redirect_to "/newbands"
else
render "/"
end
end
private
def band_params
params.require(:band).permit(:name, :country, :members, :genre)
end
end
newbands.html.erb
<%= simple_form_for #band do |b| %>
<%= b.input :name %>
<%= b.input :country %>
<%= b.input :members %>
<%= b.input :genre %>
<%= b.button :submit %>
<% end %>
schema.rb
ActiveRecord::Schema[7.0].define(version: 2022_03_14_133839) do
create_table "bands", force: :cascade do |t|
t.string "name"
t.string "country"
t.integer "members"
t.string "genre"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end

Related

How to fixed the value in input field and also set default zero

now i doing a form that can add product to outlet.This form is create at the outletproduct page
So when i click to a product info page have a button (add product to outlet) then go to the form which located at outletproduct page. Now i need to set these things
1.I have create the form out but at the product name field there i need to display product name i choose to be fixed and cannot be change.(Example, click on the product fish then click button then the product name should be fish.)
2.The selling_price and last_cost will need to show the product price and cost in the input field there but this can be change(Example, at product page price is $2 and cost is $1, then here the input field will be selling_price $2 and last_cost $1.)
i have try do but it won't work.
I have update some pic about UI.
OutletProduct Controller
class OutletProductsController < ApplicationController
def new
#outlet_product = OutletProduct.new
#product = Product.all
#outlet = Outlet.all
#category = Category.all
end
def index
end
def show
end
def create
#outlet_product = OutletProduct.new(outlet_product_params)
#category_id = Category.all
#outlet_id = Outlet.all
#product_id = Product.all
if #outlet_product.save
flash[:success] = "Succesful create!"
redirect_to #outlet_product
else
render 'new'
end
end
def edit
end
def outlet_product_params
params.require(:outlet_product).permit(:product_id, :outlet_id, :quantity,
:selling_price ,:last_cost)
end
end
new.html.erb
<h1>Add product to outlet</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: #outlet_product, local: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :product_name %>
<%= f.text_field :#product.name ,class: "form-select" %>
<%= f.label :quantity %>
<%= f.number_field :quantity%>
<%= f.label :selling_price %>
<%= f.number_field :selling_price, #product.price , class: 'form-control' %>
<%= f.label :last_cost %>
<%= f.number_field :last_cost,#product.cost, class: 'form-control' %>
<%= f.label :outlet_id %>
<%= f.select(:outlet_id, Outlet.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
OutletProduct migration in schema
create_table "outlet_products", force: :cascade do |t|
t.integer "outlet_id"
t.integer "product_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.decimal "selling_price"
t.decimal "last_cost"
t.decimal "quantity"
end
product migration in schema
create_table "products", force: :cascade do |t|
t.string "name"
t.integer "quantity"
t.integer "price"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.decimal "cost"
end
Product page
Product info page]
form page
Hi you can define all the default values in view like below
<%= f.label :selling_price %>
<%= f.number_field :selling_price, 0 , class: 'form-control' %>
or if you wanted to keep it by default in the database, then you can make changes in migration files as below:
create_table "products", force: :cascade do |t|
t.string "name", default: 'fish'
t.integer "quantity", default: 0
t.integer "price"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.decimal "cost"
end
I hope this is what you are trying to achieve.

Rails Not able to save data, association

I'm doing a parking permit website. The problem I met is that I'm not able to save my data to the PERMIT database which associated with the USER database. The problem i think is I didn't bring the user to the permit(Maybe i missed something). I found out the error when I trying to save from Permit.errors.full_messages is ["User must exist"]. Any help is appreciated, Thank you!
Schema.rb
ActiveRecord::Schema.define(version: 20160920143651) do
create_table "permits", force: :cascade do |t|
t.string "vehicle_type"
t.string "name"
t.string "studentid"
t.string "department"
t.string "carplate"
t.string "duration"
t.date "permitstart"
t.date "permitend"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_permits_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.integer "user_type"
end
end
Create_permit.rb
class CreatePermits < ActiveRecord::Migration[5.0]
def change
create_table :permits do |t|
t.string :vehicle_type
t.string :name
t.string :studentid
t.string :department
t.string :carplate
t.string :duration
t.date :permitstart
t.date :permitend
t.references :user, foreign_key: true
t.timestamps
end
add_index :permits, :user_id
end
end
Permit_controller
class PermitsController < ApplicationController
before_action :set_permit, only: [:show, :destroy]
def index
#permits = Permit.all
end
def new
#permits = Permit.new
end
def create
#permits = Permit.new(permit_params)
if #permits.save
redirect_to #permits
else
redirect_to contact_path
end
end
def destroy
Permit.destroy_all(user_id: 1)
respond_to do |format|
format.html { redirect_to users_url, notice: 'Permit was successfully canceled.' }
format.json { head :no_content }
end
end
def show
#permits = Permit.find(params[:id])
end
def update
respond_to do |format|
if #permits.update(user_params)
format.html { redirect_to #user, notice: 'Permit was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_permit
#permits = Permit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def permit_params
params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate, :duration, :permitstart, :permitend)
end
end
user.rb
class User < ApplicationRecord
has_many :permits
has_secure_password
end
Permit.rb
class Permit < ApplicationRecord
belongs_to :user
end
permit/new.html.erb
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#permits) do |f| %>
<%= f.label :"Vehicle" %>
<%= f.text_field :vehicle_type, class: 'form-control' %>
<%= f.label :"License Plate" %>
<%= f.text_field :carplate, class: 'form-control' %>
<%= f.label :"Student ID" %>
<%= f.text_field :studentid, class: 'form-control' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :"Department of applicant" %>
<%= f.text_field :department, class: 'form-control' %>
<%= f.label :permit_start %>
<%= f.date_select :permitstart, class: 'form-control' %>
<%= f.label :permit_end %>
<%= f.date_select :permitend, class: 'form-control' %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
I guess you are using Rails 5. The problem is you have belongs_to association on permit that is belongs to user but while creating permit you are not associating any user with it and in Rails 5 it is mandatory to assign data to belongs_to association i.e you can not save permit when it don't have user_id so try to assign a user_id to permit. refer this for change to rails 5 belongs_to association
Where exactly is your error ? If in update action, you have to change your before_action. You must add there :update action.
before_action :set_permit, only: [:show, :destroy, :update]

Can't save my data into sqlite database

I am trying to implement a Parking Permit application page using ROR. I couldn't get my data saved into the database. The permit database is associated with the user also. The program won't save the data and execute the else statement. There is no error generated, i think i have missed something but i don't know the exact problem. Any help is appreciated!
Permit_controller.rb
class PermitsController < ApplicationController
before_action :set_permit, only: [:show, :destroy]
def index
#permits = Permit.all
end
def new
#permits = Permit.new
end
def create
#permits = Permit.new(permit_params)
if #permits.save
redirect_to root_path
else
redirect_to contact_path
end
end
def destroy
end
def show
#permits = Permit.find(params[:id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_permit
#permits = Permit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def permit_params
params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate,:permitstart, :permitend)
end
end
Permit.rb
class Permit < ApplicationRecord
belongs_to :user
end
Create_permit.rb
class CreatePermits < ActiveRecord::Migration[5.0]
def change
create_table :permits do |t|
t.string :vehicle_type
t.string :name
t.string :studentid
t.string :department
t.string :carplate
t.date :permitstart
t.date :permitend
t.references :user, foreign_key: true
t.timestamps
end
add_foreign_key :permits, :user
add_index :permits, [:user_id, :created_at]
end
end
User.rb
class User < ApplicationRecord
has_secure_password
has_many :permits
end
#book pg 264 Validation
permit/new.html.erb
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#permits) do |f| %>
<%= f.label :"Vehicle" %>
<%= f.text_field :vehicle_type, class: 'form-control' %>
<%= f.label :"License Plate" %>
<%= f.text_field :carplate, class: 'form-control' %>
<%= f.label :"Student ID" %>
<%= f.text_field :studentid, class: 'form-control' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :"Department of applicant" %>
<%= f.text_field :department, class: 'form-control' %>
<%= f.label :permit_start %>
<%= f.date_select :permitstart, class: 'form-control' %>
<%= f.label :permit_end %>
<%= f.date_select :permitend, class: 'form-control' %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
schema.rb
ActiveRecord::Schema.define(version: 20160921071908) do
create_table "permits", force: :cascade do |t|
t.string "vehicle_type"
t.string "name"
t.string "studentid"
t.string "department"
t.string "carplate"
t.date "permitstart"
t.date "permitend"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_permits_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.integer "user_type"
end
end
check with this #permits.save!.
it shows the exact error.
module ApplicationHelper
#for current user to use through out the app
def current_user
#current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
end
end
and
def create
#permits = Permit.new(permit_params)
#permits.user = current_user
if #permits.save
redirect_to root_path
else
redirect_to contact_path
end
end
test it
Or you can just say that a permit has a single user and avoid the confusion.
#models/permit.rb
class Permit < ApplicationRecord
has_one :user
end
#controllers/permit_controller.rb
def create
#user = User.find(session[:user_id]) #use your session variable
#permits = Permit.new(permit_params)
if #permits.save
#user.permits << #permits
redirect_to root_path
else
redirect_to contact_path
end
end
It will save permits for the logged in user.

ActionView::Template::Error: SQLite3::SQLException no such column "question_list_id" =?

I have a Rails application and when I run my test i've got this error message:
Error: QuestionListsControllerTest#test_should_show_question_list: ActionView::Template::Error:
SQLite3::SQLException: no such column: questions.question_list_id: SELECT "questions".* FROM "questions" WHERE "questions"."question_list_id" = ?
app/views/question_lists/show.html.erb:8:in `_app_views_question_lists_show_html_erb__3832013936113844388_70290298491920'
test/controllers/question_lists_controller_test.rb:28:in `block in <class:QuestionListsControllerTest>'
This is my controller:
class QuestionListsController < ApplicationController
before_action :set_question_list, only: [:show, :edit, :update, :destroy]
def index
#question_lists = QuestionList.all
end
def show
#questions = #question_list.questions
end
private
def set_question_list
#question_list = QuestionList.find(params[:id])
end
def question_list_params
params.require(:question_list).permit(:title)
end
end
This is my test file:
require 'test_helper'
class QuestionListsControllerTest < ActionController::TestCase
setup do
#question_list = question_lists(:one)
end
test 'should get index' do
get :index
assert_response :success
assert_not_nil assigns(:question_lists)
end
test 'should show question_list' do
get :show, id: #question_list
assert_response :success
end
end
And this is my show.html.erb
<p id="notice"><%= notice %></p>
<p>
<h1>Question List: <%= #question_list.title %></h1>
</p>
<h2>Questions</h2>
<% #questions.each do |question| %>
<p>
<strong>Question:</strong>
<%= question.title %>
</p>
<% end %>
<h2>Create a question</h2>
<%= form_for([#question_list, #questions.build]) do |f| %>
<p>
<%= f.label :title, "Question:" %>
<%= f.text_field :title %>
</p>
<p>
<%= f.submit "Create Question"%>
</p>
<% end %>
<%= link_to 'Edit', edit_question_list_path(#question_list) %> |
<%= link_to 'Back', question_lists_path %>
And the Models en Schema
class QuestionList < ActiveRecord::Base
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :question_list
end
Schema.rb
ActiveRecord::Schema.define(version: 20160316111127) do
create_table "question_lists", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "questions", force: :cascade do |t|
t.string "title"
t.integer "question_list_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
Thanks in advance!
Test database schema is out of sync.
Force a resynchronization with rake db:test:prepare.

Rails - Form HABTM

I am new to Rails but I am stuck. I would like to create a form that will add data to two tables (books and books_authors). My view looks like:
<%= form_for :book, url: book_path do |f| %>
<p>
<%= f.label :title %> :<br/>
<%= f.text_field :title %><br/>
<ul>
<% #authors.each do |x| %>
<li> <%= f.check_box {...} %> <%= x.name + " " + x.surname%></li>
<% end %>
</ul>
</p>
<p><%= f.submit %></p>
and my create method in controller books_controller.rb looks like:
def create
#book = Book.new(params.require(:book).permit(:title))
#book.save
params[:authors].each do |author_id|
book.authors << Author.find(author_id)
end
redirect_to root_path
end
and my schema:
ActiveRecord::Schema.define(version: 20150709110928) do
create_table "author_books", id: false, force: :cascade do |t|
t.integer "author_id"
t.integer "book_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "author_books", ["author_id"], name:
"index_author_books_on_author_id"
add_index "author_books", ["book_id"],
name:"index_author_books_on_book_id"
create_table "authors", force: :cascade do |t|
t.string "name"
t.string "surname"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "books", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
I need params for :authors but I dont know how to nest it inside the form
I n your _form
<% #authors.each do |author| %>
<%= check_box_tag "author_ids[]", author.id %>
<%= author.name %> // I assume that your author has a name which can be used as a label
<% end %>
in your controller,
def create
// you will get the authors in params[:author_ids]
#book = Book.new(params.require(:book).permit(:title, :author_ids => [])) // its a dirty code
// skip other codes
end
fresh code for controller
define a private method and put
def book_params
params.require(:book).permit(:title, :author_ids => [])
end
and in
def create
#book = Book.new(book_params)
end

Resources