Unpermitted parameters: :supplier, :outlet - ruby-on-rails

now i'm doing a grocery website. I trying to build an order form that can choose select supplier and outlet,so admin can create order by choosing which supplier and outlet, but now it come out error say unpermitted parameters.
Orders Controller
class OrdersController < ApplicationController
def supplier
#supplier = Supplier.find(params[:id])
end
def outlet
#outlet = Outlet.find(params[:id])
end
def index
#orders = Order.all
end
def show
#order = Order.find(params[:id])
end
def new
#order = Order.new
#supplier = Supplier.all
#outlet = Outlet.all
end
def create
#order = Order.new(order_params)
#supplier_id = Supplier.all
#outlet_id = Outlet.all
if #order.save
flash[:success] = "Succesful create!"
redirect_to #order
else
render 'new'
end
end
private
def order_params
params.require(:order).permit(:supplier_id,:grand_total, :order_date,
:delivery_date, :delivery_address, :outlet_id)
end
end
new.html.erb
<% provide(:title, "Create Orders") %>
<h1>Create Order</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: #order, local: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :supplier %>
<%= f.select(:supplier, Supplier.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>
<%= f.label :grand_total %>
<%= f.number_field :grand_total, class: 'form-control' %>
<%= f.label :order_date %>
<%= f.date_field :order_date, class: 'form-control' %>
<%= f.label :delivery_date %>
<%= f.date_field :delivery_date, class: 'form-control' %>
<%= f.label :delivery_address %>
<%= f.text_field :delivery_address, class: 'form-control' %>
<%= f.label :outlet %>
<%= f.select(:outlet, Outlet.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>
<%= f.submit "Create order", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Order migration table in schema
create_table "orders", force: :cascade do |t|
t.integer "supplier_id"
t.integer "grand_total"
t.date "order_date"
t.date "delivery_date"
t.string "delivery_address"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "outlet_id"
end
parameter in console
error in website

The reason you're getting
Unpermitted parameters: :supplier, :outlet
is because you're not allowing :supplier and :outlet params but :supplier_id and :outlet_id in order_params
The Fix
Change this in your view new.html.erb
<%= f.label :supplier_id %>
<%= f.select(:supplier_id, Supplier.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>
<%= f.label :outlet_id %>
<%= f.select(:outlet_id, Outlet.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>
This should fix the issue by allowing the :supplier_id and :outlet_id params and assigning the object with proper values

Related

NoMethodError in SuppliersController#create undefined method `outlet_suppliers'

now I'm trying store data (supplier_id and outlet_id) to outlet_suppliers from create supplier form, but it shown some error which is undefined method `outlet_suppliers'. Below is my code:
suppliers controller
def new
#supplier = Supplier.new
#outlet = Outlet.all
#supplier.company_id = params[:company_id]
end
def create
#supplier = Supplier.new(supplier_params)
#company_id = Company.find(params[:supplier][:company_id])
#outlet_id = Outlet.all
if #supplier.save
#outlet_supplier = Outlet.find(params[:supplier][:outlet_id]).outlet_suppliers.create(supplier: #supplier)
redirect_to #supplier
else
render 'new'
end
end
new.html.erb
<% provide(:title, "Create Suppliers") %>
<h1>Create Supplier</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: #supplier, local: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %>
<%= f.label :phone_number %>
<%= f.number_field :phone_number, class: 'form-control' %>
<%= f.label :outlet_id %>
<%= f.select(:outlet_id, Outlet.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>
<%= f.hidden_field :company_id , value: 2%>
<%= f.submit "Create Supplier", class: "btn btn-primary" %>
<% end %>
</div>
</div>
outlet_suppliers migration
create_table "outlet_suppliers", force: :cascade do |t|
t.integer "outlet_id"
t.integer "supplier_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Error show in website
issue is with your model file as pointed out by #Sergio in a comment.
#app/models/outlet_supplier.rb
class OutletSupplier < ApplicationRecord
belongs_to :outlet
belongs_to :supplier
#...
end
#app/models/outlet.rb
class Outlet < ApplicationRecord
has_many :outlet_suppliers
#...
end
I hope this will help you.

ActiveRecord::RecordNotFound in WorkersController#create

now i'm doing creating a worker with role. I have worker , role and worker_role. Now i have done the create worker form and have a selection to choose role. After done the form i need to role_id and worker_id store in worker_role table. I have try and it return me some error.
worker controller
def new
#worker = Worker.new
#worker.company_id = params[:company_id]
#role = Role.all
end
def create
#worker = Worker.new(worker_params)
#company_id = Company.find(params[:worker][:company_id])
#role_id = Role.all
if #worker.save
#worker_role = Role.find(params[:id]).worker_roles.create(worker: worker, returned: true)
#log_in #worker
flash[:success] = "Welcome to the Areca Supermarket System!"
redirect_to #worker
else
render 'new'
end
end
new.html.erb(the form)
<% provide(:title, "Register worker") %>
<h1>Create Worker</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_with(model: #worker, local: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :phone_number %>
<%= f.number_field :phone_number, class: 'form-control' %>
<%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :role_id %>
<%= f.select(:role_id, Role.all.collect { |l| [ l.name, l.id] }, {class: "form-select"}) %>
<%= f.hidden_field :company_id , value: 2%>
<%= f.submit "Create worker", class: "btn btn-primary" %>
<% end %>
</div>
</div>
worker_role model
class WorkerRole < ApplicationRecord
belongs_to :worker
belongs_to :role
end
worker_role migration
create_table "worker_roles", force: :cascade do |t|
t.integer "worker_id"
t.integer "role_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
worker migration
create_table "workers", force: :cascade do |t|
t.string "name"
t.string "email"
t.integer "phone_number"
t.string "address"
t.integer "company_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.boolean "admin", default: false
end
Error show in website
parameter in website
Hi as mentioned in request params, I can see that role_id you are passing in params as
Parameters:
params = {"authenticity_token"=>"[FILTERED]",“worker"=>{"name"=>"Daniel", "“email"=:“daniel#example.com", “phone_number"=:"Q134657891",“address"=>"Alor Setar", “password"=>"[FILTERED]", “role id"=>"1", “company _id"=:"2",“commit"=>"Create worker"}
#you can access the role ID as below:
params[:worker][:role_id]
But you are using it in the controller as
#worker_role = Role.find(params[:id])
#this should be as below:
#worker_role = Role.find(params[:worker][:role_id]).worker_roles.create(worker: #worker)
# OR
#if you are permitting role id
#worker_role = Role.find(worker_params[:role_id]).worker_roles.create(worker: #worker)

unknown attribute 'category_id' for Post

I am trying to create categories for my posts. To do so, I generated a new category model. Although I have associated the two models, I am still receiving this error:
ActiveRecord::UnknownAttributeError: unknown attribute 'category_id' for Post.
I have doublechecked the schema which does include category_id for the post table:
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "cost"
t.integer "openspots"
t.boolean "approval"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "category_id"
end
add_index "posts", ["user_id"], name: "index_posts_on_user_id"
Here are my associations as well:
class Category < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :comments
default_scope { order('created_at DESC') }
end
Finally I wanted to include my posts controller:
class PostsController < ApplicationController
def new
# #post = Post.new
#post = Post.new
#categories = Category.all.map{|c| [ c.name, c.id ] }
end
def create
#post = Post.new(params.require(:post).permit(:title, :description, :cost, :openspots, :approval, :category_id))
#post.category_id = params[:category_id]
#post.user = current_user
if #post.save
flash[:notice] = "Post was saved."
redirect_to #post
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
def update
#post = Post.find(params[:id])
#post.category_id = params[:category_id]
if #post.update_attributes(params.require(:post).permit(:title, :description))
flash[:notice] = "Post was updated."
redirect_to #post
else
flash[:error] = "There was an error saving the post. Please try again."
render :edit
end
end
def edit
#post = Post.find(params[:id])
#post.category_id = params[:category_id]
end
def destroy
end
def index
#posts = Post.all
end
def show
#post = Post.find(params[:id])
end
end
Whenever I create a new post, the post is created successfully;however, the category id is never saved.
Here is my view as well:
<h1>New Post</h1>
<div class="row">
<div class="col-md-4">
<p>Guidelines for posts</p>
<ul>
<li>Make sure it rhymes.</li>
<li>Don't use the letter "A".</li>
<li>The incessant use of hashtags will get you banned.</li>
</ul>
</div>
<div class="col-md-8">
<%= form_for #post do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter post title" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, rows: 8, class: 'form-control', placeholder: "Enter post description" %>
</div>
<div class="form-group">
<%= f.label :cost %>
<%= f.number_field :cost, rows: 8, class: 'form-control', placeholder: "Enter cost" %>
</div>
<div class="form-group">
<%= f.label :openspots %>
<%= f.number_field :openspots, rows: 8, class: 'form-control', placeholder: "Enter Number of Open Spots" %>
</div>
<div class="form-group">
<%= f.label :approval %>
<%= f.check_box :approval, rows: 8, class: 'form-control', placeholder: "Check if Approval Necessary" %>
</div>
<div class="form-group">
<%= f.label :category %>
<%= f.collection_select :category_id, Category.order(:name),:id,:name, include_blank: true %>
</div>
<div class="form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<% end %>
</div>
</div>

Rails: How to compare times and display error?

I'd like to check from and to times.
If from > to, I'd like display an error.
How can I edit my code?
Althogh I tried some codes with cover, include, I haven't be able to apply them to my code.
schema.rb
...
create_table "events", force: :cascade do |t|
t.time "from"
t.time "to"
...
schedules_controller.rb
...
def new
#schedule = Schedule.new
room = #schedule.rooms.build
schedule.events.build
end
def create
#schedule = current_user.schedules.build(schedule_params)
if #schedule.save
flash[:success] = "schedule created!"
redirect_to root_url
else
render 'new'
end
end
def edit
#day_max = Room.where("schedule_id = ?", #schedule.id).maximum(:day)
end
def update
#schedule.rooms.maximum(:day)
if #schedule.update(schedule_params)
flash[:success] = "schedule updated!"
redirect_to root_url
else
render 'edit'
end
end
_schedule_form.html.erb
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
<br>
<%= f.label :departure_date %>
<div class="input-group date" id="datetimepicker">
<%= f.text_field :departure_date, :value => (f.object.departure_date.strftime('%b/%d/%Y') if f.object.departure_date), class: 'form-control' %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker').datetimepicker({format:'MMM-DD-YYYY'});
});
</script>
<br>
<div id="room">
<%= f.simple_fields_for :rooms do |a| %>
<div id="room_<%= a.object.object_id %>">
<p class="day-number-element-selector"><b>Day <%= a.index.to_i + 1 %></b></p>
<%= a.simple_fields_for :events do |e| %>
<span class="form-inline">
<p>
<%= e.input :from, label: false %>
<%= e.input :to, label: false %>
</p>
</span>
<%= e.input :title, label: false %>
<% end %>
</div>
<%= a.link_to_add "Add event", :events, data: {target: "#room_#{a.object.object_id}"}, class: "btn btn-primary" %>
<%= a.input :room %>
<% end %>
</div>
It would be appreciated if you could give me how to check and display error.
You probably want to implement a validator in your event model, as explained in the documentation.
class Event < ActiveRecord::Base
validates :to, presence: true
validates :from, presence: true
validate do |e|
if e.from.present? && e.to.present? and e.from > e.to
e.errors[:base] << "To time must be after from time"
end
end
end

Using form, save :member_id, :group_id and :content to GroupMessage model

(What I would like to do)
Using form, I would like to save some data into GroupMessage model and at the same time,
to show the group message on the page where I filled in the form(groups/show?id=).
(What I did)
I wrote the code as below.
After pushing Create Group Message button,
I went to the page,
http:// localhost:3000/ groups/:id/group_messages
and got an error
"We're sorry, but something went wrong".
Help!!
(group/show?id=)
<%= form_for([#group, #group_message]) do |f| %>
<% if #group_message.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#group_message.errors.count, "error") %> prohibited this group_message from being saved:</h2>
<ul>
<% #group_message.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :member_id %><br />
<%= f.number_field :member_id %>
</div>
<div class="field">
<%= f.label :group_id %><br/>
<%= f.number_field :group_id %>
</div>
<div class="field">
<%= f.label :content %><br/>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
(groups_controller)
def show
#group = Group.find(params[:id])
#group_message = Group.find(params[:id]).group_messages.build
if !checklogin? then return end
groupfriends = GroupInMember.where(:member_id => session[:login].id)
gflist = '('
groupfriends.each do |gf|
gflist += gf.member_id.to_s + ","
end
gflist += session[:login].id.to_s + ')'
#query = "select * from group_messages where member_id in " + gflist
#group_messages = GroupMessage.find_by_sql(#query)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #group_messages }
end
end
(GroupMessage table)
create_table "group_messages", :force => true do |t|
t.integer "member_id"
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "group_id"
end

Resources