Rails: Display user's liked post & own their posts on the same page -
i'm new rails & i've been stuck on problem hours. trying display posts user has liked , user's actual posts.
with current code, i'm getting error "undefined method `title' #" , it's being extracted line: "<%= link_to post.title, post %>".
coud shed light how can work? (more code below)
def show @user = user.find_by_username(params[:id]) if @user @posts = @user.posts.all + @user.likes.all render actions: :show @likes = @user.likes.all else render file: 'public/404', status: 404, formats: [:html] end end
here's routes file:
resources :likes, only: [:create, :destroy] resources :posts devise_scope :user 'register', to: 'devise/registrations#new' 'edit', to: 'devise/registrations#edit' 'login', to: 'devise/sessions#new' 'logout', to: 'devise/sessions#destroy' end
here's 'show' view:
<% if @posts %> <% @posts.each |post| %> <%= link_to post.title, post %> <% end %> <% end %>
"<%= link_to post.title, post %> display <%= post.inspect %>" here's results
<post id: 11, title: "testing123", user_id: 2, created_at: "2013-05-18 19:25:45", updated_at: "2013-05-18 19:25:45"> #<like id: 23, post_id: 10, user_id: 2, created_at: "2013-05-18 21:39:17", updated_at: "2013-05-18 21:39:17">
it works when use -> @posts = @user.posts.all
problem starts when use -> @posts = @user.posts.all + @user.likes.all
which gives me "undefined method `title'" message..
you may want add user model...
class user < activerecord::base has_many :likes has_many :liked_posts, :through => :likes, :source => :post ... end
then, in controller can this
@posts = @user.posts + @user.liked_posts
Comments
Post a Comment