I want to set a reservation based on user_id and trip_id . but i dont know how to . I tried to make a form in trips/show
What i want is to see the full list of trips and select one and the form below should save the user_id and trip_id in databse. Idk if i can send data like this because i wrote the form in trips which has his own model of Trip.
Can someone explain how has_many :through concept work. The problem is that idk if i can make another controller to save ids or i can do it in trip controllers. please help
<h1>Trips</h1>
<table class="table ">
<thead class="thead-dark">
<tr>
<th>Locatie</th>
<th>Descriere</th>
<th colspan="3"></th>
</tr>
</thead>
<%= Current.user.admin %>
<% if Current.user.admin == true %>
<tbody>
<% #trips.each do |trip| %>
<tr>
<td><%= trip.locatie %></td>
<td><%= trip.descriere %></td>
<td class="text-center"><%= link_to 'Select', trip , class: "btn btn-secondary "%></td>
<td class="text-center"><%= link_to 'Edit', edit_trip_path(trip) ,class: "btn btn-secondary " %></td>
<td class="text-center"><%= link_to 'Destroy', trip, method: :delete, data: { confirm: 'Are you sure?' } ,class: "btn btn-secondary "%></td>
</tr>
<% end %>
</tbody>
<% else %>
<tbody>
<% #trips.each do |trip| %>
<tr>
<td><%= trip.locatie %></td>
<td><%= trip.descriere %></td>
<td class="text-center"><%= link_to 'Select', trip , class: "btn btn-secondary "%></td>
</tr>
<% end %>
</tbody>
<% end %>
</table>
<br>
<%= link_to 'New Trip', new_trip_path ,class:"btn btn-secondary"%>
<p>
<strong>Locatie:</strong>
<%= #trip.locatie %>
</p>
<p>
<strong>Descriere:</strong>
<%= #trip.descriere %>
</p>
<%= #trip.id %>
<%= Current.user.id %>
<br/>
<%= form_with model: #reservation, local: true , url: save_reservation_path do |form| %>
<%= form.text_field :user_id , value: #trip.id %>
<%= form.text_field :trip_id , value: Current.user.id%>
<%= form.submit %>
<% end %>
<%= link_to 'Back', trips_path ,class: "btn btn-secondary "%>
Trip controller
class TripsController < ApplicationController
before_action :set_trip, only: %i[ show edit update destroy ]
# GET /trips or /trips.json
def index
#trips = Trip.all
end
# GET /trips/1 or /trips/1.json
def show
end
# GET /trips/new
def new
#trip = Trip.new
end
# GET /trips/1/edit
def edit
end
# POST /trips or /trips.json
def create
#trip = Trip.new(trip_params)
respond_to do |format|
if #trip.save
format.html { redirect_to trip_url(#trip), notice: "Trip was successfully created." }
format.json { render :show, status: :created, location: #trip }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #trip.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /trips/1 or /trips/1.json
def update
respond_to do |format|
if #trip.update(trip_params)
format.html { redirect_to trip_url(#trip), notice: "Trip was successfully updated." }
format.json { render :show, status: :ok, location: #trip }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #trip.errors, status: :unprocessable_entity }
end
end
end
# DELETE /trips/1 or /trips/1.json
def destroy
#trip.destroy
respond_to do |format|
format.html { redirect_to trips_url, notice: "Trip was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_trip
#trip = Trip.find(params[:id])
end
# Only allow a list of trusted parameters through.
def trip_params
params.require(:trip).permit(:locatie, :descriere)
end
end
Reservation controller
class ReservationsController < ApplicationController
# GET /reservations or /reservations.json
def index
#reservations = Reservation.all
end
# GET /reservations/1 or /reservations/1.json
def show
#reservations = Reservation.all
end
# GET /reservations/new
def new
#reservation = Reservation.new
end
# GET /reservations/1/edit
def edit
end
# POST /reservations or /reservations.json
def create
#reservation = Reservation.new(reservation_params)
respond_to do |format|
if #reservation.save
format.html { redirect_to reservation_url(#reservation), notice: "Reservation was successfully created." }
format.json { render :show, status: :created, location: #reservation }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reservations/1 or /reservations/1.json
def update
respond_to do |format|
if #reservation.update(reservation_params)
format.html { redirect_to reservation_url(#reservation), notice: "Reservation was successfully updated." }
format.json { render :show, status: :ok, location: #reservation }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reservations/1 or /reservations/1.json
def destroy
#reservation.destroy
respond_to do |format|
format.html { redirect_to reservations_url, notice: "Reservation was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reservation
#reservation = Reservation.find(params[:id])
end
# Only allow a list of trusted parameters through.
def reservation_params
params.require(:reservation_params).permit(user_id: [], trip_id: [])
end
end
Related
I am new to Ruby on Rails and we have a project called TaskApp.
It that has 2 models: Category, Task.
Category has many tasks and Task belongs to Category.
When I go from tasks index to edit task, I am getting the edit view with blank fields.
Please find relevant codes below.
I am grateful for any advise to get me to resolve this.
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: 'registrations' }
get 'home/index'
root 'home#index'
resources :categories do
resources :tasks
end
end
tasks_controller.rb
class TasksController < ApplicationController
before_action :authenticate_user!
before_action :set_category, except: %i[index edit]
before_action :set_task, only: %i[show update destroy]
def index
#tasks = Task.where(user_id: current_user.id, deadline: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).order(priority: :asc)
end
def new
#task = #category.tasks.build
end
def show
end
def create
#task = #category.tasks.create(task_params.merge(user_id: current_user.id))
respond_to do |format|
if #task.save
format.html { redirect_to #task.category, notice: "Task was successfully created." }
format.json { render :show, status: :created, location: #task.category }
else
format.html { redirect_to #task.category, notice: "Invalid inputs." }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def edit
#category = Category.find(params[:category_id])
#task = #category.tasks.find(params[:id])
end
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task.category, notice: 'Task was successfully updated.' }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to #task.category, notice: 'Task was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_category
#category = Category.find(params[:category_id])
end
def set_task
#task = #category.tasks.find(params[:id])
end
def task_params
params.require(:task).permit(:name, :description, :priority, :deadline, :completed, :user_id, :category_id)
end
end
.../tasks/views/edit.html.erb
<div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Priority</th>
<th>Deadline</th>
<th>Completed</th>
<th>Action</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<% #category.tasks.each do |task| %>
<% if task.user_id == current_user.id %>
<tr>
<td><%= link_to task.name, [task.category, task] %></td>
<td><%= task.description %></td>
<td><%= task.priority %></td>
<td><%= task.deadline.strftime("%d %b %Y") %></td>
<td><%= task.completed %></td>
<td><%= link_to 'Edit', edit_category_task_path(task.category, task) %></td>
<td><%= link_to 'Delete', [task.category, task],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
I am currently getting this error while loading up the homepage. I have only create the first page which I's trying to make the homepage. TweeetsController#index is missing a template for request formats: text/html Below are my codes for the controller, view, and routes. Thank you for any help.
Controller:
class TweeetsController < ApplicationController
before_action :set_tweeet, only: [:show, :edit, :update, :destroy]
# GET /tweeets
# GET /tweeets.json
def index
#tweeets = Tweeet.all
end
# GET /tweeets/1
# GET /tweeets/1.json
def show
end
# GET /tweeets/new
def new
#tweeet = Tweeet.new
end
# GET /tweeets/1/edit
def edit
end
# POST /tweeets
# POST /tweeets.json
def create
#tweeet = Tweeet.new(tweeet_params)
respond_to do |format|
if #tweeet.save
format.html { redirect_to #tweeet, notice: 'Tweeet was successfully created.' }
format.json { render :show, status: :created, location: #tweeet }
else
format.html { render :new }
format.json { render json: #tweeet.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tweeets/1
# PATCH/PUT /tweeets/1.json
def update
respond_to do |format|
if #tweeet.update(tweeet_params)
format.html { redirect_to #tweeet, notice: 'Tweeet was successfully updated.' }
format.json { render :show, status: :ok, location: #tweeet }
else
format.html { render :edit }
format.json { render json: #tweeet.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tweeets/1
# DELETE /tweeets/1.json
def destroy
#tweeet.destroy
respond_to do |format|
format.html { redirect_to tweeets_url, notice: 'Tweeet was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tweeet
#tweeet = Tweeet.find(params[:id])
end
# Only allow a list of trusted parameters through.
def tweeet_params
params.require(:tweeet).permit(:tweeet)
end
end
view:
<p id="notice"><%= notice %></p>
<h1>Tweeets</h1>
<table>
<thead>
<tr>
<th>Tweeet</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tweeets.each do |tweeet| %>
<tr>
<td><%= tweeet.tweeet %></td>
<td><%= link_to 'Show', tweeet %></td>
<td><%= link_to 'Edit', edit_tweeet_path(tweeet) %></td>
<td><%= link_to 'Destroy', tweeet, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Tweeet', new_tweeet_path %>
routes:
resources :tweeets
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root "tweeets#index"
end
model:
class Tweeet < ApplicationRecord
end
I am trying to add items to a table in rails but getting the error it the title. It works for me locally but on heroku it gives me this error. I have seen a couple of solution but none seem to work. This is my controller:
require './app/policies/personal_trainer_policy'
class PersonalTrainersController < ApplicationController
before_action :set_personal_trainer, only: [:show, :edit, :update, :destroy]
# GET /personal_trainers
# GET /personal_trainers.json
def index
#personal_trainers = PersonalTrainer.all
end
# GET /personal_trainers/1
# GET /personal_trainers/1.json
def show
end
# GET /personal_trainers/new
def new
#personal_trainer = PersonalTrainer.new
end
# GET /personal_trainers/1/edit
def edit
end
def personal_trainer_policy
#_personal_trainer_policy ||= PersonalTrainerPolicy.new(personal_trainer)
end
# POST /personal_trainers
# POST /personal_trainers.json
def create
#personal_trainer = PersonalTrainer.new(personal_trainer_params)
authorize #personal_trainer
if #personal_trainer.persist
render json: #personal_trainer.record
else
render json: #personal_trainer.errors, status: :unpocessably_entity
end
respond_to do |format|
if #personal_trainer.save
format.html { redirect_to #personal_trainer, notice: 'Personal trainer was successfully created.' }
format.json { render :show, status: :created, location: #personal_trainer }
else
format.html { render :new }
format.json { render json: #personal_trainer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /personal_trainers/1
# PATCH/PUT /personal_trainers/1.json
def update
authorize #personal_trainer
respond_to do |format|
if #personal_trainer.update(personal_trainer_params)
format.html { redirect_to #personal_trainer, notice: 'Personal trainer was successfully updated.' }
format.json { render :show, status: :ok, location: #personal_trainer }
else
format.html { render :edit }
format.json { render json: #personal_trainer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /personal_trainers/1
# DELETE /personal_trainers/1.json
def destroy
authorize #personal_trainer
#personal_trainer.destroy
respond_to do |format|
format.html { redirect_to personal_trainers_url, notice: 'Personal trainer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_personal_trainer
#personal_trainer = PersonalTrainer.find(params[:id])
end
# Only allow a list of trusted parameters through.
def personal_trainer_params
params.require(:personal_trainer).permit(:firstName, :secondName, :desription, :amountOfClients)
end
end
The controller was generated using a scaffold. This is my erb file:
<p id="notice"><%= notice %></p>
<h1 class = "title">Personal Trainers</h1>
<div class = "page-conatiner">
<table class = "table">
<thead>
<%= stylesheet_link_tag 'gym_classes', media: 'all', 'data-turbolinks-track': 'reload' %>
<tr>
<th>First Name</th>
<th>Second Name</th>
<th>Desription</th>
<th>Amount of Clients</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #personal_trainers.each do |personal_trainer| %>
<tr>
<td><%= personal_trainer.firstName %></td>
<td><%= personal_trainer.secondName %></td>
<td><%= personal_trainer.desription %></td>
<td><%= personal_trainer.amountOfClients %></td>
<td><%= link_to 'Show', personal_trainer %></td>
<td><%= link_to 'Edit', edit_personal_trainer_path(personal_trainer) %></td>
<td><%= link_to 'Destroy', personal_trainer, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if current_user.admin == true %>
<%= link_to 'New Personal Trainer', new_personal_trainer_path %>
<% end %>
</div>
and this is my model:
class PersonalTrainer < ApplicationRecord
has_many :gym_class_final
has_many :pt_client
end
I have it set so that admins can only create new items using a policy and adding authorise #personal_trainer. I wonder if this has something to do with the error. I just dont understand how it would work locally but not when its deployed. Any suggestions.
persist is not a valid method on an active record object, you're looking for save:
if #personal_trainer.save
render json: #personal_trainer.record
else
render json: #personal_trainer.errors, status: :unpocessably_entity
end
For more information on what methods are available for active record persistence, see https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html (Rails 6)
Hi I have a simple rails application that has two models Equipment_Types & Tasks
Each equipment type has many tasks. When I export to csv file I would like to export the name of the equipment type, the task name and the associated schedule. Basically every thing that is displayed on equipment_type/show.html.erb
<p id="notice"><%= notice %></p>
<div class="row">
<table class="table table-bordered">
<thead>
<tr>
<th class="align-middle" rowspan="2">Equipment</th>
<th class="align-middle" rowspan="2">Task</th>
<th class="text-center" colspan="4">Frequency</th>
</tr>
<tr>
<th class="text-center">M</th>
<th class="text-center">Q</th>
<th class="text-center">B</th>
<th class="text-center">A</th>
</tr>
</thead>
<tbody>
<tr>
<% if #taskcount > 3 %>
<td class="text-center bottomtotop" rowspan="0"><%= link_to #equipment_type.name, edit_equipment_type_path(#equipment_type) %></td>
<% else %>
<td class="text-center" rowspan="0"><%= link_to #equipment_type.name, edit_equipment_type_path(#equipment_type) %></td>
<% end %>
</tr>
<% #equip_tasks.each do |task| %>
<tr>
<td><%= link_to task.name, task %></td>
<td class="text-center"><%= if task.monthly then 'x' else ' ' end %></td>
<td class="text-center"><%= if task.quarterly then 'x' else ' ' end %></td>
<td class="text-center"><%= if task.sixmonthly then 'x' else ' ' end %></td>
<td class="text-center"><%= if task.annually then 'x' else ' ' end %></td>
</tr>
<% end %>
<div class="collapse" id="collapsenewline">
</div>
</tbody>
</table>
</div>
<div class="row">
<div class="col-sm">
<%= link_to 'Add Task', new_task_path(#equipment_type), class: 'btn btn-dark' %>
</div>
<div class="col-sm">
<%= link_to 'Back', equipment_types_path, class: 'btn btn-dark' %>
</div>
<div class="col-sm">
<button class="btn btn-dark" type="button" data-toggle="collapse" data-target="#collapsedownload" aria-expanded="false" aria-controls="collapseExample">
Download
</button>
</div>
</div>
<div class="row">
<div class="col-sm">
</div>
<div class="col-sm">
</div>
<div class="col-sm">
<div class="collapse" id="collapsedownload">
<div class="card card-body">
<h3>Download File</h3>
<%= link_to "csv", equipment_types_path(format: "csv"), class: 'btn btn-dark' %>
<br>
<%= link_to "pdf", equipment_types_path(format: "pdf"), class: 'btn btn-dark' %>
<br>
<%= link_to "word", equipment_types_path(format: "word"), class: 'btn btn-dark' %>
</div>
</div>
</div>
</div>
equipment_type Model
class EquipmentType < ApplicationRecord
has_many :tasks
accepts_nested_attributes_for :tasks
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
EquipmentType.create! row.to_hash
end
end
def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |equipmenttype|
csv << equipmenttype.attributes.values_at(*column_names)
end
end
end
end
equipment_types Controller
class EquipmentTypesController < ApplicationController
before_action :set_equipment_type, only: [:show, :edit, :update, :destroy]
before_action :set_task, only: [:show]
# GET /equipment_types
# GET /equipment_types.json
def index
#equipment_types = EquipmentType.all
respond_to do |format|
format.html
format.csv { send_data #equipment_types.to_csv }
end
end
# GET /equipment_types/1
# GET /equipment_types/1.json
def show
#equipment_type = EquipmentType.find_by(id: params[:id])
#tasks = Task.all
#equip_tasks = #equipment_type.tasks.all
#taskcount = #equip_tasks.count
respond_to do |format|
format.html
format.csv { send_data text: #equip_tasks.to_csv }
end
end
# GET /equipment_types/new
def new
#equipment_type = EquipmentType.new
end
# GET /equipment_types/1/edit
def edit
end
# POST /equipment_types
# POST /equipment_types.json
def create
#equipment_type = EquipmentType.new(equipment_type_params)
respond_to do |format|
if #equipment_type.save
format.html { redirect_to #equipment_type, notice: 'Equipment type was successfully created.' }
format.json { render :show, status: :created, location: #equipment_type }
else
format.html { render :new }
format.json { render json: #equipment_type.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /equipment_types/1
# PATCH/PUT /equipment_types/1.json
def update
respond_to do |format|
if #equipment_type.update(equipment_type_params)
format.html { redirect_to #equipment_type, notice: 'Equipment type was successfully updated.' }
format.json { render :show, status: :ok, location: #equipment_type }
else
format.html { render :edit }
format.json { render json: #equipment_type.errors, status: :unprocessable_entity }
end
end
end
def import
EquipmentType.import(params[:file])
redirect_to equipment_type_path, notice: "Equipment Type Added Successfully"
end
# DELETE /equipment_types/1
# DELETE /equipment_types/1.json
def destroy
#equipment_type.destroy
respond_to do |format|
format.html { redirect_to equipment_types_url, notice: 'Equipment type was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_equipment_type
#equipment_type = EquipmentType.find(params[:id])
end
def set_task
#task = Task.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def equipment_type_params
params.require(:equipment_type).permit(:name, task: [])
end
end
Task Model
class Task < ApplicationRecord
belongs_to :equipment_type
end
Tasks Controller
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
# GET /tasks
# GET /tasks.json
def index
#tasks = Task.all
end
# GET /tasks/1
# GET /tasks/1.json
def show
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks
# POST /tasks.json
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: 'Task was successfully created.' }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1
# PATCH/PUT /tasks/1.json
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task, notice: 'Task was successfully updated.' }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.json
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
#task = Task.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def task_params
params.require(:task).permit(:name, :monthly, :quarterly, :sixmonthly, :annually, :equipment_type_id)
end
end
This should get you started:
You merely need to just append the attributes to the array.
Instead of,
csv << equipmenttype.attributes.values_at(*column_names)
Drop in this (or similar) (note: this is resource-intensive so you'll want to trim it down especially if you have lots of tasks)
def csv_attributes
{
tasks: self.tasks.all.map(&:attributes).map(&:values),
id: self.id
}
end
and then
csv << equipmenttype.csv_attributes
Hello I have a nested database "processing" which belongst to "shopping_process". However I want somehow to see the messages, which are saved in processing. How can I do that? If I go to http://localhost:3000/processings/index the Error: Couldn't find Processing with 'id'=index appears. Has somebody an idea?
processings_controller.rb
class ProcessingsController < ApplicationController
before_action :set_processing, only: [:show, :edit, :update, :destroy]
# GET /processings
# GET /processings.json
def index
#processings = Processing.all
end
# GET /processings/1
# GET /processings/1.json
def show
end
# GET /processings/new
def new
#processing = Processing.new
end
# GET /processings/1/edit
def edit
end
# POST /processings
# POST /processings.json
def create
#processing = Processing.new(processing_params)
respond_to do |format|
if #processing.save
format.html { redirect_to #processing, notice: 'Processing was successfully created.' }
format.json { render :show, status: :created, location: #processing }
else
format.html { render :new }
format.json { render json: #processing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /processings/1
# PATCH/PUT /processings/1.json
def update
respond_to do |format|
if #processing.update(processing_params)
format.html { redirect_to #processing, notice: 'Processing was successfully updated.' }
format.json { render :show, status: :ok, location: #processing }
else
format.html { render :edit }
format.json { render json: #processing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /processings/1
# DELETE /processings/1.json
def destroy
#processing.destroy
respond_to do |format|
format.html { redirect_to processings_url, notice: 'Processing was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_processing
#processing = Processing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def processing_params
params.require(:processing).permit(:shopping_process_id, :shopping_list_id, :accepted, :responded, :message)
end
end
view/processings/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Processings</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #Processing.all.each do |processing| %>
<tr>
<td><%= link_to 'Show', processing %></td>
<td><%= link_to 'Edit', edit_processing_path(processing) %></td>
<td><%= link_to 'Destroy', processing, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Processing', new_processing_path %>
models/processing.rb
class Processing < ApplicationRecord
# db associations
belongs_to :shopping_process
belongs_to :shopping_list
# validations
validates :shopping_list, :presence => true
end
Now here comes the code for the database "shopping_processes"
shopping_processes_controller.rb
class ShoppingProcessesController < ApplicationController
load_and_authorize_resource
layout 'shopping_process', only: [:shopper_show, :senior_show]
# GET /shopping_processes/1
# GET /shopping_processes/1.json
def show
end
# POST /shopping_processes
# POST /shopping_processes.json
def create
#shopping_process.status =nil
#shopping_process.processed = nil
puts params.inspect
respond_to do |format|
if #shopping_process.save
format.html { redirect_to #shopping_process, notice: 'Shopping process was successfully created.' }
format.json { render :show, status: :created, location: #shopping_process }
else
format.html { render :new }
format.json { render json: #shopping_process.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /shopping_processes/1
# PATCH/PUT /shopping_processes/1.json
def update
respond_to do |format|
if #shopping_process.update(shopping_process_params)
format.html { redirect_to #shopping_process, notice: 'Shopping process was successfully updated.' }
format.json { render :show, status: :ok, location: #shopping_process }
else
format.html { render :edit }
format.json { render json: #shopping_process.errors, status: :unprocessable_entity }
end
end
end
# DELETE /shopping_processes/1
# DELETE /shopping_processes/1.json
def destroy
#shopping_process.destroy
respond_to do |format|
format.html { redirect_to shopping_processes_url, notice: 'Shopping process was successfully destroyed.' }
format.json { head :no_content }
end
end
def shopper_show
#shopping_process = ShoppingProcess.find(params[:id])
#users = {}
first = true
#shopping_process.shopping_lists.each do |shopping_list|
user = shopping_list.user
#users[user.id] = {color: (first ? 'blue' : 'yellow'), name: user.firstname + ' ' + user.lastname}
first = false
end
end
def senior_show
#shopping_process = ShoppingProcess.find(params[:id])
#shopping_process.shopping_lists.each do |shopping_list|
#if shopping_list.user == current_user
# #shopping_list = shopping_list
#end
#shopping_list = shopping_list
#to show the correct shopping list uncomment the block above and remove the declartion underneath it
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_shopping_process
#shopping_process = ShoppingProcess.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def shopping_process_params
params.require(:shopping_process).permit(:user_id, :status, :appointed, :processed, :shopping_list_id, processings_attributes: [ :shopping_list_id, :message ])
end
end
views/shopping_processes/form.html.erb
<%= form_for(shopping_process) do |f| %>
<% if shopping_process.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(shopping_process.errors.count, "error") %> prohibited this shopping_process from being saved:</h2>
<ul>
<% shopping_process.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :appointed %>
<%= f.datetime_select :appointed %>
</div>
<div class="field">
<%= f.label :shopper %>
<%= f.text_field :user_id, value: current_user.id, :readonly => true%>
</div>
<div class="field">
<%= f.label :shopping_list_id %>
<%= f.select :shopping_list_id, ShoppingList.accessible_by(current_ability).map{ |sl| [sl.name, sl.id] } %>
</div>
<!-- SUB FORM FOR NESTED RESOURCE : PROCESSINGS -->
<%= f.fields_for :processings, #shopping_process.processings.build do |ff| %>
<div>
<%= ff.label :Begleiter %>
<%= ff.select :shopping_list_id, ShoppingList.all.map{ |sl| [sl.user.firstname, sl.id]} , {:prompt => true} %>
</div>
<div>
<%= ff.label :message %>
<%= ff.text_field :message%>
</div>
<% end %>
<!-- Submit-->
<div class="actions">
<%= f.submit %>
</div>
<% end %>