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.
Related
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 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?
In my quest to keep my application views as DRY as possible I've encountered a little snag. My appliation.html.erb incorporates a static sidebar menu. Each of my main controllers incorporates a secondary sidebar menu (essentially a submenu). I can take the code that renders the menu out of application.html.erb and put it in each of my views and change the secondary sidebar there, but this produces a lot repetition in my views.
I saw this SO post and looked at this page, but I was unable to get either idea to work. I was thinking that I could put something like:
<% provide(:submenu, 'layouts/sidebars/sidebar_customers_contacts') %>
at the top of each view and use that to render the associated partial by doing
<% content_for(:submenu) do %>
<%= render :partial => :submenu %>
<% end %>
from the application.html.erb but of course that didn't work.
This is my current application.html.erb:
<div class="side">
<%= render 'layouts/sidebar' %>
<%= render 'layouts/sidebars/sidebar_dashboard' %><!-- this needs to load a sidebar based on the controller that calls it. Each view of the controller will get the same sidebar. -->
</div>
<div class="main-content">
<%= yield %>
</div>
I feel like I'm making this more difficult than it really is. Is there a simple way to do this?
Rails provides a helper called controller_name which you can read more about here.
Assuming you adhere to your own naming conventions, this should work as-is. If you decide some controllers don't get a sidebar, you may need to throw in some conditionals...
application.html.erb
<div class="side">
<%= render "layouts/sidebar" %>
<%= render "layouts/sidebars/#{ controller_name }" %>
</div>
<div class="main-content">
<%= yield %>
</div>
EDIT
Sorry, my mistake was using single quotes instead of double-quotes. You cannot use #{string interpolation} within single quotes. Source
This is the error I'm getting:
ActionView::MissingTemplate in Posts#index
Missing partial text_posts/_text_post with {:locale=>[:en], :formats=>[:html],...
Extracted source (around line #5):
3 </div>
4
5 <%= render #posts %>
Here's the code in the file app/views/posts/index.html.erb
<div class="page-header">
<h1>Home</h1>
</div>
<%= render #posts %>
I'm following along the 'Rails Crash Course' book and this is one of the steps to create a social network. I don't know the reason for the error.
I am assuming that in your posts_controller.rb file you have specified the following:
def index
#posts = Post.all
end
The reason why you are getting this error is because Rails is trying to display a partial for the variable #posts. What is implicit here, is that Rails is looking for a file named _post.html.erb in the folder app/views/posts/ and not finding anything. To fix this, you will have to manually create the file in that directory (Note that all partials begin with an underscore).
Now, if you are wondering what you should put in this partial, first you should know what <%= render #posts %> is doing. When it goes to the partial, it is iterating over all your posts and for each of them, it is going to do something. So your partial may look something like this:
<p>
<%= link_to post.title, post_path(post.id) %>
</p>
<p>
<%= link_to "Edit", edit_post_path %>
</p>
<p>
<%= link_to "Delete", post_path(post.id), method: :delete %>
</p>
Just know that what is implicit here, is that we already have a #posts.each do |post| given for us, so you only have to define whatever content you wish to display for each individual post.
Hope this helps!!
As you're using <%= render #posts %>, i'm am sure this will call the partial file with leading _ symbol on same directory. Please ensure you have already file with the name _posts.html.erb. IF you want to pass the value into partial, here i give you simple illustration :
Assume you have this in customer_controller :
def show
#customer= Customer.find(params[:id])
end
Then it instance will available on show.html.erb :
<p>Item: <%= #customer.name%></p>
These instance variables are available in layouts and partials as well, instead pass variables to partials as locals like:
// here is the same concept with your problem
// render "cust_name" find the filename leading with underscore
<%= render "cust_name", name: #customer.name%>
Above line of code will call the file with name _cust_name.html.erb that contains :
<h1><%= name %></h1>
Hope this helped.
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') %>");