If I have one <%= yield %>
tag then all my views render in the same place in the layout. Can I have different <%= yield %>
tags for different views? Is so how do I do this? Thanks
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- Android ContextMenu for View (Not Activity)
- How to specify memcache server to Rack::Session::M
相关文章
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
Yes, you can have multiple
<%= yield %>
tags. You can specify eachyield
tag with names like these in the base view.<%= yield :head %>
<%= yield :footer %>
Then use the
content_for
tag in your individual views.You can use yield and content for:
Look into ActionView::Helpers::CaptureHelper. You can do something like this in your views:
This will run the template inside the content_for block, but will not output as part of the regular template
yield
buffer, it will be stored in a separate buffer for later. Then later on, including in the layout, you can useyield :content_name
to output the content:So in a sense you can have different
yield
s for different views, you just have to give the differing content a name withcontent_for
in the views, and yield it with that same name in the layout.Consider your case, where you want different views in different places. Let's say you have three panels, panel1, panel2, and panel3. You can do this in your layout:
You don't even need to include a plain
<%= yield %>
if you don't want to. Then in your views, you can choose which panel to display the content in by surrounding the entire view with the appropriatecontent_for
. For example, one of your views might be changed like this:To show in panel 2. Another one might be intended for panel 3, like this: