Undefined method link_to_edit using Draper decorator

1.5k Views Asked by At

I've got a User and Post models, which are related to each other in a classical way -- User has_many :posts and Post belongs_to :user. In my users#show, where I display a user's profile, I also have a list of all posts he has made. Also, I wanted to have links to edit and delete each post respectfully. So, I made up with this:

<% @user.posts.each do |post| %>
  <h1><%= link_to post.title, post_path(post) %></h1>
  <% if @user == current_user %>
      <%= link_to 'Edit', edit_post_path(post) %>
      <%= link_to 'Delete', post_path(post), method: :delete %>
  <% end %>
<% end %>

But surely placing this logic into view results in a mess, so I decided to use Draper and write decorators for that. As we are going to check rights for posts#edit and posts#delete methods, I came up with a decorator for Post model and tried to use it in PostsController. Here it goes:

class PostDecorator << Draper::Decorator
  delegate_all

  def link_to_edit
    if object.user == current_user
      h.link_to 'Edit', h.edit_post_path(object)
    end
  end

  def link_to_delete
    if object.user == current.user
      h.link_to 'Delete', h.post_path(object), method: :delete
    end
  end   
end

Then, in my PostsController:

# ... class definition
before_action :set_post, only: [:show, :edit, :update, :destroy]

# ... other controller methods
def edit; end

def update
  if @post.update(post_params)
    @post.save
    redirect_to post_path(@post)
  else
    render 'edit'
  end
end

def destroy
  @post.destroy
  redirect_to feed_path
end

private

# Using FriendlyId gem to have neat slugs
def set_post
  @post = Post.friendly.find(params[:id]).decorate
end

But every time I try to render my User profile with list of his posts, with the use of my new helpers <%= post.link_to_delete %> and <%= post.link_to_edit %> instead of that conditional mess, it just returns me the following error:

error

What am I doing wrong?

1

There are 1 best solutions below

1
Sebastian Iorga On BEST ANSWER

You probably figured this out in the meantime but here's an answer for others: You were calling @post = ....decorate in your controller but you are using @user.posts.each { |post| ... } in your view. The objects fed to that block are not decorated. Only @post is.

In your view you should have done something like @user.posts.each { |raw_post| post = raw_post.decorate } and so on. Obviously, with ERB syntax. Or @user.decorated_posts.each ... where

class User < ActiveRecord::Base
  ...
  def decorated_posts
    # this will load every post associated with the user.
    # if there are a lot of them you might want to only load a limited scope of them
    posts.map(&:decorate)
  end
  ...
end