我对铁杆很新。 并得到此错误
没有路线匹配{:action =>" show",:controller =>" waitinglists", :format => nil,:id => nil}缺少必需的键:[:id]
我知道为什么,因为当我想重定向时,我没有给出:id,但我不知道如何给它。
在索引操作中,我没有使用任何表单或link_to内容,因为我只是想让它在时间过程中使用自动刷新自动重定向。
我想要做的是从索引重定向到show action。
请告诉我如何使用它。
控制器
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
模型
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
观看次数(仅限相关观点)
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>
答案 0 :(得分:1)
如果您想为路线助手提供价值,例如。 waitinglists_path
,只需将其作为参数提供:所以它将是redirect_to waitinglists_path(@waitinglist)
。 @waitinglist
可以是任何Waitinglist
个对象或ID。在您的代码中,您将使用redirect_to waitinglists_path(current_person_list)
。