【发布时间】:2016-08-06 10:49:40
【问题描述】:
我正在构建一个活动应用程序,我正在尝试创建一个从活动显示页面到活动创建者个人资料的链接,但我收到以下错误 -
ActiveRecord::RecordNotFound in UsersController#show 找不到 'id'=21 的用户
错误突出显示用户控制器中的这一特定代码行 -
def show
@user = User.find(params[:id])
end
开发日志产生这个输出 -
于 2016 年 4 月 15 日 12:37:08 +0100 开始为 ::1 获取“/users/21” 由 UsersController#show 处理为 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]] Completed 404 Not Found in 14ms (ActiveRecord: 0.9ms)
ActiveRecord::RecordNotFound(找不到 'id'=21 的用户): app/controllers/users_controller.rb:14:in `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
我没有在部分表单上添加与用户个人资料相关的任何内容,因为我认为它不相关。任何帮助将不胜感激。
【问题讨论】:
-
Event是否与User有任何关联? -
<button><%= link_to "Profile", user_path %></button>user_path` 是解析为UserController#show并期望一个参数的助手。做user_path(current_user) -
我试过 current_user 还是不行。
-
是的,用户有_许多事件和事件属于_用户
标签: ruby-on-rails ruby activerecord