Metaprogramming with ruby on Rails in a partial

Asked

Viewed 76 times

0

I am working on a Ruby on Rails project and I am working on a module (Financial) it is linked to several models, and depending on the screen that the user is he can bring a screen with all the financial movements of that object. However I wanted to do only a partial, in partial I would pass a variable (catch via metaprogramming) to a given field (also caught via meta programming)

To try to generate the link I am doing so:

  <%%= link_to new_financial_path(<%= singular_table_name %>_id: @<%= singular_table_name %>.id), class: 'btn btn-primary pull-right', remote: true do %>
    <span class="fa fa-plus"></span>
    <%= I18n.t('financials.new.new') %>
  <%% end %>

But the system is returning

NoMethodError - undefined method `singular_table_name' for #<#<Class:0x007ff098434898>:0x007ff0b5c02eb8>

Did you Mean? singleton_method: app/views/Financials/_Financials.html.erb:2:in `_app_views_financials__financials_html_erb___3468020359035041149_70335661148700'

Does anyone know how I could do it? After all it would be much easier since the only thing that changes is the colum_id field: @columvariable_id in case Colum would receive the singular table name of the controller itself.

  • If I do so <%= link_to new_financial_path(<%= controller_name.singularize %>_id: @<%= controller_name.singularize %>.id), class: 'btn btn-Primary pull-right', remote: true of %> <class="fa fa-plus"></span> <%= I18n.t('Financialsnew.new') %> <%% end %> The system prints an html line like this: <%= link_to new_financial_path(<%= controller_name.singularize %>_id: @Reproduction.id), class: btn btn-Primary pull-right', remote: true do %> <%= I18n.t('Financials.new.new') %> <% end %>

  • You are aware of this error because there is no "singular_table_name".

1 answer

1


A suggestion is to do so:

1) Create a Concern for templates that may have extract you will need to use. Remembering that the Concerns should be in the models/Concern folder so Rails will automatically read the Concerns. The code would look something like this:

module Financiable
  extend ActiveSupport::Concern

  class_methods do

    def transactions
      puts "Found: 30 itens"
    end
  end

end

2) Use the Concern created in your models:

class Customer < ActiveRecord::Base
  include Financiable

end

2) Create a partial that takes a "model" variable. It would look like this:

<%%= link_to new_financial_path(model.class.name, model.id), class: 'btn btn-primary pull-right', remote: true do %>
    <span class="fa fa-plus"></span>
    <%= I18n.t('financials.new.new') %>
<%% end %>

3) When you use partial, use like this:

<%= render partial: "financial_trancations", locals: { model: cliente } %>

Taking into account that you have a variable called model-type client.

4) In the control that loads the partial data should have a method that would look like this:

def index
    clazz = params[:model_name].constantize.find(params[:model_id])
    @transactions = obj.transactions if class.respond_to?(:transactions)
end

If everything is in the right place it should work.

Abs!

Browser other questions tagged

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