Rails: How can i add elements in application.html.erb layout instead of typing hard coded html element?
How can i add elements i开发者_如何学Cn application.html.erb layout instead of typing hard coded html element ?
Is it possible to do this like we can do this in cakephp? http://book.cakephp.org/view/1081/Elements
Sounds like Rails Partials or helpers
You could use helpers for small snippets or even partials for larger html chunks. The cakepho element looks more similar to partials. In rails you would have a partial for rendering a component like a nav in _nav.html.erb and it may require a few local variables to render. In your application.html.erb you would render the partial:
<%= render :partial => 'nav', :locals => { :var1 => 'someval', :var2 => someobj } %>
This would be a great resource for you to read: http://guides.rubyonrails.org/layouts_and_rendering.html
Helpers are ruby methods you write to help create views. Here's a useful one:
def sentence_links objects, label_method = :name
objects.map { |o| link_to(o.send(label_method), o) }.to_sentence.html_safe
end
And the in your views you can call sentence_links Player.limit(3)
to output:
<a href="/players/15692665">Pearlie Gulgowski</a>, <a href="/players/34801824">Mr. Tommie O'Conner</a>, and <a href="/players/44139906">Wyman Schumm</a>
精彩评论