Low commit activity in last 3 years
No release in over a year
Be explicit about what you send to your Rails views
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

>= 0
 Project Readme

render_with_view

Be explicit about the things you send from your Rails controller to the view.

Example

app/controllers/application_controller.rb:

class ApplicationController
  include RenderWithView
end

app/controllers/home_controller.rb:

class HomeController < ApplicationController
  def index
    render_with_view posts: Post.all
  end
end

app/views/home/index.html.erb:

<ul>
  <% view.posts.each do |post| %>
    <li><%= link_to post.title, post %></li>
  <% end %>
</ul>

Why not just use instance variables?

They feel like magic. Instead I like how this forces me to be explicit in what I send along to my templates. It's like a small step towards having a presenter/view layer (or whatever) but not going further than just adding the convention of using a single variable.

What's view?

An object with reader methods for every key in the hash you gave it. A HalfOpenStruct in a way.

Installation

Add render_with_view to your Gemfile:

gem 'render_with_view'

Include it in your ApplicationController:

class ApplicationController
  include RenderWithView
end

Bonus RSpec matcher

require 'render_with_view/rspec_matcher'

describe ThingController do
  subject { get :index }
  it { should set_view_local :key, optional_value }
end

License

MIT