Rails closure_tree - how do I add Devise method current_user to Create action in PostsController? -


i built nested posts using closure_tree tutorial:

[nested comments rails - sitepoint][1]

then installed devise , began attributing posts users in postscontroller , post & user models. how can add current_user method create action (the 'if' portion, not 'else' portion) in postscontroller can track creator of post has replies? have tried several combinations including:

 @post = current_user.parent.children.build(post params)  @post = current_user.posts.parent.children.build(post params) 

they throwing errors.

here code:

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 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 <%= post.user.name.capitalize %> | <% from_reply_form ||= nil %>     <% unless from_reply_form %>       <%= link_to 'reply', new_post_path(post.id) %>     <% end %>       </div>     </div> 

thanks!!!

`threadedsalesapp::application.routes.draw    devise_for :users    resources :users, only: [:show, :index]   resources :posts, only: [:index, :show, :create]   '/posts/new/(:parent_id)', to: 'posts#new', as: :new_post    root     'posts#index'   end` 

i'm having bit of issue understanding post nested under? tutorial referenced shows how nest comments. comments could nested under posts. i'm sure others more helpful given limited experience can see, methods wrong. consider following:

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

in index, ought show index (think of list) of objects, in case list of posts. you're initiating new post should happen in 'new' method. in tutorial referenced, you'll notice index defined as:

  def index     @comments = comment.all   end 

to give example, if we're nesting posts under topics, code this:

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 added!"         redirect_to [@topic, @post]         else         flash[:error] = "there error saving post."         render :new         end     end ... 

basically, if posts nested under else, must find object's id first nest under that. same done comments (comments being nested under posts). post_id in comments controller.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -