Adding attribute values to table record in rails - ruby-on-rails

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

Related

Saving state of object if changes are made - Rails

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?

Model validation not working in rails app

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?

Rails, update the product right after create one

I have the web app on RoR, but the issue is once the user upload the image, the product_id is not associated with the product_attachments. Not until I proceed to next form.
Following were my controller
ProductAttachmentsController:
class ProductAttachmentsController < ApplicationController
before_action :set_product_attachment, only: [:show, :edit, :update, :destroy]
def index
#product_attachments = ProductAttachment.all
end
def show
end
def new
#product_attachment = ProductAttachment.new
end
def create
#product_attachment = ProductAttachment.new(product_attachment_params)
respond_to do |format|
if #product_attachment.save
format.html { redirect_to #product_attachment, notice: 'Product attachment was successfully created.' }
format.json {render :json => #product_attachment}
else
format.html { render :new }
format.json { render json: #product_attachment.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #product_attachment.update(product_attachment_params)
format.html { redirect_to #product_attachment.product, notice: 'Product attachment was successfully updated.' }
format.json { render :show, status: :ok, location: #product_attachment }
else
format.html { render :edit }
format.json { render json: #product_attachment.errors, status: :unprocessable_entity }
end
end
end
def destroy
#product_attachment.destroy
respond_to do |format|
format.html { redirect_to product_attachments_url, notice: 'Product attachment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product_attachment
#product_attachment = ProductAttachment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_attachment_params
params.require(:product_attachment).permit(:product_id, :attachment)
end
end
How can I trick the create method, so it will create a product_id when I create product_attachment? Currently I need to proceed next step and it trigger update method to insert product_id in product_attachments table. Thanks!!
You can store the product attachment id in a session variable
if #product_attachment.save
session[:product_attachment] = #product_attachment.id ### HERE!
format.html { redirect_to #product_attachment, notice: 'Product attachment was successfully created.' }
format.json {render :json => #product_attachment}
else
format.html { render :new }
format.json { render json: #product_attachment.errors, status: :unprocessable_entity }
end
When you create the product (in the ProductsController ) recall the attachment and update the product_id
if #product.save
if session[:product_attachment]
ProductAttachment.find(session[:product_attachment]).update_attribute(:product_id, #product.id)
session[:product_attachment] = nil
end
...
end

How can I allow to create a new object both for registered and unregistered users?

In my Calendar app both registered and unregistered users can create a meeting. In my meetings_controller :
def new
#meeting = Meeting.new
#meeting = current_user.meetings.new(meeting_params) if current_user
end
def create
respond_to do |format|
if #meeting.save
format.html { redirect_to #meeting, notice: 'Meeting was successfully created.' }
format.json { render :show, status: :created, location: #meeting }
else
format.html { render :new }
format.json { render json: #meeting.errors, status: :unprocessable_entity }
end
end
end
def meeting_params
params.require(:meeting).permit(:name, :start_time)
end
So, if a current_user exists it creates current_user.meetings.new(meeting_params) and if not, it should create just a Meeting.new without any user. However, it doesn't work and I get an error:
undefined method `save' for nil:NilClass
respond_to do |format|
if #meeting.save
format.html { redirect_to #meeting, notice: 'Meeting was successfully created.' }
format.json { render :show, status: :created, location: #meeting }
else
It works well if there is a current_user, but why the meeting without a user defines as 'nill' if I mentioned that it shout be just a Meeting_new? How can I make it work?
Thank you!
Change your controller code as like :
def new
#meeting = Meeting.new
end
def create
#meeting = Meeting.new(meeting_params) #edit
#meeting.user_id = current_user.id if current_user
respond_to do |format|
if #meeting.save
format.html { redirect_to #meeting, notice: 'Meeting was successfully created.' }
format.json { render :show, status: :created, location: #meeting }
else
format.html { render :new }
format.json { render json: #meeting.errors, status: :unprocessable_entity }
end
end
end
def meeting_params
params.require(:meeting).permit(:name, :start_time, :user_id)
end

Getting ID from an item that's not saved yet

I am saving data to two different models at once. This has successfully been done.
These two models are associated with each other, so one most store the others ID on save. How to I store the questionnaire_contact_id in QuestionnaireResult?
class QuestionnaireResultsController < ApplicationController
def create
#questionnaire_result = QuestionnaireResult.new(params[:questionnaire_result])
#questionnaire_contact = QuestionnaireContact.new(params[:questionnaire_contact])
respond_to do |format|
if #questionnaire_result.save
#questionnaire_contact.save
format.html { redirect_to root_path, notice: 'Questionnaire was successfully submited.' }
format.json { render json: questionnaires_path, status: :created, location: questionnaires_path }
else
format.html { render action: "new" }
format.json { render json: questionnaires_path.errors, status: :unprocessable_entity }
end
end
end
end
You should use activerecord associations:
def create
#questionnaire_result = QuestionnaireResult.new(params[:questionnaire_result])
#questionnaire_contact = #questionnaire_result.questionnaire_contacts.new(params[:questionnaire_contact])
respond_to do |format|
if #questionnaire_result.save #this line will automatically save associated contact
# code
else
# code
end
end
end
Solved, it was as easy as doing this:
class QuestionnaireResultsController < ApplicationController
def create
#questionnaire_result = QuestionnaireResult.new(params[:questionnaire_result])
#questionnaire_contact = QuestionnaireContact.new(params[:questionnaire_contact])
respond_to do |format|
#questionnaire_contact.save
#questionnaire_result.admin_questionnaire_contact_id = #questionnaire_contact.id
if #questionnaire_result.save
format.html { redirect_to root_path, notice: 'Questionnaire was successfully submited.' }
format.json { render json: questionnaires_path, status: :created, location: questionnaires_path }
else
format.html { render action: "new" }
format.json { render json: questionnaires_path.errors, status: :unprocessable_entity }
end
end
end
end

Resources