Rendering a View from My Ruby on Rails Gem

Rendering a view from my Ruby on Rails Gem

The "correct" way to do this since Rails 3 is to create an Engine; there's also a Rails guide for this, but creating a basic engine is as easy as:

module MyGemName
class Engine < Rails::Engine
end
end

When Rails looks for a view to render, it will first look in the app/views directory of the application. If it cannot find the view there, it will check in the app/views directories of all engines that have this directory.

Render Custom views instead of default Devise views- Rails

Overriding the default Devise’s views is simple and straight forward, what we need to do is just generate those views in our app/views folder by the following single command.

rails g devise:views

The following views are generated.

app/views/devise/confirmations
app/views/devise/mailer
app/views/devise/passwords
app/views/devise/registrations
app/views/devise/sessions
app/views/devise/shared
app/views/devise/unlocks

No other changes are necessary.
Or if you have already done it,you need to move folders inside app/views/devise

Railtie(gem): How do i include and render erb partials?

After a lot of trail and error i managed to get it working. This is what i had to do:

I changed this:

initializer 'embeddable.add_view_paths', :after => :add_view_paths do |app|
ActiveSupport.on_load(:action_controller) do
append_view_path app.root.join("lib/embeddable/").to_s
end
end

Into this:

initializer 'embeddable.add_view_paths', :after => :add_view_paths do |app|
ActiveSupport.on_load(:action_controller) do
append_view_path "#{Gem.loaded_specs['embeddable'].full_gem_path}/lib/embeddable"# <- this is the change
end
end

Now i can render partials from the helper like this:

render 'partials/youtube', id: video_id, width: width, height: height


Related Topics



Leave a reply



Submit