Is there any way to render a partial only once. What I am trying to say here is I have a partial
_popup_message.html.erb. I am rendering this partial from multiple places on a same page. The problem is inside the partial I have few id attributes. So, if the partial is getting rendered multiple times on a page so HTML is giving error that id attribute is used twice or thrice.
So, is there any way in rails by which I can check whether that partial already rendered if yes then don't render it.
Here is my code
<div class="row">
<div class="medium-centered columns">
<div class="popup-message">
<%= dashboard_tile_message.html_safe %>
###so if the below partial got rendered once I don't want to render it
<%= render :partial => 'popup_message', locals: { user: current_user, setting: settings } %>
</div>
</div>
</div>
If someone got struggled, here the example what #max want to propose(haml):
- #rendered ||= false
- unless #rendered
#- Your partial code
- #rendered = true```
Related
I have a Page model which has a body property in which the following in stored as example:
<h1>This is the HTML body</h1>
<p>and we want to use some rails code </p>
<%= Time.current.to_s %>
In my controller I get this page and render it simple in my view like this
-- rest omitted to keep it clear
<div class="card-body">
<%= #post.body.html_safe %>
</div>
How can I make the rails part <%= Time.current.to_s %> to render into displaying the date?
I am new to rails and I want my carousel code to be seen in the index page only,please give a solution and below is my code :
<body>
<%= render 'layouts/navigation'%>
<%= render 'layouts/carousel' %>
<%= render 'layouts/messages'%>
<div class="container">
<div class="row">
<%= yield %>
<div class="col-md-8">
</div>
<div class="col-md-4">
</div>
</body>
Normally you'd want the move any code that's specific to a page out of the layout and into the template for the appropriate action.
However, in your case that piece of code looks embedded within the rest of the page skeleton so it could be hard to refactor it that way.
For this reason I'd suggest using conditional rendering by simply using a Boolean variable.
For example you could do:
<%= render 'layouts/carousel' if #render_carousel %>
And then you could just set the #render_carousel variable to true for any action that you'd want the carousel to appear in, e.g.:
def index
# rest of the code
#render_carousel = true
end
I am learning Ruby On Rails.I'd like to understand how RoR works. I have the following url when i want to edit user informations:
http://localhost:3000/users/3/edit
Here is my controller :
# GET /users/1/edit
def edit
end
The form is :
<div class="ui middle aligned center aligned grid">
<div class="column">
<h1>Profil</h1>
<div class="ui form segment">
<%= render "form" %>
</div>
</div>
</div>
<%= link_to 'Show', #user %> |
<%= link_to 'Back', users_path %>
I understand that the form is print by the code <%= render "form" %>. I'd like to know how to see what contains form ? Which informations are available in form ?
In your case
<%= render "form" %>
renders partial. You should find file with name _form.html.erb
or read more about partials in GUIDE
The render method is used to load a partial. A partial is basically repeated fragment of code in a view. To keep things dry, you separate it out in a partial and then pass it to the render method to render it.
In your case, the form is a partial.
You will most probably find it at app/views/users/_form.html.erb
Partials are always prefixed with a _ in their filename.
In my index page I have one partial:
Index-Site:
<div id="chapter_list">
<%= render 'icd1' %>
</div>
This partial _icd1 should have links to another partial _icd2.
Actually _icd1 links to a normal site:
<% #icd1.each do |f| %>
<%= link_to "#{f.von} - #{f.bis} #{f.bezeichnung}", icd_show_path(f), remote: true %>
<% end %>
So my first questionis how can i link to a partial _icd2 with the param "f"?
And next i would like that when a user clicks in the partial _icd1 on the link to _icd2, the partial _icd1 disappears and the partial _icd2 is rendered instead:
So that the index-Site looks like:
<div id="chapter_list">
<%= render 'icd2' %>
</div>
As you can see in my second code snippet my links already respond with ajax. But i have no clue how i can remove the _icd1 partial and display the partial icd2 instead, with js!! So that my icd2.js.erb file actually looks like this:
$('#chapter_list').fadeOut();
Thanks!
$('#chapter_list').html("<%= j render('icd2') %>");
TL; DR: How can I use the same layout multiple times on a page? All attempts to render partials with recurring layouts go into the elements where the first time the layout is used.
Quick Info:
Using
Rails 2.3.14
Ruby 1.8.7
My partials use content_for, which is pretty handy with layouts.
Basically, my layout looks like this:
<div class="header">
<%= yield :modal_header %>
</div>
<div class="body">
<%= yield :modal_body or yield %>
</div>
Generally, this is how I use my modal layout:
<% content_for :modal_header do %>
header text / elements
<% end %>
<% content_for :modal_body do %>
body info / settings for object or warning message, etc
<% end %>
This is how I render modals on my page:
<%= render :partial => "section_options", :layout => "modal" %>
and that works really well
BUT
when, on the same page, I try to render another modal:
<%= render :partial => "section_content_options", :layout => "modal" %>
This is what happens to the modal at the top of my page (occurs first in the HTML document)
<div class="header">
-- header from second partial --
-- header from first partial --
</div>
<div class="body">
-- body from second partial --
-- body from first partial --
</div>
and then later in the page, where the content from the second partial is supposed to be:
everything is rendered correctly ...
For the sake of this example:
<div class="header">
section content options
</div>
<div class="body">
section contents options ... options
lil confirm / cancel buttons
</div>
Is there a way to fix this? This behavior really messes with javascript bindings as well. so. yup.
Is there a bug with layouts in rails 2.3.14? do I have to upgrade to rails 3 to get rid of this problem?
Modals should be rendered using partials, not layouts. Layouts are designed as a way of defining the overall layout of the entire site, not specific parts. Something like a modal (which by design is specific) should be a partial that's rendered either through a condition or JavaScript.