Austin Burke
Austin Burke

Reputation: 148

Rails - Using Posts#Index View to show only 'Liked' Posts

Since the index view for posts has the same code, I have an action in the Posts controller #liked, which save all of the current_user's liked posts. I am trying to redirect_to posts_path(@posts), but for some reason the index view still has @posts returning all posts..? I'm sure there is some sort of rails magic happening here.

I already checked my #liked method and it is successfully saving only liked posts in @posts, but the redirect is simply not passing @posts from this action, but rather from the original #index action.

Ideas?

Upvotes: 0

Views: 70

Answers (1)

NN796
NN796

Reputation: 1267

Austin Burke, I am assuming you have following two methods in posts controller:

def posts @posts = Post.all end

And a second method:

def liked @liked_posts = [post1, post2] end

You should do:

def posts @liked_posts = Post.where(liked: true) end

The reason is you are using redirect_to posts_path(@posts) which calls index method of posts controller. This index method of posts controller should take @liked_posts with it.

Upvotes: 4

Related Questions