Reputation: 143
I'm new to ROR. I'm implementing a website that create a permit then after that will redirect the user to an invoice page. I have a user model, invoice model and permit model which they have association with each other below:
invoice.rb
class Invoice < ApplicationRecord
belongs_to :user
has_one :receipt
belongs_to :permit
end
user.rb
class User < ApplicationRecord
has_many :permits
has_many :visitor_permits
has_many :healthreports
has_secure_password
end
permit.rb
class Permit < ApplicationRecord
belongs_to :user
has_one :invoice
end
What I going to do is the user fill in all the details when they applying for permits and the information will save into the permit model. After that, user will be redirect to invoice page which render all the data which just filled from the user in the permit application page and extract it from permit model and display on the invoice page which after that will be save again into the invoice page. I have no idea on how to start it. Any idea on how to do it?
I have permit and invoice controller. If you guys need any more information or my code feel free to tell me. Thanks!
What I have tried is creating a variable linked with the permits model in my invoice controller NEW action so I can use it to render the data from Permit model and then save it to invoice model.
class InvoiceController < ApplicationController
def new
@permits = Permit.find(params[:id])
@invoice = Invoice.new
end
def create
#@current_user = User.find_by(id: session[:user_id])
@invoice = current_user.visitor_permits.invoices.build(invoice_params)
if @invoice.save
else
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invoice
@invoice = Invoice.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:vehicle_type, :name, :department, :carplate, :duration, :permitstart, :permitend, :price, :time)
end
end
But I keep getting the error
ActiveRecord::RecordNotFound in InvoiceController#new
Couldn't find Permit with 'id'=
which point to the NEW action.
@permits = Permit.find(params[:id])
This is my permits_controller.rb which will redirect user in the CREATE action once the button pressed
class PermitsController < ApplicationController
before_action :set_permit, only: [:destroy]
def index
@permits = Permit.where(:user_id => current_user.id)
end
def new
@permits = Permit.new
end
def create
@permits = current_user.permits.build(permit_params)
if @permits.save
redirect_to createinvoice_path(@permits)
else
render 'new'
end
end
def destroy
Permit.destroy_all(user_id: current_user)
respond_to do |format|
format.html { redirect_to root_path, notice: 'Permit was successfully canceled.' }
format.json { head :no_content }
end
end
def confirm
@fields = %i[vehicle_type, carplate, studentid, name, department, permitstart, permitend]
@permit = current_user.permits.build(permit_params)
render :new and return unless @permit.valid?
end
def show
@permits = Permit.find(params[:id])
end
def update
@permits = Permit.where(user_id: current_user).take
respond_to do |format|
if @permits.update(permit_params)
format.html { redirect_to root_path}
flash[:success] = "Permit successfully updated"
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def edit
@permits = Permit.find(params[:id])
#@permits = Permit.find_or_initialize_by(user_id: params[:id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_permit
@permits = Permit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def permit_params
params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate, :duration, :permitstart, :permitend)
end
end
Route.db
Rails.application.routes.draw do
resources :users
resources :permits do
collection do
post :confirm
end
end
resources :visitor_permits do
collection do
post :confirm
end
end
resources :invoice
resources :receipt
resources :health_report
root 'static_pages#home'
get 'permitapplication' => 'permits#new'
get 'viewpermit' =>'permits#show'
get 'show_permit' =>'permits#index'
get 'invoice' => 'permits#invoice'
post 'createpermit' => 'permits#create'
post 'permits' => 'permits#create'
post 'createvpermit' => 'visitor_permits#create'
post 'vpermits' => 'visitor_permits#create'
get 'show_vpermit' =>'visitor_permits#index'
get 'show_visitor_permit' =>'visitor_permits#show'
get 'visitorpermit' => 'visitor_permits#new'
get 'createinvoice' => 'invoice#new'
post 'invoice' => 'invoice#create'
get 'new_health_report' => 'health_report#new'
get 'payment' =>'transaction#new'
get 'homepage/index'
get 'adminlogin' => 'admin_controller#index'
get 'patrollogin' => 'patrol_officer#index'
post 'citations' => 'citations#create'
get 'new_citation' => 'citations#new'
get 'unpaid_citations' => 'citations#list_all'
get 'edit_Citation' => 'citations#edit'
get 'contact'=> 'static_pages#contact'
get 'about' => 'static_pages#about'
get 'help' => 'static_pages#help'
get 'signup' => 'users#new'
post 'users' => 'users#create'
get 'login' => 'sessions#new' #Page for a new session
post 'login' => 'sessions#create' #Create a new session
delete 'logout'=>'sessions#destroy' #Delete a session
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Upvotes: 0
Views: 44
Reputation: 1923
I see that your createinvoice
route is not setup to accept any extra parameter. default route should have generated something like invoice/new
. So while sending @permit
id in path you need something like:
redirect_to createinvoice_path(permit_id: @permits.id)
and in invoice_controller
fetch params[:permit_id]
EDIT:
I am not good at explaining but I will try.
your route path for invoice is get createinvoice
so when someone visits yourdomain.com/createinvoice
it will take to that page.
Notice that your route get createinvoice
does not contain any placeholder for any params, something you see in rails default generated show page like get users/:id
. If the route setup is like this then when we write redirect_to users_path(@user)
, rails will take the id
of @user
and replace it in url
and it will become like users/3
when page renders if users id is 3 and in controller you can access that id by params[:id]
. But in your case route is not setup to accept any params so rails is simply ignoring the @permits
inside the createinvoice_path
and you are getting error since there is no id
is present in params
.
What I suggested is createinvoice_path(permit_id: @permits.id)
which will make the url as createinvoice/?permit_id=3
if the id of permits is 3. So this will send the params as a query params like you see while you search in google. And you will get the permit_id
in params
.
Hope you got my point.
Upvotes: 1