我对rails很陌生。得到这个错误
没有路由匹配{:action=>"show",:controller=>"waitinglists",:format=>nil,:id=>nil}缺少所需密钥:[:id]
我知道为什么,因为当我想重定向时,我没有给出:id,但我不知道如何给出。
在索引操作中,我不使用任何表单或link_to填充,因为我只想让它通过时间进程自动刷新来自动重定向
我想做的是从索引重定向到显示操作。
请告诉我如何操作。
控制器
等待列表_控制器
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
应用程序控制器
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
会话控制程序
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
型号
用户.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
等待列表.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
视图(仅相关视图)
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>
如果您想为路由助手(例如waitinglists_path
)提供一个值,只需将其作为参数提供即可:因此它将是redirect_to waitinglists_path(@waitinglist)
。@waitinglist
可以是任何Waitinglist
对象或ID。在代码中,您将使用redirect_to waitinglists_path(current_person_list)
。