felipecamposclarke
felipecamposclarke

Reputation: 924

how to render a view from the model in Rails?

I have to edit a Rails application, but I am a novice when it comes to language (I am new to Ruby & Rails). When viewing the source files I raised some questions:

class Card < ActiveRecord::Base
    class CardRenderer < AbstractController::Base
        include AbstractController::Rendering
        include AbstractController::Helpers

        self.view_paths = Rails.application.config.paths["app/views"]

        helper_method :res_url

        def print(_card, _template)
             @card = _card
             render template: "prints/#{_template}.html"
        end

        protected
            def res_url(_res_name)
                "#{Rails.root}/app/views/prints/res/#{_res_name}"
            end
     end
     .......
     .......
     .......
end

I saw a model that contained a classe inside another class Do you have a name this language feature?

How I can call this class from an instance of the model? for example from the controller (Obviously this does not work):

@card::CardRenderer.new.print(@card, @template)

The class that is inside the model class has a function that renders a view

I can make use this function from the controller? for example from the controller (obviously this does not work either)

render Card::CardRenderer.new.print(@card, @template)

Is that the ideal is that I read a book!, And I'll do it! but in these days I can not (timing problems).

Upvotes: 1

Views: 391

Answers (1)

AJcodez
AJcodez

Reputation: 34166

Do you have a name for this language feature?

"nested classes", I think

How I can call this class from an instance of the model?

@card.class::CardRenderer should work

The class that is inside the model class has a function that renders a view. Can I use this function from the controller?

Read the whole thing start to finish: http://guides.rubyonrails.org/layouts_and_rendering.html


p.s. I have a sneaking suspicion the current design of the app goes completely against the ideas of Model-View-Controller

Upvotes: 1

Related Questions