Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9040803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:04:52+00:00 2026-06-16T10:04:52+00:00

My Rails app has Post and Member models. Within posts/:id/show contains a like button

  • 0

My Rails app has Post and Member models. Within posts/:id/show contains a “like” button which Members can click, or “Unlike” if @member has already “liked” this @post already.

(This button will link to a post action that does some ajax and makes the “like” button change into a “unlike” button)

Whats the best practice for caching the button? (below code obviously doesn’t cache the button html).

Should I add :touch => true to member.rb, and then make a cache key for the button e.g. <% cache ['V1', @post, @member, 'like_button'] ? (seems redundant?)

post.rb

has_many :likes

like.rb

belongs_to :member
belongs_to :post

member.rb

has_many :likes

*posts/show.html.erb *

<% cache ['V1', @post, 'show'] do %>
  <h1>@post.title</h1>
  <div class="content">@post.content</div>
<% end %> 
  <%= render 'like_button', :post=> @post, :member => @member %> 

** posts/_like_button.html.erb **

<% if member.liked_post?(post) %> 
  <%= link_to unlike_post_path(post), :method => :post, :remote => true, :class => 'btn' %>
<% else %> 
  <%= link_to like_post_path(post), :method => :post, :remote => true, :class => 'btn' %>  
<% end %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:04:53+00:00Added an answer on June 16, 2026 at 10:04 am

    You can do something along these lines:

    <% cache ['V1', @post, @member.liked_post?(@post), 'show'] do %>
      <h1>@post.title</h1>
      <div class="content">@post.content</div>
      <%= render 'like_button', :post=> @post, :member => @member %> 
    <% end %> 
    

    This gives your 2 different cached versions of the fragment – one each for the ‘liked’ and ‘not liked’ states. This is better than 1 version per user.

    YOu run the risk here of someone adding code to the like_button partial that uses more of the @member parameter, and that isn’t part of the cache key, so you’ll get incorrect results.

    For this case, I’d change the like_button partial to take the same parameter as the cache call – @member.liked_post(@post) — to make it clear that this is the only value used inside the partial code.

    <%= render 'like_button', :post=> @post, :liked => @member.liked(@post) %> 
    

    With the new partial:

    <% if liked %> 
      <%= link_to unlike_post_path(post), :method => :post, :remote => true, :class => 'btn' %>
    <% else %> 
      <%= link_to like_post_path(post), :method => :post, :remote => true, :class => 'btn' %>  
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app which currently has 3 models: Post , User and
In my Rails 2.3.2 app I have 2 models: class Post has_many :approved_comments, :class_name
My Rails app has some pages which are SSL-required and others which are SSL-optional.
I'm writing a Rails app which has the following jasmine spec: describe buttons, ->
As I said in a previous post, our Rails app has to interface with
I have a rails 3.1 app with the following Models: class Post < ActiveRecord::Base
I have some rails app. For example I have next models: User , Post
I'm currently developing a Rails app with 4 nested models (as per THIS POST
I am developing a rails app that allows users to post, much like facebook
In my rails app I have 2 models: post and post_translations. class PostTranslation <

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.