I have a validation in one of my models: (The sku_code validation.)
class Vendor < ActiveRecord::Base
has_many :purchases
validates :name, format: {with: /\A[A-Za-z0-9\S\s]+\z/, message: "Vendor name is invalid."}
validates :sku_code, format: {with: /\A[A-Z]{3}\z/, message: "must follow format ABC"}
end
Controller:
class VendorsController < ApplicationController
def index
#vendors = Vendor.all
respond_to do |format|
format.js
format.html
format.json
end
end
def new
#vendor = Vendor.new
respond_to do |format|
format.js
format.html
format.json
end
end
def create
#vendor = Vendor.new(vendor_params)
respond_to do |format|
if #vendor.save
format.html { redirect_to #vendor, notice: 'Vendor successfully created.' }
format.js {}
format.json { render json: #vendor, status: :created, location: #vendor }
else
format.html { render 'new' }
format.json { render json: #vendor.errors, status: :unprocessable_entity }
end
end
end
def update
#vendor = Vendor.find(params[:id])
respond_to do |format|
if #vendor.update(vendor_params)
format.html { redirect_to #vendor, notice: 'Vendor successfully updated.' }
format.js {}
format.json { render json: #vendor, status: :updated, location: #vendor }
else
format.html { render 'edit' }
format.json { render json: #vendor.errors, status: :unprocessable_entity }
end
end
end
def edit
#vendor = Vendor.find(params[:id])
respond_to do |format|
format.js
format.html
format.json
end
end
def destroy
#vendor = Vendor.find(params[:id])
#vendor.destroy
#flash.notice="Vendor '#{#vendor.name}' was deleted."
redirect_to action: "index"
end
def show
#vendor = Vendor.find(params[:id])
respond_to do |format|
format.js
format.html
format.json
end
end
private
def vendor_params
params.require(:vendor).permit(:name, :sku_code, :contact, :phone, :email, :address_line1, :address_line2, :address_city, :address_state, :address_zip, :address_country, :lead_time)
end
end
What I expected with this regex was that it would allow for a three capital letter string to be entered into my form but prevent anything else.
Currently it is rejecting the three capital letter string.
What have I done wrong here?
Related
Rails 7
I have the following:
controllers/orders_controller.rb
class OrdersController < ApplicationController
before_action :set_order, only: %i[ show edit update destroy ]
def index
#orders = Order.all
end
def show
end
def new
#order = Order.new
end
def edit
end
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save
format.html { redirect_to order_url(#order), notice: "Order was successfully created." }
format.json { render :show, status: :created, location: #order }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #order.update(order_params)
format.html { redirect_to order_url(#order), notice: "Order was successfully updated." }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
def destroy
#order.destroy
respond_to do |format|
format.html { redirect_to orders_url, notice: "Order was successfully destroyed." }
format.json { head :no_content }
end
end
private
def set_order
#order = Order.find(params[:id])
end
def order_params
params.require(:order).permit(:country, :sku, :abbreviation, :description)
end
end
models/order.rb
class Order < ApplicationRecord
end
What I would like to do, is create an OrderHistory Model, with the following fields:
order_id (int)
order_history (JSON)
When an update is made to the order, I would like the DB entry for the original order, saved as a JSON to OrderHistory.
What I am having trouble with, is creating a variable that contains the state of the existing order, when I enter the controller, without making it available to all app users. Any ideas?
I need to create array of Events instead of one with same params except event.id . This is code :
def create
#event = Event.new(event_params)
#event.user_id = current_user.id
respond_to do |format|
if #event.save
format.html { redirect_to #event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: #event }
else
format.html { render :new }
format.json { render json: #event.errors, status: :unprocessable_entity }
end
end
end
How should I do this?
UPD
Event params :
def event_params
params.require(:event).permit(:title, :description, :start_time, :end_time, :repeat)
end
def create
(1..10).each do |i|
params[:event][:user_id] = current_user.id
params[:event][:start_time] = params[:event][:start_time] + 24.hours if i > 1
params[:event][:end_time] = params[:event][:end_time] + 24.hours if i > 1
#event = Event.new(event_params)
#event.save
end
redirect_to events_path
end
I want to insert user_id when product#create action.
How to insert user_id in productController??
user is just model.
here is
products_controller.rb
def create
#user = current_user
#product = Product.new(product_params, user_id: user_id)
respond_to do |format|
if #product.save
format.html { redirect_to wardrobe_items_path, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
product table
class AddUserIdToProducts < ActiveRecord::Migration
def change
add_column :products, :user_id, :string
end
end
Do this
#product = Product.new(product_params)
#product.user_id = current_user.id
Do This...
def create
#user = current_user
#product = Product.new(product_params)
#product.user_id = current_user.id
respond_to do |format|
if #product.save
format.html { redirect_to wardrobe_items_path, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
I have a website I am making that tracks a users companies through employments. I need to know what I am doing wrong because when I make a new user company the user doesn't know about it.
companies_controller.rb
class CompaniesController < ApplicationController
# GET /companies
# GET /companies.json
def index
#companies = current_user.companies
respond_to do |format|
format.html # index.html.erb
format.json { render json: #companies }
end
end
# GET /companies/1
# GET /companies/1.json
def show
#company = Company.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #company }
end
end
# GET /companies/new
# GET /companies/new.json
def new
#company = Company.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #company }
end
end
# GET /companies/1/edit
def edit
#company = Company.find(params[:id])
end
# POST /companies
# POST /companies.json
def create
#company = Company.new(params[:company])
current_user.employments.create!(company_id: #company.id)
respond_to do |format|
if #company.save
format.html { redirect_to #company, notice: 'Company was successfully created.' }
format.json { render json: #company, status: :created, location: #company }
else
format.html { render action: "new" }
format.json { render json: #company.errors, status: :unprocessable_entity }
end
end
end
# PUT /companies/1
# PUT /companies/1.json
def update
#company = Company.find(params[:id])
respond_to do |format|
if #company.update_attributes(params[:company])
format.html { redirect_to #company, notice: 'Company was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #company.errors, status: :unprocessable_entity }
end
end
end
# DELETE /companies/1
# DELETE /companies/1.json
def destroy
#company = Company.find(params[:id])
#company.destroy
respond_to do |format|
format.html { redirect_to companies_url }
format.json { head :no_content }
end
end
end
The problem is within your create action, specifically the line
current_user.employments.create!(company_id: #company.id)
this is executed before the company record is saved so it doesn't have an id (== nil). Just move that line after
if #company.save
and it should attach it to the current_user via the :through relationship.
Hello guys I've 2 models: User(aka parent) and Profil(aka child).And I want to limit the number of profil for a user to one.
models/user.rb
class User < ActiveRecord::Base
has_one :profil
end
models/profil.rb
class Profil < ActiveRecord::Base
attr_accessible :logo
belongs_to :user
mount_uploader :logo, ImageUploader
validate :limit_profil_to_one, :on => :create
def limit_profil_to_one
if self.user.profils(:reload).count > 1
errors.add(:base, "Exceeded thing limit")
end
end
end
But when I try to create a profil I get the following error message:
NoMethodError (undefined method `profils' for nil:NilClass):
app/models/profil.rb:11:in `limit_profil_to_one'
app/controllers/profils_controller.rb:52:in `block in create'
app/controllers/profils_controller.rb:51:in `create
controllers/profils_controller.rb
# -*- encoding : utf-8 -*-
class ProfilsController < ApplicationController
# GET /factures
# GET /factures.json
def index
#profils = Profil.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #profil }
end
end
# GET /profils/1
# GET /profils/1.json
def show
#profil = Profil.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #profil }
end
end
# GET /profils/new
# GET /profils/new.json
def new
#profil = Profil.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #profil }
end
end
# GET /profils/1/edit
def edit
#profil = Profil.find(params[:id])
end
# POST /profils
# POST /profils.json
def create
#profil = Profil.new(params[:profil])
respond_to do |format|
if #profil.save
format.html { redirect_to #profil, notice: 'Profil was successfully created.' }
format.json { render json: #profil, status: :created, location: #profil }
else
format.html { render action: "new" }
format.json { render json: #profil.errors, status: :unprocessable_entity }
end
end
end
# PUT /profils/1
# PUT /profils/1.json
def update
#profil = Profil.find(params[:id])
respond_to do |format|
if #profil.update_attributes(params[:profil])
format.html { redirect_to #profil, notice: 'Profil was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #profil.errors, status: :unprocessable_entity }
end
end
end
# DELETE /factures/1
# DELETE /factures/1.json
def destroy
#profil = Profil.find(params[:id])
#profil.destroy
respond_to do |format|
format.html { redirect_to profils_url }
format.json { head :ok }
end
end
end
What I am doing wrong?
Look at line 2 in the limit_profil_to_one - self.user is nil so it is failing.
def limit_profil_to_one
if self.user.profils(:reload).count > 1 # self.user is nil
errors.add(:base, "Exceeded thing limit")
end
end
I am making some assumptions about what your app is doing, but for this post I am going to assume that your controller has a current user defined in the controller and that you are creating a Profil for that User (side: note, what is a profil? I am assuming you actually mean profile) You should set the user in the controller to the user it is supposed to be, like so.
def create
#profil = Profil.new(params[:profil])
#profil.user = current_user # however you access the currently logged in user
respond_to do |format|
if #profil.save
format.html { redirect_to #profil, notice: 'Profil was successfully created.' }
format.json { render json: #profil, status: :created, location: #profil }
else
format.html { render action: "new" }
format.json { render json: #profil.errors, status: :unprocessable_entity }
end
end
end