{"id":57,"date":"2009-04-02T23:55:31","date_gmt":"2009-04-02T22:55:31","guid":{"rendered":"http:\/\/www.schuerig.de\/michael\/blog\/?p=57"},"modified":"2021-11-24T09:20:32","modified_gmt":"2021-11-24T08:20:32","slug":"ruby-views","status":"publish","type":"post","link":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/2009\/04\/02\/ruby-views\/","title":{"rendered":"Ruby as a language for Rails views"},"content":{"rendered":"<p>Didn&#8217;t you always want to write your Rails views as plain Ruby objects? &#8212; <em>&#8220;What?&#8221;<\/em>, I hear you say. No, I haven&#8217;t lost my mind and the idea is quite sensible (or so I hope), once you add the restriction that it is JSON-formatted data that you want to return.<\/p>\n<p>Say you need to set up some hashes or arrays for rendering to JSON. This is best done in Ruby and it is clearly a view concern. So let&#8217;s do it in the views. Like this:<\/p>\n<p><code><\/p>\n<pre>\r\n  # app\/controllers\/movies_controller.rb\r\n  def index\r\n    respond_to do |format|\r\n      format.json do\r\n        @movies = Movie.all\r\n        @count = Movie.count\r\n        render :template =&gt; 'movies\/index.json.rb'\r\n      end\r\n    end\r\n  end\r\n<\/pre>\n<p><\/code><\/p>\n<p><code><\/p>\n<pre>\r\n  # app\/views\/movies\/index.json.rb\r\n  {\r\n    :identifier =&gt; Movie.primary_key,\r\n    :totalCount =&gt; @count,\r\n    # render @movies does not work as it insists on returning a string\r\n    :items =&gt; @movies.map { |m| render(m) }\r\n  }\r\n<\/pre>\n<p><\/code><\/p>\n<p><code><\/p>\n<pre>\r\n  # app\/views\/movies\/_movie.json.rb\r\n  {\r\n    :id =&gt; movie.to_param,\r\n    :title =&gt; movie.title,\r\n    :releaseDate =&gt; movie.release_date\r\n  }\r\n<\/pre>\n<p><\/code><\/p>\n<h2>Getting it<\/h2>\n<ul>\n<li><a href=\"http:\/\/github.com\/mschuerig\/ruby_template_handler\">github<\/a><\/li>\n<li>$ sudo gem install mschuerig-ruby_template_handler<\/li>\n<\/ul>\n<h2>Let your Rails app know about it<\/h2>\n<p>In the appropriate place in <code>config\/environment.rb<\/code> add<\/p>\n<p><code><\/p>\n<pre>\r\nconfig.gem \"mschuerig-ruby_template_handler\", :lib =&gt; 'ruby_template_handler'\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Didn&#8217;t you always want to write your Rails views as plain Ruby objects? &#8212; &#8220;What?&#8221;, I hear you say. No, I haven&#8217;t lost my mind and the idea is quite sensible (or so I hope), once you add the restriction &hellip; <a href=\"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/2009\/04\/02\/ruby-views\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-rails","category-ruby"],"_links":{"self":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":1,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":58,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/57\/revisions\/58"}],"wp:attachment":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}