Rails closure_tree - 如何在PostsController中添加Devise方法current_user来创建动作?

时间:2015-07-14 16:58:16

标签: ruby-on-rails devise nested

我使用这个closure_tree教程构建了嵌套的帖子:

[使用Rails嵌套注释 - Sitepoint] [1]

然后我安装了Devise并开始将帖子归功于PostsController和Post&用户模型。如何在我的PostsController中将current_user方法添加到Create操作(' if'部分,而不是' else'部分),以便我可以跟踪有回复的帖子的创建者?我尝试了几种组合,包括:

 @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 partial

   <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`

2 个答案:

答案 0 :(得分:0)

我有一个问题,了解你的帖子嵌套在什么位置?您引用的教程显示了如何嵌套注释。评论可以嵌套在帖子下。我相信别人会比我有限的经验更有帮助,但从我所看到的,你的方法是错误的。请考虑以下事项:

def index
@post = Post.new
@posts = Post.hash_tree
end

在索引中,您应该显示对象的索引(将其视为列表),在这种情况下,它将是帖子列表。您正在发布一个新帖子,该帖子应该在您的“新”方法中发生。在您引用的教程中,您会注意到索引定义为:

  def index
    @comments = Comment.all
  end

为了给您一个示例,如果我们在主题下嵌套帖子,您的代码可能如下所示:

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
...

基本上,如果您的帖子嵌套在其他内容中,您必须首先找到该对象的ID以将其嵌套在其下。评论也可以这样做(评论嵌套在你的帖子下)。您可以在评论控制器中查找post_id。

答案 1 :(得分:0)

想出来。我需要将user_id:与post_params合并,并且在post index视图中调用用户时,我需要使用post.user.name与user.name。