Biggztyle
Biggztyle

Reputation: 111

RoR - uninitialized constant ToursController & not able to assign ID in def show, def create

Issue is flagged with couldnt find id=create - the framework is not creating any tours with specific id.

This issue has been troubling for some time and decided to start from scratch again to see if I missed anything. I am new to the RoR framework and it has been a great learning curve already.

Project: Tour website which allows host to post tours Aim: To assign 'tours' to a host (which is using the Devise Gem) Current issue: Am not able to assign id's to a create tour and not be able to see any tours being saved or created.

Attached are my file snapshots which have been updated as per previous user answer:

routes.rb

Rails.application.routes.draw do

  devise_for :host
  # devise_for :hosts

  resources :host, only: [:show]
  resources :tour

  #Root
  root 'static_pages#home'

  #Tour
  get 'tour/index'
  get 'tour/new'
  get 'tour/create'
  #get 'tours/:id'
  patch 'tour/update'
  get 'tour/show'
  get 'tour/edit'
  get 'tour/delete'
  get 'tour/update'

  #Static Pages 

  get 'about' => 'static_pages#about'
  get 'contact' => 'static_pages#contact'

Rake routes

    Prefix Verb   URI Pattern                   Controller#Action
        new_host_session GET    /host/sign_in(.:format)       devise/sessions#new
            host_session POST   /host/sign_in(.:format)       devise/sessions#create
    destroy_host_session DELETE /host/sign_out(.:format)      devise/sessions#destroy
           host_password POST   /host/password(.:format)      devise/passwords#create
       new_host_password GET    /host/password/new(.:format)  devise/passwords#new
      edit_host_password GET    /host/password/edit(.:format) devise/passwords#edit
                         PATCH  /host/password(.:format)      devise/passwords#update
                         PUT    /host/password(.:format)      devise/passwords#update
cancel_host_registration GET    /host/cancel(.:format)        devise/registrations#cancel
       host_registration POST   /host(.:format)               devise/registrations#create
   new_host_registration GET    /host/sign_up(.:format)       devise/registrations#new
  edit_host_registration GET    /host/edit(.:format)          devise/registrations#edit
                         PATCH  /host(.:format)               devise/registrations#update
                         PUT    /host(.:format)               devise/registrations#update
                         DELETE /host(.:format)               devise/registrations#destroy
                    host GET    /host/:id(.:format)           host#show
              tour_index GET    /tour(.:format)               tour#index
                         POST   /tour(.:format)               tour#create
                new_tour GET    /tour/new(.:format)           tour#new
               edit_tour GET    /tour/:id/edit(.:format)      tour#edit
                    tour GET    /tour/:id(.:format)           tour#show
                         PATCH  /tour/:id(.:format)           tour#update
                         PUT    /tour/:id(.:format)           tour#update
                         DELETE /tour/:id(.:format)           tour#destroy
                    root GET    /                             static_pages#home
                         GET    /tour/index(.:format)         tour#index
                tour_new GET    /tour/new(.:format)           tour#new
             tour_create GET    /tour/create(.:format)        tour#create
             tour_update PATCH  /tour/update(.:format)        tour#update
               tour_show GET    /tour/show(.:format)          tour#show
               tour_edit GET    /tour/edit(.:format)          tour#edit
             tour_delete GET    /tour/delete(.:format)        tour#delete
                         GET    /tour/update(.:format)        tour#update
                   about GET    /about(.:format)              static_pages#about
                 contact GET    /contact(.:format)            static_pages#contact

tour_controller.rb

This is where I am getting the main problem with the def show or create id

    class TourController < ApplicationController
   #before_action :set_tour, only: [:show, :edit, :update, :create]
   before_action :authenticate_host!, except: [:create]
   before_action :host_session

   def index
        @tours = Tour.all
   end

   def show
        @tour = Tour.find(params[:id])
   end

   def new
        @tour = Tour.new
   end

    def create
     @tour = Tour.new(tour_params)
     @tour.host = current_host
     @tour.save
    end

   def edit
        @tour = Tour.find(params[:id])
   end

   def update
        @tour = Tour.find(params[:id])

   if @tour.update_attributes(tour_param)
      redirect_to :action => 'show', :id => @tour
   else
      render :action => 'edit'
   end
   end

   def delete
          Tour.find(params[:id]).destroy
   redirect_to :action => 'index'
   end

   def set_tour
      @tour = Tour.find_by_id(params[:id])
   end

private

   def tour_params
      #params.require(:tour_details).permit(:host_id) if params[:tour_details]
      params.require(:tour).permit(:host_id) if params[:tour]
   end
end

_form.html erb

Form being used for creating the tours, although in my database migration I am using a string field for the title - but RoR throws out this message when I use the f.string ... so I always change it back to f.text_field

undefined method `string' for # Did you mean? String

 <h1>Add new tour</h1>

<%= form_for @tour do |f| %>

  <div class="form-group">
  <%= f.label :title %>
  <%= f.text_field :title %>
  <%= f.label :description %>
  <%= f.text_field :description%>
  </div>

  <%= f.submit "Create" %>

<% end %>

<%= link_to 'Back', {:action => 'index'} %>

show.html.erb

This is my basic setup now but need to move it on when I can get the ID working with the tours.

<h1>show tour details</h1>

<p>
  <strong>Title:</strong>
  <h4><%= @tour.title %></h4>
</p>

<%= link_to 'Back' %>

schema.rb

ActiveRecord::Schema.define(version: 20160711094000) do

  create_table "hosts", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "hosts", ["email"], name: "index_hosts_on_email", unique: true
  add_index "hosts", ["reset_password_token"], name: "index_hosts_on_reset_password_token", unique: true

  create_table "tours", force: :cascade do |t|
    t.string   "title"
    t.float    "price"
    t.integer  "tour_id"
    t.text     "description"
    t.datetime "created_at"
    t.integer  "host_id"
  end

end

New Error at /tour/create

ActiveRecord::RecordNotFound at /tour/create Couldn't find Tour with 'id'=create

snapshot from tour_controller which is highlighted from better errors

def show @tour = Tour.find(params[:id])

{"controller"=>"tour", "action"=>"show", "id"=>"create"}

Instance variables - @_routes nil

After I do the action create the following occurs: ActionController::RoutingError at /tours uninitialized constant ToursController

So to some up the basics of it. - Host signs in (which is working with devise and provide as host.id) - Create a tour (basic for now with only a title and description) - The tour is assigned a unique id under the current host id. - To create a tour with unique id

Upvotes: 0

Views: 74

Answers (1)

oreoluwa
oreoluwa

Reputation: 5633

You error is being caused because of the custom routes you defined.

resources :tours already defined all 7 RESTful routes for tours.

A way to immediately fix is to change yours routes like this:

Rails.application.routes.draw do

  devise_for :host
  # devise_for :hosts

  resources :host, only: [:show]
  resources :tour

  #Root
  root 'static_pages#home'
  #Static Pages 

  get 'about' => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
end

Now to visit the new action route, you should visit: localhost:3000/tours/new

The error you encountered initially was because you were trying to visit localhost:3000/tours/create, which because of the resources :tours, would map to your tours#show action. Hence the reason your application saw it as id=create.

I'd recommend you read the Hartl Tutorials for an in-depth knowledge of the Ruby on Rails framework. You'd be better for reading it.

Hope I was able to help

Upvotes: 0

Related Questions