Rails Custom Renderer

Rails Custom Renderer

What might help is to create a responder (and use the newer respond_with) with the explicit method:

class ActionController::Responder
def to_my_custom_renderer
controller.render :my_custom_renderer => controller.action_name
end
end

custom will_paginate renderer

Found a decent blog post about custom will_paginate renderer

module ApplicationHelper
# change the default link renderer for will_paginate
def will_paginate(collection_or_options = nil, options = {})
if collection_or_options.is_a? Hash
options, collection_or_options = collection_or_options, nil
end
unless options[:renderer]
options = options.merge :renderer => MyCustomLinkRenderer
end
super *[collection_or_options, options].compact
end
end

and then in an initializer

class MyCustomLinkRenderer < WillPaginate::ActionView::LinkRenderer do
def container_attributes
{class: "tc cf mv2"}
end

def page_number(page)
if page == current_page
tag(:span, page, class: 'b bg-dark-blue near-white ba b--near-black pa2')
else
link(page, page, class: 'link ba b--near-black near-black pa2', rel: rel_value(page))
end
end

def gap
text = @template.will_paginate_translate(:page_gap) { '…' }
%(<span class="mr2">#{text}</span>)
end

def previous_page
num = @collection.current_page > 1 && @collection.current_page - 1
previous_or_next_page(num, @options[:previous_label], 'link ba near-black b--near-black pa2')
end

def next_page
num = @collection.current_page < total_pages && @collection.current_page + 1
previous_or_next_page(num, @options[:next_label], 'link ba near-black b--near-black pa2')
end

def previous_or_next_page(page, text, classname)
if page
link(text, page, :class => classname)
else
tag(:span, text, :class => classname + ' bg-dark-blue near-white')
end
end
end

Render custom layout instead of application layout

As I mentioned, you should change

format.html { render :new }

to

format.html { render :new, layout: 'theme_forest'}

When an error occurs during create process, render new action is
invoked

Explanation:

No! When you use render, it just loads the template(i.e, new.html.erb). It won't invoke/trigger the corresponding controller#method(i.e, new). So render layout: 'theme_forest' in the new method never gets called in this situation.

Custom Renderer in Simple Navigation (Rails Gem)

Regarding your two specific questions:

I can't figure out how to lay out the ruby/rails code so that it mirrors the behavior of the markup in the fiddle?

The target html structure seems pretty complicated to me. Is there any way to simplify this? In addition, it probably would be easier to help if you would fork the simple-navigation bootstrap renderer so it would be more obvious what you have changed.

The kicker here is that the children UL/LI elements must be presented on page load, but the simple navigation GEM hides them until their parent UL/LI element is active.... frustratingly.... without fail.

simple-navigation is - by default - configured that it only renders all primary items and the subnavigation of the active primary item, so this is a feature, not a bug :-). If you need to render the complete navigation independently of the active item, you need to pass the :expand_all => true option to the render_navigation call, which in turn is used to determine the return value of SimpleNavigation::Rendering::Renderer::Base#include_sub_navigation?.

Rails 3 and JSON: Default renderer but custom mime type

Solution for rendering:

Put an empty method into your models:

def to_xxx_v1
end

It is not called but has to be present for respond_with.

Setting up custom formats for respond_to and respond_with in Ruby on Rails 4

You'll need to manually add a Renderer for your custom format if you want it to render without explicitly adding a view template.

The Rails built-in formats xml and json have renderers automatically added from within the Rails framework, which is why they work right out of the box and your custom format does not (source code).

Try adding this in an initializer or right below where you register your MIME type

# config/initializers/renderers.rb
ActionController::Renderers.add :foo do |object, options|
self.content_type ||= Mime::FOO
object.respond_to?(:to_foo) ? object.to_foo : object
end

NOTE: Don't use custom as your format name because it will conflict with a method in the respond_with internals.

There is an excellent blog post that explains building custom Renderers in depth here: http://beerlington.com/blog/2011/07/25/building-a-csv-renderer-in-rails-3/

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



Related Topics



Leave a reply



Submit