Say "i want to save my data in database but one error is occur in both controller.
This is my Controller Code.....
class ChiefexecutiveofficerController < ApplicationController
def new
end
def index
#chiefexecutiveofficer = Chiefexecutiveofficer.all
end
def create
#chiefexecutiveofficer = Chiefexecutiveofficer.new()
#chiefexecutiveofficer.ceo_name = params[:chiefexecutiveofficer][:ceo_name]
#chiefexecutiveofficer.ceo_pin = params[:chiefexecutiveofficer][:ceo_pin]
#chiefexecutiveofficer.ceo_address = params[:chiefexecutiveofficer][:ceo_address]
#chiefexecutiveofficer.ceo_salary = params[:chiefexecutiveofficer][:ceo_salary]
if #chiefexecutiveofficer.save
flash[:notice]= " Your CEO Record Is Successfully Created"
redirect_to welcome_index_path
else
flash[:notice]= " Your CEO record Is Not Created"
redirect_to welcome_index_path
end
end
private
def my_params
params.require(:chiefexecutiveofficer).permit(:ceo_name,:ceo_name,:ceo_address,:ceo_salary)
end
end
Related
I'm currently trying to store an updated value in the database.
The process is the following, a user chooses to buy 3 bottles, the stock goes down by -3. The stock is set within the Wines Controller and then displayed in the reservations controller for the user
The current problem is that for each new reservation, the stock get's reset in the reservations controller, since the stock isn't updating in the wines controller.
Here lays the problem:
#reservation.in_stock = wine.in_stock - #reservation.bottle
Here is my full Reservations Controller:
class ReservationsController < ApplicationController
before_action :authenticate_user!
before_action :set_reservation, only: [:approve, :decline]
def create
wine = Wine.find(params[:wine_id])
if current_user == wine.user
flash[:alert] = "Du kannst nicht deinen eigenen Wein kaufen!"
else
start_date = Date.parse(reservation_params[:start_date])
#reservation = current_user.reservations.build(reservation_params)
#reservation.wine = wine
#reservation.price = wine.price
#reservation.in_stock = wine.in_stock - #reservation.bottle
#reservation.total = wine.price * #reservation.bottle
# #reservation.save
if #reservation.save
if wine.Reservieren?
flash[:notice] = "Anfrage versendet!"
else
#reservation.Bearbeitung!
flash[:notice] = "Eroflgreich bestellt!"
end
else
flash[:alert] = "Can't make a reservation!"
end
end
redirect_to wine
end
def your_orders
#orders = current_user.reservations.order(start_date: :asc)
end
def your_reservations
#wines = current_user.wines
redirect_to root_path unless current_user.admin == true
#count = Reservation.count(:all)
#total = Reservation.sum(:total)
end
def your_upcoming_reservations
#wines = current_user.wines
redirect_to root_path unless current_user.admin == true
end
def approve
#reservation.Versendet!
redirect_to your_reservations_path
redirect_to root_path unless current_user.admin == true
end
def decline
#reservation.Abgelehnt!
redirect_to your_reservations_path
redirect_to root_path unless current_user.admin == true
end
# Each Order Details
def order_details
#orders = current_user.reservations.order(start_date: :asc)
end
private
def set_reservation
#reservation = Reservation.find(params[:id])
end
def set_stock
#reservation.in_stock = wine.in_stock
end
def reservation_params
params.require(:reservation).permit(:start_date, :bottle, :in_stock)
end
end
models/collaborators.rb:
class Collaborator < ActiveRecord::Base
belongs_to :user
belongs_to :wiki
def wiki_collaborations
end
end
controllers/wikis_controller.rb:
class WikisController < ApplicationController
def index
#user = current_user
if #user.admin?
#wikis = Wiki.all
elsif #user.premium?
#wikis = Wiki.where(private: false) | #user.wiki_collaborations | #user.wikis
elsif #user.standard?
#wikis = Wiki.where(private: false) | #user.wiki_collaborations
else
#wikis = Wiki.where(private: false)
end
end
def show
#wiki = Wiki.find(params[:id])
authorize #wiki
end
def new
#wiki = Wiki.new
end
def create
#wiki = current_user.wikis.new(wiki_params)
if #wiki.save
flash[:notice] = "Wiki was saved."
redirect_to #wiki
else
flash.now[:alert] = "There was an error saving the wiki. Please try again."
render :new
end
end
def edit
#user = current_user
#wiki = Wiki.find(params[:id])
#user_emails = User.where.not(id: current_user.id || #wiki.users.pluck(:id)).map(&:email)
authorize #wiki
end
def update
#wiki = Wiki.find(params[:id])
authorize #wiki
if #wiki.update(wiki_params)
flash[:notice] = "Wiki was updated."
redirect_to #wiki
else
flash.now[:alert] = "There was an error saving the wiki page. Please try again."
render :edit
end
end
def destroy
#wiki = Wiki.find(params[:id])
if #wiki.destroy
flash[:notice] = "\"#{#wiki.title}\" was deleted successfully."
redirect_to wikis_path
else
flash.now[:alert] = "There was an error deleting the wiki page."
render :show
end
end
private
def wiki_params
params.require(:wiki).permit(:title, :body, :private)
end
end
I tried to access http://localhost:3000/wikis.
I get the following error.
Possibly you've not defined wiki_collaborations method for user.
You can delegate it to User like,
class User < ActiveRecord::Base
delegate :wiki_collaborations, to: :collaborator
end
Btw, be aware of | is not same as ||.
In our Rails app, we have a CalendarsController:
class CalendarsController < ApplicationController
def create
#calendar = current_user.calendars.create(calendar_params)
current_user.add_calendar_and_role(#calendar.id, 'Owner')
if #calendar.save
current_user.total_calendar_count += 1
current_user.owned_calendar_count += 1
current_user.save
flash[:success] = "Calendar created!"
redirect_to dashboard_path
else
render 'static_pages/home'
end
end
def show
#calendar = Calendar.find(params[:id])
#posts = #calendar.posts
#post = Post.new
end
def index
end
def edit
end
def destroy
Calendar.find(params[:id]).destroy
flash[:success] = "Calendar deleted"
redirect_to dashboard_path
end
private
def calendar_params
params.require(:calendar).permit(:name)
end
end
In the create action, when a new #calendar is created, we run #calendar.save to check if the new instance has actually been created, and then perform some actions.
We would like to implement a similar process in our destroy action.
We are thinking of updating the destroy method as follows:
def destroy
#calendar = Calendar.find(params[:id])
#calendar.destroy
if #calendar.delete
flash[:success] = "Calendar deleted"
current_user.total_calendar_count -= 1
if #calendar.administrations.role == "Owner"
current_user.owned_calendar_count -= 1
end
end
redirect_to dashboard_path
end
Is the syntax of this code correct, in particular if #calendar.delete and if #calendar.administrations.role == "Owner"?
And, most importantly, would the code of this destroy action make sense?
Did you think about using the persisted? method
#calendar.destroy
unless #calendar.persisted?
... some code here ....
end
I believe it would be more like:
def destroy
#calendar = Calendar.find(params[:id])
calendar_admin_role = #calendar.administrations.role
if #calendar.destroy
flash[:success] = "Calendar deleted"
current_user.total_calendar_count -= 1
if calendar_admin_role == "Owner"
current_user.owned_calendar_count -= 1
end
end
redirect_to dashboard_path
end
But this is off the top of my head after a long day at work so could be wrong.
So I am having trouble in my sessions controller. I am trying to allow users to signin and businessusers to signin. They both have their own respective model and table in the database. However only users can signin. This is my sessions controller currently. What can I add to the controller to allow businessusers to also signin?
class SessionsController < ApplicationController
def new
end
def create
customer = Customer.find_by_name(params[:name])
if customer && customer.authenticate(params[:password])
session[:customer_id] = customer.id
redirect_to customer
else
flash.now[:notice] = "Invalid name/password combination."
render 'new'
end
end
def destroy
if signed_in?
session[:customer_id] = nil
else
flash[:notice] = "You need to sign in first"
end
redirect_to signin_path
end
end
class SessionsController < ApplicationController
def new
end
def create
customer = Customer.find_by_name(params[:name])
if customer.present? && customer.authenticate(params[:password])
session[:user_type] = 'Customer'
session[:customer_id] = customer.id
redirect_to customer
else
businessuser = BusinessUser.find_by_name(params[:name])
if businessuser.present? && businessuser.authenticate(params[:password])
session[:user_type] = 'Business'
session[:businessuser_id] = businessuser.id
redirect_to businessuser
else
flash.now[:notice] = "Invalid name/password combination."
render 'new'
end
end
end
def destroy
if signed_in?
session[:user_type] = nil
session[:customer_id] = nil
session[:businessuser_id] = nil
else
flash[:notice] = "You need to sign in first"
end
redirect_to signin_path
end
end
I have the following controllers:
1- students_controller.rb
class StudentsController < ApplicationController
def index
#students = Student.all
end
def show
#student = Student.find(params[:id])
end
def new
#student = Student.new
end
def create
#student = Student.new(params[:student])
if #student.save
flash[:notice] = ' Student Record Saved Successfully. Please fill the Parent Details.'
redirect_to new_student_guardian_path(#student.id)
else
flash[:error] = 'An error occurred please try again!'
render 'new'
end
end
def edit
end
end
2- guardians_controller.rb
class GuardiansController < ApplicationController
before_filter :set_student, only: [:new, :create]
def index
#guardian = Guardian.all
end
def show
#guardian = Guardian.find(params[:id])
end
def new
#guardian = Guardian.new
end
def create
#guardian = Guardian.new(params[:guardian])
if #guardian.save
flash[:notice] = ' Parent Record Saved Successfully. Please fill the Additional Details.'
redirect_to new_student_previous_detail_path(#student)
else
flash.now[:error] = 'An error occurred please try again!'
render 'new'
end
end
def edit
end
private
def set_student
#student = Student.find(params[:student_id])
end
end
3- previous_details_controller.rb
class PreviousDetailsController < ApplicationController
before_filter :set_student, only: [:new, :create]
def index
#previous_detail = PreviousDetail.all
end
def show
#previous_detail = PreviousDetail.find(params[:id])
end
def new
#previous_detail = PreviousDetail.new
end
def create
#previous_detail = PreviousDetail.new(params[:previous_detail])
if #previous_detail.save
flash[:notice] = 'Record Saved Successfully.'
# redirect_to user profile page
else
flash.now[:error] = 'An error occurred please try again!'
redirect_to '/student/admission1'
end
end
def edit
end
private
def set_student
#student = Student.find(params[:student_id])
end
end
4- student.rb
class Student < ActiveRecord::Base
after_create :add_to_users
belongs_to :user
accepts_nested_attributes_for :guardians
has_one :previous_detail
accepts_nested_attributes_for :previous_detail
def add_to_users
new_user = User.new
new_user.user_name = self.first_name
new_user.first_name = self.first_name
new_user.last_name = self.last_name
new_user.email = self.email
new_user.password = "123456"
new_user.password_confirmation = "123456"
new_user.user_type_id = 3
new_user.save
end
end
This callback is used to create the student in users model.
How can i tell the previous_details_controller.rb to go to the user profile page(the student who just created and also created in users model using the after create :add_to_user) ?
I have solved my problem and I will add the slolution maybe will be helpful for anyone if needed.
1- Modifying the callback method add_to_user to the following
def add_to_users
new_user = User.new
new_user.user_name = self.first_name
new_user.first_name = self.first_name
new_user.last_name = self.last_name
new_user.email = self.email
new_user.password = "123456"
new_user.password_confirmation = "123456"
new_user.user_type_id = 3
self.user_id = new_user.id
new_user.save
t = Student.find(self.id)
t.user_id = new_user.id
t.save
end
so it will add the student to users model and also will update the user_id in student model to make a match between both.
also i have modified the name of callback above to after_create :add_to_users , on: :create to make sure it will raise on create action only.
2- Modifying the create action in previous_details_controller where i need to redirect to user profile to the following
def create
#previous_detail = PreviousDetail.new(params[:previous_detail])
if #previous_detail.save
flash[:notice] = 'Record Saved Successfully.'
redirect_to user_path(#student.user_id)
else
flash.now[:error] = 'An error occurred please try again!'
redirect_to '/student/admission1'
end
end
after typing all this will redirect me to the User's profile page which belongs to the student who newly added to the system.