Rails closure_tree - 如何将设计方法 current_user 添加到 PostsController 中的创建操作?
Rails closure_tree - how do I add Devise method current_user to Create action in PostsController?
我使用这个 closure_tree 教程构建了嵌套的 posts:
[嵌套评论 Rails - Sitepoint][1]
然后我安装了 Devise 并开始将 posts 归因于 PostsController 和 Post & User 模型中的用户。如何将 current_user 方法添加到我的 PostsController 中的 Create 操作('if' 部分,而不是 'else' 部分),以便我可以跟踪 post哪位有回帖?我尝试了几种组合,包括:
@post = current_user.parent.children.build(post params)
@post = current_user.posts.parent.children.build(post params)
都是抛错。
这是我的代码:
Post.rb
class Post < ActiveRecord::Base
belongs_to :user
acts_as_tree order: 'created_at DESC'
end
User.rb
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
posts_controller.rb
class PostsController < ApplicationController
before_action :authenticate_user!, except: [:index]
def index
@post = Post.new
@posts = Post.hash_tree
end
def show
@post = Post.find(params[:id])
end
def new
@post = current_user.posts.build(parent_id: params[:parent_id])
end
def create
if params[:post][:parent_id].to_i > 0
parent = Post.find_by_id(params[:post].delete(:parent_id))
@post = parent.children.build(post_params)
else
@post = current_user.posts.build(post_params)
end
if @post.save
flash[:success] = 'Your post was successfully added!'
redirect_to posts_path
else
render 'new'
end
end
def edit
end
def update
end
def destroy
end
private
def post_params
params.require(:post).permit(:content)
end
end
_Post.html.erb部分
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="http://placehold.it/50x50" alt="...">
</a>
</div>
<div class="media-body">
<%= post.content %></br>
<%= link_to time_ago_in_words(post.created_at), post_path(post) %> ago by <%= post.user.name.capitalize %> | <% from_reply_form ||= nil %>
<% unless from_reply_form %>
<%= link_to 'reply', new_post_path(post.id) %>
<% end %>
</div>
</div>
谢谢!!!
`ThreadedSalesApp::Application.routes.draw do
devise_for :users
resources :users, only: [:show, :index]
resources :posts, only: [:index, :show, :create]
get '/posts/new/(:parent_id)', to: 'posts#new', as: :new_post
root 'posts#index'
end`
我在理解您的 post 嵌套在什么下时遇到了一些问题?您引用的教程显示了如何嵌套评论。评论 可以 嵌套在 post 下。鉴于我有限的经验,我相信其他人会比我更有帮助,但据我所知,你的方法是错误的。考虑以下因素:
def index
@post = Post.new
@posts = Post.hash_tree
end
在您的索引中,您应该显示对象的 index(将其视为列表),在这种情况下,它将是帖子列表。您正在启动一个新的 post,这应该在您的 'new' 方法中发生。在您引用的教程中,您会注意到索引定义为:
def index
@comments = Comment.all
end
举个 示例,如果我们在主题下嵌套 post,您的代码可能如下所示:
class Topics::PostsController < ApplicationController
def show
@post = Post.find(params[:id])
@topic = Topic.find(params[:topic_id])
end
def new
@topic = Topic.find(params[:topic_id])
@post = Post.new
end
def create
@topic = Topic.find(params[:topic_id])
@post = current_user.posts.build(post_params)
@post.topic = @topic
if @post.save
flash[:notice] = "Your post was successfully added!"
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving your post."
render :new
end
end
...
基本上,如果您的 post 嵌套在其他对象下,您必须先找到该对象的 ID 才能将其嵌套在该对象下。评论也可以这样做(评论嵌套在您的 post 下)。你会在你的 Comments Controller 中寻找 post_id。
想通了。我需要将 user_id: 与 post_params 合并,并且在 post 索引视图中调用用户时我需要使用 post.user.name 而不是 user.name。
我使用这个 closure_tree 教程构建了嵌套的 posts:
[嵌套评论 Rails - Sitepoint][1]
然后我安装了 Devise 并开始将 posts 归因于 PostsController 和 Post & User 模型中的用户。如何将 current_user 方法添加到我的 PostsController 中的 Create 操作('if' 部分,而不是 'else' 部分),以便我可以跟踪 post哪位有回帖?我尝试了几种组合,包括:
@post = current_user.parent.children.build(post params)
@post = current_user.posts.parent.children.build(post params)
都是抛错。
这是我的代码:
Post.rb
class Post < ActiveRecord::Base
belongs_to :user
acts_as_tree order: 'created_at DESC'
end
User.rb
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
posts_controller.rb
class PostsController < ApplicationController
before_action :authenticate_user!, except: [:index]
def index
@post = Post.new
@posts = Post.hash_tree
end
def show
@post = Post.find(params[:id])
end
def new
@post = current_user.posts.build(parent_id: params[:parent_id])
end
def create
if params[:post][:parent_id].to_i > 0
parent = Post.find_by_id(params[:post].delete(:parent_id))
@post = parent.children.build(post_params)
else
@post = current_user.posts.build(post_params)
end
if @post.save
flash[:success] = 'Your post was successfully added!'
redirect_to posts_path
else
render 'new'
end
end
def edit
end
def update
end
def destroy
end
private
def post_params
params.require(:post).permit(:content)
end
end
_Post.html.erb部分
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="http://placehold.it/50x50" alt="...">
</a>
</div>
<div class="media-body">
<%= post.content %></br>
<%= link_to time_ago_in_words(post.created_at), post_path(post) %> ago by <%= post.user.name.capitalize %> | <% from_reply_form ||= nil %>
<% unless from_reply_form %>
<%= link_to 'reply', new_post_path(post.id) %>
<% end %>
</div>
</div>
谢谢!!!
`ThreadedSalesApp::Application.routes.draw do
devise_for :users
resources :users, only: [:show, :index]
resources :posts, only: [:index, :show, :create]
get '/posts/new/(:parent_id)', to: 'posts#new', as: :new_post
root 'posts#index'
end`
我在理解您的 post 嵌套在什么下时遇到了一些问题?您引用的教程显示了如何嵌套评论。评论 可以 嵌套在 post 下。鉴于我有限的经验,我相信其他人会比我更有帮助,但据我所知,你的方法是错误的。考虑以下因素:
def index
@post = Post.new
@posts = Post.hash_tree
end
在您的索引中,您应该显示对象的 index(将其视为列表),在这种情况下,它将是帖子列表。您正在启动一个新的 post,这应该在您的 'new' 方法中发生。在您引用的教程中,您会注意到索引定义为:
def index
@comments = Comment.all
end
举个 示例,如果我们在主题下嵌套 post,您的代码可能如下所示:
class Topics::PostsController < ApplicationController
def show
@post = Post.find(params[:id])
@topic = Topic.find(params[:topic_id])
end
def new
@topic = Topic.find(params[:topic_id])
@post = Post.new
end
def create
@topic = Topic.find(params[:topic_id])
@post = current_user.posts.build(post_params)
@post.topic = @topic
if @post.save
flash[:notice] = "Your post was successfully added!"
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving your post."
render :new
end
end
...
基本上,如果您的 post 嵌套在其他对象下,您必须先找到该对象的 ID 才能将其嵌套在该对象下。评论也可以这样做(评论嵌套在您的 post 下)。你会在你的 Comments Controller 中寻找 post_id。
想通了。我需要将 user_id: 与 post_params 合并,并且在 post 索引视图中调用用户时我需要使用 post.user.name 而不是 user.name。