Q. Rails undefined method 'full_name' for #Room: 0x0000000ed6d478 [closed]

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 displayed error:       undefined method 'full_name' for #Room: 0x0000000ed6d478

As you can see the method is already defined in the controller, and my view is able to call various methods of the Room controller except the ones I defined.

    
asked by anonymous 18.05.2017 / 17:02

2 answers

0

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

To do so, add the method to your Room model

def nome_completo
  "#{title}, #{location}"
end
    
19.05.2017 / 14:44
0

I was able to solve it, 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 receive would be

  

RoomController: 0x0000000ed6d478

instead of

  

Room: 0x0000000ed6d478

    
18.05.2017 / 18:21