how can i render rails code from a store HTML - ruby-on-rails

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?

Related

Render a partial only once

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```

I want my carousel to be appeared in index page only in rails application

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

Ruby on Rails : How to see information in render form?

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.

Rails what does this "yield" do?

What does the yield do in this snippet?
<body data-spy="scroll" data-target=".sidebar">
<!-- Your timezone is <%= Time.zone %> -->
<!-- <%= "Ruby Version is #{RUBY_VERSION}" if Rails.env =~ /test|development/ %> -->
<%= render partial:'shared/account_status' %>
<%= render partial:"shared/session_timeout" %>
<div class="container">
<%= render partial:"shared/branding" %>
<%= render partial:"shared/nav", locals:{icons:icons, actionable_urls:actionable_urls, top_level_items:MenuItem.top_level_items_with_access_rights_for_user(current_user).sort{|a, b| a.sequence <=> b.sequence}, current_item:current_navigation_item} %>
<div style="clear:both"></div>
<div id="content">
<%= render partial:"shared/flash", object:flash %>
<%= yield %>
</div>
</div>
<%= render partial:"shared/ldap_user_menu" if signed_in_as_ldap_user? %>
</body>
SOLUTION
see #Christian_Rolle 's answer below
The reserved Ruby key word yield is for processing closures (like a Proc or lamda). That said it is some kind of placeholder for processing some logic.
In Ruby on Rails view templating it is used for merging in partials. In the case of a layout file like the application.html.erb it merges in controller response templates like index.html.erb or show.html.erb.
Think of it as a placeholder for your controller response HTML in a global layout environment.
Read more at: Understanding yield
or about Ruby closures:
Do the Proc! ... a Ruby closure and
Have a lambda! ... a Ruby closure
okay ... lets talk about it in easy way , yield is like a placeholder or like a container . And while you make different parts of view and want to show it on any specific layout file , then you can just call that part on the yield section . And this is all it does .

Replace partial with another rails

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') %>");

Resources