I am making a sort of checklist section for my site. I have a model called commission that will contain data about a commissioning task. What I need to do is when a new commission entry is created I need to create a series of about 30 commission tasks that will link to it. A sort of checklist of predefined values for a person to go down through and check. What would be the best way to do this?
Here are my models and controller:
commission.rb
class Commission < ApplicationRecord
has_many :comtasks
belongs_to :project
belongs_to :user
accepts_nested_attributes_for :comtasks, allow_destroy: true
end
comtask.rb
class Comtask < ApplicationRecord
belongs_to :commission
belongs_to :user
end
commissions_controller.rb
class CommissionsController < ApplicationController
before_action :set_commission, only: [:show, :edit, :update, :destroy]
# GET /commissions
# GET /commissions.json
def index
#commissions = Commission.all
end
# GET /commissions/1
# GET /commissions/1.json
def show
end
# GET /commissions/new
def new
#commission = Commission.new
end
# GET /commissions/1/edit
def edit
end
# POST /commissions
# POST /commissions.json
def create
#commission = Commission.new(commission_params)
respond_to do |format|
if #commission.save
format.html { redirect_to #commission, notice: 'Commission was successfully created.' }
format.json { render :show, status: :created, location: #commission }
else
format.html { render :new }
format.json { render json: #commission.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /commissions/1
# PATCH/PUT /commissions/1.json
def update
respond_to do |format|
if #commission.update(commission_params)
format.html { redirect_to #commission, notice: 'Commission was successfully updated.' }
format.json { render :show, status: :ok, location: #commission }
else
format.html { render :edit }
format.json { render json: #commission.errors, status: :unprocessable_entity }
end
end
end
# DELETE /commissions/1
# DELETE /commissions/1.json
def destroy
#commission.destroy
respond_to do |format|
format.html { redirect_to commissions_url, notice: 'Commission was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_commission
#commission = Commission.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def commission_params
params.require(:commission).permit(:project_id, :user_id, :description, :objectname, :location, comtasks_attributes: [:id, :content, :notes])
end
end
Any thoughts or suggestions are appreciated.
below is the idea,
def create
#commission = Commission.create!(commission_params)
# use create not new to generate #commission.id value
# so comtask records can use the id value as reference
create_comtasks_job
# for comtask create I put in other method
respond_to do |format|
if #commission.save
format.html { redirect_to #commission, notice: 'Commission was successfully created.' }
format.json { render :show, status: :created, location: #commission }
else
format.html { render :new }
format.json { render json: #commission.errors, status: :unprocessable_entity }
end
end
end
def create_comtasks_job
# loop 30 tasks / or manual as follow
#commission.comtasks.build(content: 'content1',notes:'notes1')
#commission.comtasks.build(content: 'content2',notes:'notes2')
end
additional code for your model
make sure for your model has relation like sample below
for your model
class Commission < ActiveRecord::Base
has_many :comtasks
end
class Comtask < ActiveRecord::Base
belongs_to :commission
end
Related
I'm rebuilding an app on top an existing database. There is some unexpected wiring going on in the database that I am writing a newer Rails 5.0 app on top of to interact with. And I ran into this problem when trying to add a record to the "Volunteer_events" table.
I have a form at "volunteer_events/new"
My controller looks like this...
class VolunteerEventsController < ApplicationController
before_action :set_volunteer_event, only: [:show, :edit, :update, :destroy]
# GET /volunteer_events
# GET /volunteer_events.json
def index
#volunteer_events = VolunteerEvent.limit(20)
volunteer_task_names
end
def volunteer_task_names
#volunteer_task_names = VolunteerTaskType.uniq.pluck(:description)
# gives me the uniq names of the description column to display in the form dropdown
end
# GET /volunteer_events/1
# GET /volunteer_events/1.json
def show
end
# GET /volunteer_events/new
def new
#volunteer_event = VolunteerEvent.new
volunteer_task_names
end
# GET /volunteer_events/1/edit
def edit
end
# POST /volunteer_events
# POST /volunteer_events.json
def create
#volunteer_event = VolunteerEvent.new(volunteer_event_params)
respond_to do |format|
if #volunteer_event.save
format.html { redirect_to #volunteer_event, notice: 'Volunteer event was successfully created.' }
format.json { render :show, status: :created, location: #volunteer_event }
else
format.html { render :new }
format.json { render json: #volunteer_event.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /volunteer_events/1
# PATCH/PUT /volunteer_events/1.json
def update
respond_to do |format|
if #volunteer_event.update(volunteer_event_params)
format.html { redirect_to #volunteer_event, notice: 'Volunteer event was successfully updated.' }
format.json { render :show, status: :ok, location: #volunteer_event }
else
format.html { render :edit }
format.json { render json: #volunteer_event.errors, status: :unprocessable_entity }
end
end
end
# DELETE /volunteer_events/1
# DELETE /volunteer_events/1.json
def destroy
#volunteer_event.destroy
respond_to do |format|
format.html { redirect_to volunteer_events_url, notice: 'Volunteer event was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_volunteer_event
#volunteer_event = VolunteerEvent.find(params[:id])
end
# Only allow a list of trusted parameters through.
def volunteer_event_params
params.require(:volunteer_event).permit(:description, :volunteer_default_event_id, :date, :notes, :nowalkins)
end
end
My Model looks like this
class VolunteerEvent < ApplicationRecord
# belongs_to :volunteer_default_event
has_many :volunteer_shifts, :dependent => :destroy
has_many :resources_volunteer_events, :dependent => :destroy
validates_associated :volunteer_shifts
def time_range_s
return '0-0' if self.volunteer_shifts.length == 0
my_start_time = self.volunteer_shifts.sort_by(&:start_time).first.start_time
my_end_time = self.volunteer_shifts.sort_by(&:end_time).last.end_time
(my_start_time.strftime("%I:%M") + ' - ' + my_end_time.strftime("%I:%M")).gsub( ':00', '' ).gsub( ' 0', ' ').gsub( ' - ', '-' ).gsub(/^0/, "")
end
end
And I have this error...
PG::ForeignKeyViolation at /volunteer_events
ERROR: insert or update on table "volunteer_events" violates foreign key constraint "volunteer_events_volunteer_default_event_id_fkey"
DETAIL: Key (volunteer_default_event_id)=(0) is not present in table "volunteer_default_events".
What am i missing, doing wrong, there is some unexpected wiring going on in the database that I am writing a newer Rails 5 app on top of to interact with. And I ran into this problem when trying to add a record to the "Volunteer_events" table. What must I do to resolve my error?
Thank you for your time.
I'm learning ROR. Trying to build this model: one user can have many store each store can have many product. I've been able to create store linking to its owner but I'm stuck at doing the same for product.
store.rb
class Store < ApplicationRecord
belongs_to :user
has_many :products, :foreign_key => :store_id
end
product.rb
class Product < ApplicationRecord
belongs_to :store
end
products_controller.rb
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
#products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to #product, 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
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #product }
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:product_name, :product_price, :product_description, :product_tag, :sku_code)
end
end
stores_controller.rb
class StoresController < ApplicationController
before_action :set_store, only: [:show, :edit, :update, :destroy]
# GET /stores
# GET /stores.json
def index
#stores = Store.all
end
# GET /stores/1
# GET /stores/1.json
def show
#products = Product.all
end
# GET /stores/new
def new
#store = Store.new
end
# GET /stores/1/edit
def edit
end
# POST /stores
# POST /stores.json
def create
#store = Store.new(store_params)
#store.user_id = current_user.id
respond_to do |format|
if #store.save
format.html { redirect_to #store, notice: 'Store was successfully created.' }
format.json { render :show, status: :created, location: #store }
else
format.html { render :new }
format.json { render json: #store.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /stores/1
# PATCH/PUT /stores/1.json
def update
respond_to do |format|
if #store.update(store_params)
format.html { redirect_to #store, notice: 'Store was successfully updated.' }
format.json { render :show, status: :ok, location: #store }
else
format.html { render :edit }
format.json { render json: #store.errors, status: :unprocessable_entity }
end
end
end
# DELETE /stores/1
# DELETE /stores/1.json
def destroy
#store.destroy
respond_to do |format|
format.html { redirect_to stores_url, notice: 'Store was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_store
#store = Store.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def store_params
params.require(:store).permit(:store_name, :store_description)
end
end
I expect when creating a product, it will get the store_id immediately. I'm keep getting "store must exist" message
From Rails 5, belongs_to requires the association object to be present. If your product object does not have a valid store_id (nil or inexistent store object), that product object will not be valid.
You can bypass this by allowing the association to be optional
class Product < ApplicationRecord
belongs_to :store, optional: true
end
Or, you should allow store_id as permitted parameter in your products_controller and pass it when you create a new product
# below code I assume that your store table has a column called 'store_name'
<%= form_for #product do |f| %>
<%= f.collection_select :store_id, current_user.stores, :id, :store_name, prompt: 'Please select the store of this product' %>
<%= f.submit %>
<% end %>
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux],
Rails 4.2.5
I have two projects. from 1st project i am getting data into second project through api.
User model in 1st project:
class User < ActiveRecord::Base
has_many :cars
end
Car model in 1st project:
class Car < ActiveRecord::Base
belongs_to :user
end
Car model(remote) in 2nd project:
class Car < ActiveResource::Base
self.site = 'https://myeasyb-vssram.c9users.io'
self.format = :json
end
Gpstablecontroller(2nd project):
class GpstablesController < ApplicationController
before_action :set_gpstable, only: [:show, :edit, :update, :destroy]
# GET /gpstables
# GET /gpstables.json
def index
#gpstables = Gpstable.all
end
# GET /gpstables/1
# GET /gpstables/1.json
def show
end
# GET /gpstables/new
def new
#gpstable = Gpstable.new
#gpstables = Gpstable.all
end
# GET /gpstables/1/edit
def edit
#gpstables = Gpstable.all
end
# POST /gpstables
# POST /gpstables.json
def create
#cars = Car.all
#gpstable = Gpstable.new(gpstable_params)
#cars.each do |car|
if #gpstable.car_id == car.id
#car = car
end
end
#car.update_attribute(:gpss, #gpstable.device_id)
respond_to do |format|
if #gpstable.save
format.html { redirect_to gpstables_url, notice: 'Gpstable was successfully created.' }
format.json { render :show, status: :created, location: #gpstable }
else
format.html { render :new }
format.json { render json: #gpstable.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /gpstables/1
# PATCH/PUT /gpstables/1.json
def update
respond_to do |format|
if #gpstable.update(gpstable_params)
Car.all.each do |car|
if #gpstable.car_id == car.id.to_json
#car = car
end
if #gpstable.device_id == car.gpss
car.gpss = 0
car.save!
end
end
#car.gpss = #gpstable.device_id
#car.save!
format.html { redirect_to #gpstable, notice: 'Gpstable was successfully updated.' }
format.json { render :show, status: :ok, location: #gpstable }
else
format.html { render :edit }
format.json { render json: #gpstable.errors, status: :unprocessable_entity }
end
end
end
# DELETE /gpstables/1
# DELETE /gpstables/1.json
def destroy
#cars.each do |car|
if #gpstable.device_id == car.gpss
car.gpss = 0
car.user_id = #gpstable.user_id
car.save
end
end
#gpstable.destroy
respond_to do |format|
format.html { redirect_to gpstables_url, notice: 'Gpstable was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_gpstable
#gpstable = Gpstable.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def gpstable_params
params.require(:gpstable).permit(:device_id, :car_id, :user_id)
end
end
when creating gpstable record i want to update Gpss attribute of car model(remote, calling through api).
it is updating gpss attribute.But it is changing all foriegnkeys including user_id attribute of car model to null.
using devise for users in 1st project.
The problem is i am giving user_id to current user_id in car_params. so i was unable to edit this through resource model. so i changed this to create action.
In my first app i have car controller:
def car_params
params.require(:car).permit(:name, :model, :colour, :ac, :gpss, :wifi, :luggage, :cfare, :card, :crfare, :no, :nos, :user_id, {carpicss: []} ).**merge(user: :current_user)**
end
i removed .merge(user: :current_user) from above code.
and added this in create action
def create
#car = Car.new(car_params)
#car.card=" #{#car.name} : #{#car.no}"
#car.user_id=current_user.id #added here to save current user_id
respond_to do |format|
if #car.save
format.html { redirect_to cars_path, notice: 'Car was successfully created.' }
format.json { render :show, status: :created, location: cars_path }
else
format.html { render :new }
format.json { render json: #car.errors, status: :unprocessable_entity }
end
#car.reload
end
end
I've used this instructions for simply add score when a user creates a "solucion" (which is a kind of "answer" to a micropost). I have added the has_merit line to user.rb (user model).
I want to display the user points earned for that action at the show view.
show.html.erb (for solucion):
<h2><span class="red"><%= current_user.points %></span><br>Points</br></h2>
It displays 0 points...
point_rules.rb:
module Merit
class PointRules
include Merit::PointRulesMethods
def initialize
score 5, on: 'solucions#create'
end
end
end
When I create a solucion with the current_user (already saving the user_id index and identifier to solucion), This is what my rails server output shows...
Direct link to github gist:
https://gist.github.com/roadev/7b34fd67ab93c979fa48
Embed:
<script src="https://gist.github.com/roadev/7b34fd67ab93c979fa48.js"></script>
EDIT:
solucions_micropost.rb
class SolucionsController < ApplicationController
before_action :set_solucion, only: [:show, :edit, :update, :destroy]
def index
#solucions = Solucion.all
end
def show
end
def new
#solucion = current_user.solucions.build
end
def edit
end
def create
#solucion = current_user.solucions.build(solucion_params)
respond_to do |format|
if #solucion.save
format.html { redirect_to #solucion, notice: 'Solucion was successfully created.' }
format.json { render action: 'show', status: :created, location: #solucion }
else
format.html { render action: 'new' }
format.json { render json: #solucion.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #solucion.update(solucion_params)
format.html { redirect_to #solucion, notice: 'Solucion was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #solucion.errors, status: :unprocessable_entity }
end
end
end
def destroy
#solucion.destroy
respond_to do |format|
format.html { redirect_to solucions_url }
format.json { head :no_content }
end
end
private
def set_solucion
#solucion = Solucion.find(params[:id])
end
def current_micropost
#solucion = microposts.find_by(id: params[:id])
end
def solucion_params
params.require(:solucion).permit(:solucion, :image, :micropost_id)
end
end
user.rb:
class User < ActiveRecord::Base
has_many :dreams
has_many :microposts
has_many :solucions
has_merit
end
I had a problem with a migration when I installed the merit gem.
I have basic form that is accessed, for example via: http://url.com/rentals/new/dvd/10.
The problem is when form error happens I can't redirect it to the same page with the same
url segments and show the form error messages.
rentals_controller.rb:
def create
#rental = Rental.new(rental_params)
respond_to do |format|
if #rental.save
format.html { redirect_to #rental, notice: 'Rental was successfully created.' }
format.json { render :show, status: :created, location: #rental }
else
format.html { render :new }
format.json { render json: #rental.errors, status: :unprocessable_entity }
end
end
end
routes.rb
get 'rentals/new/dvd/:dvd_id' => 'rentals#new', as: :new_dvd_rental
I have the following models created:
dvd.rb
class Dvd < ActiveRecord::Base
has_many :rentals
has_many :users, through: :rentals
validates :title, presence: true
validates :year, inclusion: {in: 1900..Time.now.year.to_i}, :presence => {:message => 'Year must be from 1900 till current year.'}
validates :length, inclusion: {in: 1..999}, :presence => {:message => 'DVD length must be in minutes in range 1..999.'}
end
rental.rb
class Rental < ActiveRecord::Base
belongs_to :user
belongs_to :dvd
validates :user_id, presence: true
validates :total_price, presence: true
end
user.rb
class User < ActiveRecord::Base
has_many :rentals
has_many :dvds, through: :rentals
end
As well as rentals_controller.rb:
class RentalsController < ApplicationController
before_action :set_rental, only: [:show, :edit, :update, :destroy]
# GET /rentals
# GET /rentals.json
def index
#rentals = Rental.all
end
# GET /rentals/1
# GET /rentals/1.json
def show
end
# GET /rentals/new
def new
#rental = Rental.new
#users = User.all
#dvd = Dvd.find(params[:dvd_id])
end
# GET /rentals/1/edit
def edit
end
# POST /rentals
# POST /rentals.json
def create
#rental = Rental.new(rental_params)
respond_to do |format|
if #rental.save
format.html { redirect_to #rental, notice: 'Rental was successfully created.' }
format.json { render :show, status: :created, location: #rental }
else
format.html { render :new }
format.json { render json: #rental.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /rentals/1
# PATCH/PUT /rentals/1.json
def update
respond_to do |format|
if #rental.update(rental_params)
format.html { redirect_to #rental, notice: 'Rental was successfully updated.' }
format.json { render :show, status: :ok, location: #rental }
else
format.html { render :edit }
format.json { render json: #rental.errors, status: :unprocessable_entity }
end
end
end
# DELETE /rentals/1
# DELETE /rentals/1.json
def destroy
#rental.destroy
respond_to do |format|
format.html { redirect_to rental_url, notice: 'Rental was successfully deleted.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_rental
#rental = Rental.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def rental_params
params.require(:rental).permit(:dvd_id, :user_id, :rent_date, :return_date, :total_price, :returned)
end
end
I've tried to modify rental controller like this, but still do not know how to pass other segments like new and dvd:
render :action => "new", :dvd_id => params[:dvd_id]
Any ideas?
I think if you draw a more restful route like this
resources :dvds do
resources :rentals
end
you will get the routes like http://url.com/dvd/10/rentals/new
here you will always get dvd_id
and in rentals_controller create method look like
def create
#dvd = Dvd.find(params[:dvd_id])
#rental = Rental.new(rental_params)
respond_to do |format|
if #rental.save
format.html { redirect_to #rental, notice: 'Rental was successfully created.' }
format.json { render :show, status: :created, location: #rental }
else
format.html { render :new }
format.json { render json: #rental.errors, status: :unprocessable_entity }
end
end
end
-- Waiting for #Sanket's ideas
Routes
The issue will almost certainly be with your redirect_to method
The problem is that your controller doesn't know you're using a nested resource, and consequently when you redirect to an object, it will likely just take you to the simplest route it can find
I would try this:
def create
...
else
format.html { render your_nested_resource_path(dvd_id: params[:dvd_id], other: params[:params]) }
...
end
This allows you to send the request to the nested route, which Rails won't route to without support