Masanobu Mizutani
Masanobu Mizutani

Reputation: 15

No route matches {:action=>"show", :controller=>"waitinglists", :format=>nil, :id=>nil} missing required keys: [:id]

I'm very new to rails. and got this error

No route matches {:action=>"show", :controller=>"waitinglists", :format=>nil, :id=>nil} missing required keys: [:id]

I know why because I didn"t give :id when I want to redirect , but I don"t know how to give it.

In the index action, I"m not using any forms or link_to stuffs because I just want it to redirect automatically using auto refreshing by time proceeding.

What I want to do is redirect from index to show action.

please tell me how to work it.

Controllers

Waitinglists_controller

class WaitinglistsController < ApplicationController
    before_action :authenticate

    def index

        last_group_number = Waitinglist.last.count_number 
        @already_group_people = Waitinglist.where(count_number: last_group_number).count
        @current_person_number = Waitinglist.where(owner_id: current_user.id).last.count_number 
        @current_group_people = Waitinglist.where(count_number: @current_person_number).count 
        current_person_list = current_user.created_waitinglists.last


        redirect_to waitinglists_path if @current_group_people === 4 

            if @already_group_people === 4 
                current_person_list.count_number = last_group_number 
                current_person_list.save
                redirect_to waitinglist_path(@waitinglist, @owner) 

            elsif @already_group_people === 5 
                last_group_number += 1 
                current_person_list.count_number = last_group_number 
                current_person_list.save 
            else
                current_person_list.count_number = last_group_number 
                current_person_list.save
            end
    end

    def new
        @waitinglist = current_user.created_waitinglists.build
    end

    def create
        @waitinglist = current_user.created_waitinglists.build(waitinglist_params)
        if @waitinglist.save
            redirect_to waitinglists_path(@waitinglist, @owner), notice:
        else
            render :new
        end
    end

    def show

        @matched_people = Waitinglist.find(count_number: @current_person_number)

    end 

    def destroy
        @waitnglist = current_user.created_waitinglists.find(params[:id])
        @waitinglist.destroy!
        redirect_to root_path,
    end



    private

    def created_by?(user)
        return false unless user
        owner_id == user.id
    end

    def waitinglist_params
        params.require(:waitinglist).permit(:look_like, :id)
    end
end

application_controller

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception



  helper_method :current_user, :logged_in?

    private

    def current_user
        @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end

    def logged_in?
        !!session[:user_id]
    end

    def authenticate
        return if logged_in?
        redirect_to root_path, alert: 
    end

end

Sessions_controller

class SessionsController < ApplicationController

    def create
        user = User.find_or_create_from_auth_hash(request.env['omniauth.auth'])
        session[:user_id] = user.id
        redirect_to root_path,
    end

    def destroy
        reset_session
        redirect_to root_path, 
    end


end

welcome_controller

class WelcomeController < ApplicationController
  def index
  end
end

Models

User.rb

class User < ActiveRecord::Base
    has_many :created_waitinglists, class_name: 'Waitinglist', foreign_key: :owner_id

    def self.find_or_create_from_auth_hash(auth_hash)
        provider = auth_hash[:provider]
        uid = auth_hash[:uid]
        name = auth_hash[:info][:name]
        image_url = auth_hash[:info][:image]

        User.find_or_create_by(provider: provider, uid: uid) do |user|
            user.nickname = name
            user.image_url = image_url
        end
    end

    def created_by?(user)
        return false unless user
        owner_id == user.id
    end
end

waitinglist.rb

class Waitinglist < ActiveRecord::Base
    belongs_to :waiting_person, class_name: 'User'
    after_initialize :set_default_value

  private
  def set_default_value
    self.count_number  ||= 0
  end
end

Views(only related views)

index.html.erb

<div class="main">

          <p><%= @current_group_people %></p>


<%= link_to 'cancel', new_waitinglist_path, class: 'btn btn-danger btn-lg btn-block', method: :delete, data:  %>

</div>

show.html.erb

<div class="list-group">
<% @matched_people.each do |matched_people| %>
<h4 class="list-group-item-heading">
<%= waiting_person.image_url %>
<%= waiting_person.nickname %>
</h4>
<p class="list-group-item-text">
<%= waitinglist.look_like %>
</p>
<% end %>
</div>

Upvotes: 0

Views: 1839

Answers (1)

Arvoreniad
Arvoreniad

Reputation: 510

If you want to supply a value to a route helper eg. waitinglists_path, just provide it as an argument: so it would be redirect_to waitinglists_path(@waitinglist). @waitinglist could be any Waitinglist object or an ID. In your code, you would use redirect_to waitinglists_path(current_person_list).

Upvotes: 1

Related Questions