2

I'm a new to Ruby on Rails. Here I have model called "Pesans". I also have controller called "sessions". Pesans has 3 attributes (receiver, sender and text)

The user could compose new message. When composing Message, I have method in sessions controller:

def createPesan
    user = Users.find_by_username(params[:receiver])
    if user
        pesan = Pesans.new
        pesan.receiver = params[:receiver]
        user = Users.find(session[:user_id])
        pesan.sender = user.username
        pesan.text = params[:text]
        pesan.save
        render 'message'
    else
        render 'MsgError'
    end
end

It works perfectly fine (I also looked up into the database using rails command prompt, and the new message really is saved). Here is my method in sessions controller to fetch the message:

def current_inbox
    @current_inbox ||= current_user && Pesans.where(["receiver = ?", current_user.username]).select("sender, text").all
end

And I have code in my view to show Pesans corresponding to current_user (logged user in the inbox web):

    <% if @current_inbox.blank? %>
            Inbox Empty
        <% else %>
            <% for pesan in @current_inbox %>
                <%= pesan.sender %> <br>
                <%= pesan.text %> <br>
            <% end %>
        <% end %>

But it keeps showing "Inbox Empty" although there's a message for corresponding user (current_user.username). Where did I go wrong?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
user3560626
  • 55
  • 1
  • 8

2 Answers2

1

It solved by changing view's name. Thank you. Although I still don't get It why -_-

user3560626
  • 55
  • 1
  • 8
1

Although I still don't get It why

Let me explain

--

Here is my method in sessions controller to fetch the message:

def current_inbox
    @current_inbox ||= current_user && Pesans.where(["receiver = ?", current_user.username]).select("sender, text").all
end

Where does this get called?

I bet you changed your view name to current_inbox.html.erb - if that's the case, it confirms the reason you have the issue - your current_inbox method will not be called in your controller

What you'll want to do is put current_inbox in a helper (app/helpers/***_helper.rb) - this will make your current_inbox helper available in your views, allowing you call it like a local variable:

#app/helpers/application_helper.rb
Class ApplicationHelper
    def current_inbox
        current_user && Pesans.where(["receiver = ?", current_user.username]).select("sender, text").all
    end
end

The reason to do this is actually quite simple

When you run your app (send it requests), it will basically run a particular controller action (method). This will mean that any adjoining methods in the controller (in your case current_inbox) will not be called - it's only the action / method you've requested

A workaround to that will be to use a callback such as before_action:

#app/controllers/your_controller.rb
Class YourController < ApplicationController
   before_action :current_inbox

   private

   def current_inbox
      #... logic here
   end
end

If this is the case for you (I don't have the code for your app) - your renaming of your view will basically allow you to run the action you need, rather than calling it properly

--

Model

You mention you called your model Peasans

That's fine if you want to break Rails conventions (thus causing) problems when they update Rails in the future

Rails' models are meant to be singular, and their tables are meant to be plural. Anything other than this will break convention, and open you up to upgrade problems down the line

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147