我正在尝试使用acts_as_follower工作,但我肯定在路线上做错了。
添加了宝石:
gem 'acts_as_follower
在用户模型中:
acts_as_follower
acts_as_followable
在Post模型中:
acts_as_followable
在我的页面控制器中:
class PageController < ApplicationController
def index
end
def profile
@posts = Post.all.order("created_at desc").where( "user_id = ?", User.find_by_id(params[:id]) )
@post = Post.new
end
def feed
@posts = Post.all.order("RANDOM()")
end
def home
end
def follow
@user = User.find(params[:id])
current_user.follow(@user)
end
def unfollow
@user = User.find(params[:id])
current_user.stop_following(@user)
end
def block
@user = User.find(params[:id])
@post.block(@user)
end
end
在我的路线中:
Rails.application.routes.draw do
resources :posts
devise_for :users
root 'page#index'
get '/users/:id' => 'page#profile'
get 'page/feed'
get 'page/home'
resources :users do
get :follow
get :unfollow
end
end
问题是,即使Users表(设计)没有user_id列,我也总是遇到异常错误,user_id设置为nil?
为什么在我的控制器中定义@user时会搜索user_id?
routes.rb中的问题是什么?
更新
Full Post模特:
class Post < ActiveRecord::Base
belongs_to :user
mount_uploader :avatar, AvatarUploader
acts_as_followable
end
完整用户模型:
class User < ActiveRecord::Base
has_many :posts
acts_as_follower
acts_as_followable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
完整跟踪日志:
app/views/page/profile.html.erb:3:in `_app_views_page_profile_html_erb__881455127_39547752'
actionview (4.1.8) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.8) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:143:in `render'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.8) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.8) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.8) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.8) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.8) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.8) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.8) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.8) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.1.8) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (1.5.5) lib/rack/etag.rb:23:in `call'
rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.5) lib/rack/head.rb:11:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.5) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
rack (1.5.5) lib/rack/content_length.rb:14:in `call'
rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
个人资料视图:
<%= link_to "Back", root_path %>
<%= link_to "Follow", follow_user_path(@user) %>
<br>
<br>
<%= render 'posts/form' %>
<br>
<br>
<% @posts.each do |post| %>
<p><%= post.description %>, <%= time_ago_in_words(post.created_at) %> ago</p>
<%= image_tag post.avatar, :class => "tumbnial" %>
<br>
<% if user_signed_in? %>
<% if post.user_id == current_user.id %>
<%= link_to "Delete post", post_path(post), :method => :delete %>
<% end %>
<% end %>
<% end %>
答案 0 :(得分:0)
@Raymond看起来错过了@user
动作中profile
个实例的初始化:
def profile
@user = User.find(params[:id])
@posts = Post.all.order("created_at desc").where(user_id: @user.id)
@post = Post.new
end
另请注意,当您使用find_by_id(...)
查找用户时,您将获得nil,如果找不到用户,则下一代码中的行为可能不正确。最好使用会引发异常的find(...)
并且你可以以某种方式处理它,例如通过重定向到某个带有关于它的flash消息的页面,或者你可以使用find_by_id
来做同样的事情,但要确保找到用户
最好将此功能提取到某些before_action,例如:
class PageController < ApplicationController
before_action :load_user, only: [:profile, :follow, :unfollow, :block]
def profile
@posts = Post.all.order("created_at desc").where(user_id: @user.id)
@post = Post.new
end
def feed
@posts = Post.all.order("RANDOM()")
end
def home; end
def follow
current_user.follow(@user)
end
def unfollow
current_user.stop_following(@user)
end
def block
@post.block(@user)
end
private
def load_user
@user = User.find_by_id(params[:id])
# here you can redirect to some place with flash message if @user.blank?
end
end