Rails: comments - ruby-on-rails

I want to comment out something in the application.html.erb
<!--
<div id="user_nav">
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. Not you?
<%= link_to "Sign out", destroy_user_session_path %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or
<%= link_to "Sign in", new_user_session_path %>
<% end %>
</div>
//-->
this ist not possible. What is to do?

That's an HTML comment; ERB processing happens on the server side.
<%#
%>
May work across block-ily, it will certainly work line-by-line.
That said, I'm not sure I'd remove functionality by commenting out the entire section.
Instead consider either (a) using source control, or (b) rendering a partial and commenting that out, instead of large chunks of ERB.

For .html.erb file you can use following code for comment.
<!-- Your Comment here -->

Related

Ruby on Rails - Underline words if they appear in dynamically generated text using Ruby

I am trying to underline words that are dynamically generated by the debug(params) method provided by rails. I have something below, but it obviously does not work, plus what I have below is attempt to try and change the words using methods that I already know about (like the .upcase method). I was hoping to underline the word controller if it appears in the text using only Ruby. Can anyone help me out here?
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<%= 'controller'.upcase %>
<% end %>
thanks
edit:
I should add that debug(params) is a method defined by RAILS, I was able to do the following which seems even more off, so far the answers have not been correct to what I want to do.
<% if Rails.env.development? %>
<% debug_method = debug(params).split.each do |word| %>
<% if word == 'controller:' %>
<ul><% word.upcase %></ul>
<% end %>
<% end %>
<%= debug_method.join %>
<% end %>
which returns the following text: https://ibb.co/cvnEpw , keep the answers coming in though. I want to get the words in the original box (that's generated by the method to underline the controller word https://ibb.co/jmSm2G).
use <u></u> tag
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<u><%= 'controller'.upcase %></u>
<% end %>
example here
Provide the css to generate html element:
p { text-decoration: underline; }
Add html elemnt to wrap your words:
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<p> <%= 'controller'.upcase %> </p>
<% end %>
The answer to the question is below. I had to use the .gsub and .html_safe methods.
<%= debug(params).gsub("controller:", "<u>controller:</u>").html_safe %>
This code keeps the existing html & css generated by rails intact

Why can't I render a partial in Exercise 1 of Chapter 11 of Michael Hartl's Rail Tutorial Book?

I'm trying to complete Exercise 1, Chapter 11 of Michael Hartl's Rail Tutorial Book, but when I load the page in a browser I don't see my partials rendered.
The brief is:
Refactor the Home page to use separate partials for the two branches of the if-else statement.
The homepage template in which I am trying to load partials is as follows:
<% if logged_in? %>
<% render 'shared/home_logged_in' %>
<% else %>
<% render 'shared/home_not_logged_in' %>
<% end %>
An example of one of the partials that I am trying to load is as follows:
<div class="center jumbotron">
<h1>Welcome to the Sample App</h1>
<h2>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</h2>
<%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>
<%= link_to image_tag("rails.png", alt: "Rails logo"),
'http://rubyonrails.org/' %>
My code is available on GitHub here and the changes I have made in an attempt to complete this exercise can be found here.
What am I doing wrong?
You are using <% render 'shared/home_logged_in' %> instead of <%= render 'shared/home_logged_in' %>. You need to print the expression into the page buffer for it be included in the response.
<% "I am invisible" %>
<%= "I am not" %>

same rails path displays different template if flash messages are present

I came across an interesting problem when I try to alter what gets display if page is certain route'
<body>
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
<!---both logged_in and is_admin are in session_helper-->
<%if current_page?(login_path) or current_page?(signup_path)%>
<p class = "title">CLOUD SOLAR</p>
<% else %>
<% if logged_in?%>
<% if is_admin?%>
<%= link_to "All Users", users_path%>
<% end%>
<%= link_to "Settings", edit_user_path(current_user)%>
<%= link_to "Log Out", logout_path, method: "delete"%>
<% else %>
<%= link_to "Sign Up", signup_path%>
<%= link_to "Log in", login_path%>
<% end %>
<% end %>
<%= yield %>
<!-- <%=debug(params) if Rails.env.development?%> -->
</body>
In the code above, I am attempting to get rid of the "sign up" and "log in" link if the current page is either sign up or log in. The above code works fine until I test it with an incorrect email and password. When I test it with a wrong password, a flash message pops up (which it should!), but for some reason the Sign Up and Log in link also pops up underneath it also.
Given the code above, I thought that if the current route is login_path or signup_path, it will never get into the else part of the condition, but somehow it did.
What is going on here? Is the route somehow being altered when I input wrong information even though the url still displays localhost:3000/login or localhost:3000/signup ??
Edit: For clarification, the title disappears completely if the login form has some sort of error. What is replaced is a new login-form and a flash error message. Where did the title CLOUD SOLAR go?
You could try changing login_path and signup_path to login_url and signup_url. Another option would be to hardcode the paths.
current_page?(Rails.env.production? ? 'http://your_live_url/signup' : 'http://localhost:3000/signup')
Also, you could check the documentation here:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page-3F

Why the use of both <% and <%= in the views?

If I write something like:
<% if signed_in?.blank? %> or <%= link_to "Sign Up", sign_up_path %>
What is the difference between the two signs of <% and <%=?
Why make it this way instead of using just one for simplicity?
When do I know I need to use <% over <%=?
<%= puts the return value of the code inside to the page.
<% just execute code.
Here is the good guide about ERB http://api.rubyonrails.org/classes/ActionView/Base.html
<% %> Simply executes the statement(s) inside that block, whereas <%= %> will output the result of the statement.
So for example, with the <% if signed_in?.blank? %>, the ruby interpreter just executes that code and checks if signed_in is blank.
The <%= link_to %> statement will actually generate HTML.

How do I wrap link_to around some html ruby code?

How do I wrap a link around view code? I can't figure out how to pass multiple lines with ruby code to a single link_to method. The result I am looking for is that you click the column and get the show page:
<div class="subcolumns">
<div class="c25l">
<div class="subcl">
<%= image_tag album.photo.media.url(:thumb), :class => "image" rescue nil %>
</div>
</div>
<div class="c75r">
<div class="subcr">
<p><%= album.created_at %></p>
<%= link_to h(album.title), album %>
<p><%= album.created_at %></p>
<p><%= album.photo_count %></p>
</div>
</div>
</div>
link_to takes a block of code ( >= Rails 2.2) which it will use as the body of the tag.
So, you do
<%= link_to(#album) do %>
html-code-here
<% end %>
But I'm quite sure that to nest a div inside a a tag is not valid HTML.
EDIT: Added = character per Amin Ariana's comment below.
Also, this may be an issue for some:
Make sure to write <%= if you are doing a simple link with code in it instead of <%.
e.g.
<%= link_to 'some_controller_name/some_get_request' do %>
Hello World
<% end %>
For older Rails versions, you can use
<% content_tag(:a, :href => foo_path) do %>
<span>Foo</span>
<% end %>
You can use link_to with a block:
<% link_to(#album) do %>
<!-- insert html etc here -->
<% end %>
A bit of a lag on this reply I know -- but I was directed here today, and didn't find a good answer. The following should work:
<% link_to raw(html here), #album %>

Resources