jeudi 18 juin 2020

Confused about Rails MVC: Why can the view invoke model-methods?

I got this Model-method:

class Post < ApplicationRecord
    def details
        "This post was created on #{created_at.strftime("%d %M %Y")}"
    end
end

In the controller: I create a variable which has the return-value of the method.

def show
    @test = @post.details
end

In the view I display the value of the variable 'test' (first paragraph).

<p>
  <%= @test %>
</p>

<p>
  <%= @post.details %>
</p>

But moreover: I can invoke the model-method directly from the view. Second paragraph ...

That really confuses me, because I thought that wouldn't be possible at all.

I thought that only the controller can invoke the model-methods and that it (the controller) creates the variables, which are then available within the view ... So that the model and view are separated and decoupled.

But they way it is (obviously), the model and view would become merged together anyway.

What sense makes the controller at all, when the view can invoke model-methods itself?

Aucun commentaire:

Enregistrer un commentaire