所以我正在进行一项任务,我正在尝试显示收藏的帖子。我目前已经显示了收藏的帖子,但是当我点击它时,它并没有将我重定向到任何地方。
以下是我目前的代码:
用户#显示我目前正在尝试显示收藏的帖子:
<div class="row">
<div class="col-md-8">
<div class="media">
<br />
<% avatar_url = @user.avatar_url(128) %>
<% if avatar_url %>
<div class="media-left">
<%= image_tag avatar_url, class: 'media-object' %>
</div>
<% end %>
<div class="media-body">
<h2 class="media-heading"><%= @user.name %></h2>
<small>
<%= pluralize(@user.posts.count, 'post') %>,
<%= pluralize(@user.comments.count, 'comment') %>
</small>
</div>
</div>
</div>
</div>
<h2>Posts</h2>
<%= posts_exists? %>
<%= render @user.posts %>
<h2>Comments</h2>
<%= comments_exists? %>
<%= render @user.comments %>
<h2>Favorites</h2>
<% @posts.each do |post| %>
<%= render partial: 'votes/voter', locals: { post: post } %>
<%= link_to post.title, topic_post_path(@topic, post) %>
<%= image_tag current_user.avatar_url(48), class: "gravatar" %>
<%= post.comments.count %> Comments
<% end %>
错误发生在以下行:
<%= link_to post.title, topic_post_path(@topic, post) %>
以下是错误输出:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"posts", :id=>"54", :topic_id=>nil} missing required keys: [:topic_id]):
29: <h2>Favorites</h2>
30: <% @posts.each do |post| %>
31: <%= render partial: 'votes/voter', locals: { post: post } %>
32: <%= link_to post.title, topic_post_path(@topic, post) %>
33: <%= image_tag current_user.avatar_url(48), class: "gravatar" %>
34: <%= post.comments.count %> Comments
35: <% end %>
app/views/users/show.html.erb:32:in `block in _app_views_users_show_html_erb__1919900632491741904_70127642538380'
app/views/users/show.html.erb:30:in `_app_views_users_show_html_erb__1919900632491741904_70127642538380'
显然Topid.id是零,但我无法弄清楚原因。我会为你提供我认为你需要的一切吗?我知道这可能是一个简单的小说问题,但我已经坚持了将近一整天。
这是我的用户#Controller:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new
@user.name = params[:user][:name]
@user.email = params[:user][:email]
@user.password = params[:user][:password]
@user.password_confirmation = params[:user][:password_confirmation]
if @user.save
flash[:notice] = "Welcome to Bloccit #{@user.name}!"
create_session(@user)
redirect_to root_path
else
flash[:error] = "There was an error creating your account. Please try again."
render :new
end
end
def show
@user = User.find(params[:id])
@posts = @user.posts.visible_to(current_user)
@posts = Post.joins(:favorites).where('favorites.user_id = ?', @user.id)
@favorites = current_user.favorites
end
end
这是我的Post#Controller:
class PostsController < ApplicationController
before_action :require_sign_in, except: :show
before_action :authorize_user, except: [:show, :new, :create]
def show
@post = Post.find(params[:id])
end
def new
@topic = Topic.find(params[:topic_id])
@post = Post.new
end
def create
@topic = Topic.find(params[:topic_id])
@post = @topic.posts.build(post_params)
@post.user = current_user
if @post.save
@post.labels = Label.update_labels(params[:post][:labels])
flash[:notice] = "Post was saved."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
@post.assign_attributes(post_params)
if @post.save
@post.labels = Label.update_labels(params[:post][:labels])
flash[:notice] = "Post was updated."
redirect_to [@post.topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :edit
end
end
def destroy
@post = Post.find(params[:id])
if @post.destroy
flash[:notice] = "\"#{@post.title}\" was deleted successfully."
redirect_to @post.topic
else
flash[:error] = "There was an error deleting the post."
render :show
end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
def authorize_user
post = Post.find(params[:id])
unless current_user == post.user || current_user.admin?
flash[:error] = "You must be an admin to do that."
redirect_to [post.topic, post]
end
end
end
这是我的主题#Controller:
class TopicsController < ApplicationController
before_action :require_sign_in, except: [:index, :show]
before_action :authorize_user, except: [:index, :show]
def index
@topics = Topic.all
end
def show
@topic = Topic.find(params[:id])
end
def new
@topic = Topic.new
end
def create
@topic = Topic.new(topic_params)
if @topic.save
@topic.labels = Label.update_labels(params[:topic][:labels])
redirect_to @topic, notice: "Topic was saved successfully."
else
flash[:error] = "Error creating topic. Please try again."
render :new
end
end
def edit
@topic = Topic.find(params[:id])
end
def update
@topic = Topic.find(params[:id])
@topic.assign_attributes(topic_params)
if @topic.save
@topic.labels = Label.update_labels(params[:topic][:labels])
flash[:notice] = "Topic was updated."
redirect_to @topic
else
flash[:error] = "Error saving topic. Please try again."
render :edit
end
end
def destroy
@topic = Topic.find(params[:id])
if @topic.destroy
flash[:notice] = "\"#{@topic.name}\" was deleted successfully."
redirect_to action: :index
else
flash[:error] = "There was an error deleting the topic."
render :show
end
end
private
def topic_params
params.require(:topic).permit(:name, :description, :public)
end
def authorize_user
unless current_user.admin?
flash[:error] = "You must be an admin to do that."
redirect_to topics_path
end
end
end
这是我的用户模型:
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :votes, dependent: :destroy
has_many :favorites, dependent: :destroy
before_save { self.email = email.downcase }
before_save { self.role ||= :member }
EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :name, length: { minimum: 1, maximum: 100 }, presence: true
validates :password, presence: true, length: { minimum: 6 }, if: "password_digest.nil?"
validates :password, length: { minimum: 6 }, allow_blank: true
validates :email,
presence: true,
uniqueness: { case_sensitive: false },
length: { minimum: 3, maximum: 100 },
format: { with: EMAIL_REGEX }
has_secure_password
enum role: [:member, :admin]
def favorite_for(post)
favorites.where(post_id: post.id).first
end
def avatar_url(size)
gravatar_id = Digest::MD5::hexdigest(self.email).downcase
"http://gravatar.com/avatar/#{gravatar_id}.png?s=#{size}"
end
end
这是我的主题模型:
class Topic < ActiveRecord::Base
has_many :posts, dependent: :destroy
has_many :labelings, as: :labelable
has_many :labels, through: :labelings
end
这是我的帖子模型:
class Post < ActiveRecord::Base
belongs_to :topic
belongs_to :user
has_many :comments, dependent: :destroy
has_many :votes, dependent: :destroy
has_many :labelings, as: :labelable
has_many :labels, through: :labelings
has_many :favorites, dependent: :destroy
default_scope { order('rank DESC') }
scope :visible_to, -> (user) { user ? all : joins(:topic).where('topics.public' => true) }
validates :title, length: { minimum: 5 }, presence: true
validates :body, length: { minimum: 20 }, presence: true
validates :topic, presence: true
validates :user, presence: true
def up_votes
votes.where(value: 1).count
end
def down_votes
votes.where(value: -1).count
end
def points
votes.sum(:value)
end
def update_rank
age_in_days = (created_at - Time.new(1970,1,1)) / 1.day.seconds
new_rank = points + age_in_days
update_attribute(:rank, new_rank)
end
end
任何人都可以提供任何见解,我将非常感激。如果你有时间解释我哪里出错了,那会更有帮助。
答案 0 :(得分:1)
用户#显示我目前正在尝试显示收藏的帖子
但您没有在User#show action中设置@topic
。这就是为什么它没有。
def show
@user = User.find(params[:id])
@posts = @user.posts.visible_to(current_user)
@posts = Post.joins(:favorites).where('favorites.user_id = ?', @user.id)
@favorites = current_user.favorites
# your @topic object is not in here?
end
由于帖子belongs_to
是一个话题,你可以这样做:
<%= link_to post.title, topic_post_path(post.topic, post) %>