1

I am beginner in rails. I am using after_action in the notifications controller.
Notifications controller

class NotificationsController < ApplicationController

    layout "posts"

    after_action :read_message, only: [:index]

    def index
        @notifications = Notification.where(:recipient_id => session[:registered_id]).order("created_at DESC")

    end

    def new
        @user = User.find(session[:user_id])
        @notification = @user.notifications.new
    end

    def create
        @user = User.find(session[:user_id])
        @notification = @user.notifications.new notification_params
        if @notification.save
            redirect_to(:controller => "posts", :action => "index")
        else
            render "new"
        end
    end

    private

    def notification_params
        params.require(:notification).permit(:message, :user_id, :recipient_id, :status)
    end

    def read_message
        @notifications = Notification.where(:recipient_id => session[:registered_id]).order("created_at DESC")
        @notifications.read_all
    end

end

notifications#index view

 <% @notifications.each do |notification| %>
   <div class = message_wrapper>
    <p><%= notification.message %></p>
    <p class = "message_details">from <span><%= notification.user.registered_id %></span></p>
  </div>
 <% end %>

Now in the after_action method in the controller, i want to set the class of <div>(currently with class message_wrapper) to message_wrapper_read. How can I do this? I appreciate your answers. Thanks in advance.

1 Answer 1

7

Define class name in controller is not good. It should work on view or helper.

simply you can do,

<div class="<%= notification.read? ? 'message_wrapper_read' : 'message_wrapper' %>">
Sign up to request clarification or add additional context in comments.

1 Comment

There should not be any spaces around the =.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.