Gary Vlc
Gary Vlc

Reputation: 143

NoMethodError, undefined method `permits_path' for #<#<Class:0xbbb3ed0>:0xbbb34b0>

I'm new to ROR. I'm trying to create a page for parking permit application. I encountered this problementer image description here

I couldn't find the problem. Or maybe i missed something. Any help is appreciated.

This is my permit_controller.rb

class PermitController < ApplicationController
  before_action :set_permit, only: [:show, :destroy]
  def index
    @permit = Permit.all
  end

  def new
    @permit = Permit.new
  end

  def create
    @permit = Permit.new(user_params)
    if @permit.save
      redirect_to root_path
    else
      flash[:success] = "Welcome to your profile!"
    end
  end

  def destroy
  end

  def show
    @permit = Permit.find(params[:id])
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_permit
    @permit = 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

This is my permit/new.html.erb

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

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@permit) do |f| %>

        <%= render 'shared/error_messages' %>
        <%= f.label :"Vehicle Type" %>
        <%= f.text_field :vehicle_type, class: 'form-control' %>

        <%= f.label :name %>
        <%= f.text_field :name, class: 'form-control' %>

        <%= f.label :"Student ID" %>
        <%= f.text_field :studentid, class: 'form-control' %>

        <%= f.label :department %>
        <%= f.text_field :department, class: 'form-control' %>

        <%= f.label :"Car Plate" %>
        <%= f.text_field :carplate, class: 'form-control' %>

        <%= f.submit "Confirm", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

This is my route.rb

Rails.application.routes.draw do
  resources :users
  resources :permit
  get 'permit/destroy'

  get 'permit/show'

  root 'static_pages#home'

  get 'homepage/index'
  post 'permit' => 'permit#create'
  get 'permitapplication' => 'permit#new'
  get 'adminlogin' => 'admin_controller#index'
  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

Upvotes: 1

Views: 85

Answers (3)

Al Graven
Al Graven

Reputation: 56

The biggest issue here is you don't have permit/new in your routes.rb file.

As has already been suggested, it might be better for you to leverage rails with a resources call

in routes.rb

resources :permits

and remove lines

get 'permit/destroy'
get 'permit/show'

etc.

Upvotes: 2

Tass
Tass

Reputation: 1628

I'll attempt to consolidate our various answers and comments.

To solve your current problem, in config/routes.rb, change resources :permit to resources :permits. This exposes all seven RESTful routes for use in your application. (This also makes obsolete the custom permit routes, unless you're explicitly calling them from within their respective forms.) Information about RESTful routes/resources here: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

Why is this valuable?

It's how your application knows to use controller actions in combination with views (and therefore forms). Say you have an edit action in your permits controller and app/views/permits/edit.html.erb has a form. Within this page's edit form, you need only have form_for @permit and Rails does all the rest. It knows you're using this particular route. I recommend you read about routing within Rails.

Please keep in mind Ruby on Rails has been carefully crafted to make things easy for you, the developer.

Upvotes: 0

Bartłomiej Gładys
Bartłomiej Gładys

Reputation: 4615

Instead resources :permit use resources :permits.

Upvotes: 2

Related Questions