Gary Vlc Gary Vlc - 1 month ago 14
Ruby Question

ActiveRecord::RecordNotFound in PermitsController#detail, Couldn't find Permit with 'id'=

Hi guys I keep get this error when i try to access to render out one specific permit data from the permits model to the detail.html.erb, i search through all my code and couldn't find the bug. Can you guys help me to check which part i did wrong? Btw I'm implementing a website using Ruby on rails

This is my permits_controller.rb

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 invoice_path
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.where(:user_id => current_user.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
def detail
@permits = Permit.find(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


Detail.html.erb

<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>

<div class="row">
<div class="col-md-6 col-md-offset-3">


<h2><%= @permits.permitstart %></h2>

</div>
</div>


Route.db

Rails.application.routes.draw do
resources :users
resources :permits do
collection do
post :confirm
end
end
resources :visitor_permits
root 'static_pages#home'

get 'viewpermit' =>'permits#detail'

get 'invoice' => 'permits#invoice'
get 'payment' =>'transaction#new'
get 'show_visitor_permit' =>'visitor_permits#show'
get 'show_permit' =>'permits#show'
get 'visitorpermit' => 'visitor_permits#new'
post 'createpermit' => 'permits#create'
get 'homepage/index'
post 'permits' => 'permits#create'
get 'permitapplication' => 'permits#new'
get 'adminlogin' => 'admin_controller#index'
get 'patrollogin' => 'patrol_officer_controller#index'
get 'createcitation' => 'citations#new'
get 'contact'=> 'static_pages#contact'
get 'about' => 'static_pages#about'
get 'signup' => 'users#new'
get 'help' => 'static_pages#help'
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


This is my linking page (show.html.erb)

<h2>My Permits</h2>
<table>
<tr>
<th>Permit Start Date</th>
<th>Permit End Data</th>
<th>Action</th>
<th> </th>
</tr>
<% @permits.each do |permit| %>

<tr>
<td><%= permit.permitstart %></td>
<td><%= permit.permitend %></td>
<td><%= link_to 'Detail', viewpermit_path(permit) %></td>
<td><%= link_to 'Edit', edit_permit_path(permit) %></td>
</tr>

<% end %>

</table>

Answer

You haven't defined your routes in a proper way. Your routes simply redirects to action #detail but you don't set your variables within the route. You have multiple options

resources :permits do
  collection do
    post :confirm
  end

  member do
    get 'viewpermit' # enables a route like /permits/1/viewpermit
  end
end

# or

get 'viewpermit/:id' => 'permits#detail' #enables route /viewpermit/1