我正在构建一个活动应用,我正在尝试创建从事件显示页面到事件创建者的个人资料的链接,但是我收到以下错误 -
在UsersController#show中的ActiveRecord :: RecordNotFound 找不到'id'= 21
的用户错误突出显示了用户控制器中的这一特定代码行 -
def show
@user = User.find(params[:id])
end
开发日志生成此输出 -
在2016-04-15 12:37:08 +0100开始为:: 1开始获取“/ users / 21” 由UsersController处理#show as HTML 参数:{“id”=>“21”} [1m [36mUser Load(0.1ms)[0m [1mSELECT“users”。* FROM“users”WHERE“users”。“id”=? ORDER BY“users”。“id”ASC LIMIT 1 [0m [[“id”,8]] [1m [35mUser Load(0.2ms)[0m SELECT“users”。* FROM“users”WHERE“users”。“id”=?限制1 [[“id”,21]] 已完成404未在14ms内找到(ActiveRecord:0.9ms)
ActiveRecord :: RecordNotFound(无法找到'id'= 21的用户): app / controllers / users_controller.rb:14:在'show'
用户ID(在这个例子中为5)没有被传递。我在show.html.erb页面尝试了很多参数,但没有一个可以工作。将users控制器中的show参数更改为@user = current_user只会成功显示查看事件的用户的配置文件,而不是事件创建者的配置文件。
这是我的代码 -
事件控制器
class EventsController < ApplicationController
before_action :find_event, only: [:show, :edit, :update, :destroy,]
# the before_actions will take care of finding the correct event for us
# this ties in with the private method below
before_action :authenticate_user!, except: [:index, :show]
# this ensures only users who are signed in can alter an event
def index
if params[:category].blank?
@events = Event.all.order("created_at DESC")
else
@category_id = Category.find_by(name: params[:category]).id
@events = Event.where(category_id: @category_id).order("created_at DESC")
end
# The above code = If there's no category found then all the events are listed
# If there is then it will show the EVENTS under each category only
end
def show
end
def new
@event = current_user.events.build
# this now builds out from a user once devise gem is added
# after initially having an argument of Event.new
# this assigns events to users
end
# both update and create actions below use event_params as their argument with an if/else statement
def create
@event = current_user.events.build(event_params)
# as above this now assigns events to users
# rather than Event.new
if @event.save
redirect_to @event, notice: "Congratulations, you have successfully created a new event."
else
render 'new'
end
end
def edit
# edit form
# @edit = Edit.find(params[:id])
@event = current_user.events.find(params[:id])
end
def update
if @event.update(event_params)
redirect_to @event, notice: "Event was successfully updated!"
else
render 'edit'
end
end
def destroy
@event.destroy
redirect_to root_path
end
private
def event_params
params.require(:event).permit(:title, :location, :date, :time, :description, :number_of_spaces, :is_free, :price, :organised_by, :organiser_profile, :url, :image, :category_id)
# category_id added at the end to ensure this is assigned to each new event created
end
def find_event
@event = Event.find(params[:id])
end
端
用户控制器 -
class UsersController < ApplicationController
before_action :authenticate_user!
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to Mama Knows Best"
session[:uid] = @user.id
redirect_to root_path
else
render 'new'
end
end
def edit
@user = current_user
end
def update
@user = current_user
if @user.update(user_params)
flash[:success] = "Profile successfully updated!"
redirect_to root_path
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :username, :biography, :email, :url)
end
end
显示页面 -
<%= image_tag @event.image.url %>
<h1><%= @event.title %></h1>
<p>Location </p>
<p><%= @event.location %></p>
<p>Date</p>
<p><%= @event.date.strftime('%A, %d %b %Y') %></p>
<p>Time</p>
<p><%= @event.time.strftime('%l:%M %p') %></p>
<!-- above expresses date and time as per UK expectations -->
<p>More details</p>
<p><%= @event.description %></p>
<p>Number of Spaces available</p>
<p><%= @event.number_of_spaces %></p>
<% if @event.is_free? %>
<p>This is a free event</p>
<% else %>
<p>Cost per person</p>
<p><%= @event.price %></p>
<% end %>
<p>Organiser</p>
<p><%= @event.organised_by %></p>
<p>Organiser Profile</p>
<button><%= link_to "Profile", user_path %></button>
<p>Link to Organiser site</p>
<button><%= link_to "Organiser site", @event.url %></button>
<p>Submitted by</p>
<p><%= @event.user.name %></p>
<% if user_signed_in? and current_user == @event.user %>
<%= link_to "Edit", edit_event_path %>
<%= link_to "Delete", event_path, method: :delete, data: { confirm: "Are you sure?"} %>
<%= link_to "Back", root_path %>
<% else %>
<%= link_to "Back", root_path %>
<%= link_to "Book the Event", new_event_booking_path(@event) %>
<% end %>
路线 -
Rails.application.routes.draw do
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users
resources :events do
resources :bookings
end
# get 'welcome/index'
authenticated :user do
root 'events#index', as: "authenticated_root"
end
root 'welcome#index'
# the above method comes from devise and allows for the site to have a home page
# for users not signed in and one for when they are signed in
end
我没有在表单部分添加与用户个人资料相关的任何内容,因为我认为它不相关。任何帮助将不胜感激。
答案 0 :(得分:1)
user_path
是Rails中的路径助手,它解析为/users/:id
的RESTful路由。这在UserController#show
中,并且期望params
哈希包含:id
。
对于你的情况,你错过了这个论点。你需要这样做:
<button><%= link_to "Profile", user_path(current_user) %></button>
它自动获取id并将其传递给params hash:{:id => 7}
Doc
您可能还想修复其他此类助手调用:
event_path
edit_event_path
有适当的论据。
答案 1 :(得分:1)
要重申您的问题,您希望活动页面上的链接转到活动组织者的个人资料页面?
var m1 = new TreeModel() { ID = 1 };
var m2 = new TreeModel() { ID = 2 };
var m21 = new TreeModel() { ID = 21 };
var m22 = new TreeModel() { ID = 22 };
var m3 = new TreeModel() { ID = 3 };
m1.Children.Add(m2);
m2.Children.Add(m21);
m2.Children.Add(m22);
m1.Children.Add(m3);
var parent = m1.GetParent(p => p.ID == 22); //<-- How?
答案 2 :(得分:0)
您使用什么进行用户身份验证,设计或类似的gem?你有自己的建造吗?如果是,您是否在会话助手中定义了current_user?下面的代码是如何定义current_user的(la Hartl Rails教程)。这将允许您在视图和控制器中使用current_user。
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end
我也在def创建的用户控制器中注意到了。我认为它应该是session [:id]而不是session [:uid]。如果不是这样,请原谅。希望这会有所帮助。