Sean Magyar
Sean Magyar

Reputation: 2380

rails extra action when clicking on link

I have a rails app with notifications. Notification has :checked_at datetime attribute. On the notification index page I link every notification to the notifiable object like <%= link_to posts_path(anchor: "post_#{notification.notifiable_id}") do %>.

What I wanna achieve is when clicking on this link (that takes to the notifiable object) the notification.checked_at attribute would get updated.

I have some different solutions in my mind like an extra AJAX call when clicking on the link OR having an extra route like notifications#check_notification the link helper would point to and after checking the attribute it would redirect_to the original posts#index page.

I'm not sure which one is the preferred way (maybe a 3rd one) in rails. Could sby tell me what to do?

Upvotes: 1

Views: 105

Answers (1)

MurifoX
MurifoX

Reputation: 15089

You could add another parameter to the route, something like update_checked_at, then on your index action inside the controller, you check if it exists and it is true.

<%= link_to posts_path(
  anchor: "post_#{notification.notifiable_id}", 
  update_checked_at: true) do %>

def index
  if params[:update_checked_at]
    # Do your thing

Upvotes: 1

Related Questions