Q. Rails Undefined method `full name_for #<Room:0x0000000ed6d478>

Asked

Viewed 72 times

0

My Controller:

class RoomsController < ApplicationController
  before_action :set_room, only: [:show, :edit, :update, :destroy]

  def nome_completo
    "#{title}, #{location}"
  end


  # GET /rooms
  # GET /rooms.json
  def index
    @rooms = Room.all
  end

  # GET /rooms/1
  # GET /rooms/1.json
  def show
  end


  # GET /rooms/new
  def new
    @room = Room.new
  end

  # GET /rooms/1/edit
  def edit
  end
......
end

My View:

<h1>Quartos recém postados</h1>
<ul>
    <% @rooms.each do |room| %>
    <li><%= link_to room.nome_completo , room %></li>
    <% end %>
</ul>

The error shown is: Undefined method `full name' for #Room:0x0000000ed6d478

As you can see, the method is already defined in the controller, and my view can call several methods from the Room controller except those I defined.

2 answers

0


You cannot define methods in your controller which are responsibilities of your model. Based on the idea of the MVC, your controller is only a "forwarder" of requests. Therefore, you should delegate this responsibility to your model.

To do so, add the method to your Room model

def nome_completo
  "#{title}, #{location}"
end

0

The problem is that the method should be defined in the model, not in the controller. If it was in the controller the error I would get would be

RoomController:0x0000000ed6d478

instead of

Room:0x0000000ed6d478

Browser other questions tagged

You are not signed in. Login or sign up in order to post.