{"id":263,"date":"2014-09-25T14:19:24","date_gmt":"2014-09-25T14:19:24","guid":{"rendered":"https:\/\/www.lianeallen.com\/home\/?p=263"},"modified":"2014-09-25T14:19:24","modified_gmt":"2014-09-25T14:19:24","slug":"view-helpers-for-noobs","status":"publish","type":"post","link":"https:\/\/www.lianeallen.com\/home\/2014\/09\/view-helpers-for-noobs\/","title":{"rendered":"Rails 4 View Helpers for Noobs"},"content":{"rendered":"<p>A view helper is a little piece of reusable code that you can place into your views to reduce the amount of code you have to repeat. You&#8217;ve probably already used some, such as form helpers (form_for) or others (image_tag). It&#8217;s also possible to make your own. If there&#8217;s something you do over, and over again, in multiple views, it&#8217;s worth considering making a helper to keep your code DRY and your typing fingers happy. <\/p>\n<p>If you&#8217;ve never made a view helper before (or if you were shown how weeks ago, but forgot everything you learned, heh), it can seem a little intimidating. If you&#8217;re searching the internet for info, you&#8217;ll find lots of info related to older versions of rails, but little related to Rails 4. <\/p>\n<p>Here are a few simple things to remember, and a nice resource that, combined, make it a piece of cake:<\/p>\n<ol>\n<li>The file must be placed in the app\/helpers directory, and the file name must end with &#8220;_helper.rb&#8221;. I made one for centering images:<br \/>\n<blockquote><p>app\/helpers\/centering_helper.rb<\/p><\/blockquote>\n<\/li>\n<li>The file name and module name must match in the same way controller names and class names match:<br \/>\n<blockquote><p>centering_helper.rb <br \/>module CenteringHelper<\/p><\/blockquote>\n<\/li>\n<li>If you&#8217;re including html code in your helper, it needs to be wrapped with the raw method:<br \/>\n<blockquote><p><code>raw('&lt;a href=\"https:\/\/www.lianeallen.com\/home\/2014\/09\/view-helpers-for-noobs\">Rails 4 View Helpers for Noobs&lt;\/a>')<\/code><\/p><\/blockquote>\n<p>If you don&#8217;t wrap it in the raw method, rails is going to treat special characters as if they&#8217;re supposed to be text, and will escape them: <\/p>\n<blockquote><p>\\<, \\>, &#8230; etc.<\/p><\/blockquote>\n<p>so whatever you enter is going to be printed out as text in the view. <\/p>\n<p>Unwrapped html as it will appear in your view: <\/p>\n<blockquote><p>&lt;a href=&#8221;https:\/\/www.lianeallen.com\/home\/2014\/09\/view-helpers-for-noobs&#8221;>Rails 4 View Helpers for Noobs&lt;\/a><\/p><\/blockquote>\n<p>Wrapped html as it will appear in your view:<\/p>\n<blockquote><p><a href=\"https:\/\/www.lianeallen.com\/home\/2014\/09\/view-helpers-for-noobs\">Rails 4 View Helpers for Noobs<\/a><\/p><\/blockquote>\n<\/li>\n<li>And here&#8217;s a nice visual tutorial that will walk you through the simple process of creating and using your new helper: <br \/>\n<a href=\"http:\/\/www.rails-dev.com\/custom-view-helpers-in-rails-4\" title=\"Custom view helpers in rails 4\" target=\"_blank\">http:\/\/www.rails-dev.com\/custom-view-helpers-in-rails-4<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>A view helper is a little piece of reusable code that you can place into your views to reduce the amount of code you have to repeat. You&#8217;ve probably already used some, such as form helpers (form_for) or others (image_tag). It&#8217;s also possible to make your own. If there&#8217;s something you do over, and over &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.lianeallen.com\/home\/2014\/09\/view-helpers-for-noobs\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Rails 4 View Helpers for Noobs&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":281,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,2,3],"class_list":["post-263","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-how-to","tag-rails-4","tag-view-helpers"],"_links":{"self":[{"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/posts\/263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":10,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":283,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/posts\/263\/revisions\/283"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/media\/281"}],"wp:attachment":[{"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lianeallen.com\/home\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}