How to I add an error with a line break? When I try adding \n it gets ignored, and when I try adding <br>, it just displays it.
Controller:
errors.add(:base, "This is the first line \n this is the second line")
View:
<% if message_form.errors.any? %>
<div class='row'>
<div class= 'col-md-12'>
<div id="error_explanation" >
<h5><%= pluralize(message_form.errors.count, "error") %> prohibited error prevented save:</h5>
<ul>
<% message_form.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
</div>
<% end %>
Probably you need to use <br> together with html_safe helper to mark that string as safe and skip all additional escaping.
However, you should take into account that if you mark as html_safe a string with user-provided inputs (or external inputs in general) you may be exposed to HTML injection or XSS attacks.
In case that is what you are trying to achieve, I would like to mention that you can associate more than one error to the same attribute, as explained in the Rails documentation.
Related
I'm running Rails v4.x and Ruby v2.3. I'd like to use a debugging tool (e.g., debugger) to dynamically test different execution paths in a Rails view. Specifically I'm trying to test the two paths of an if statement (ie., "if" block and "else" block) within a Rails view. Here is the code withing the View I'd like to test:
<% if #categories.any? %>
<% #categories.each do |category| %>
<ul class="listing">
<div class="row">
<div class="well col-md-4 col-md-offset-4">
<li class="article-title">
<%= link_to "#{category.name}", category_path(category) %>
<li><small><%= pluralize(category.articles.count, "article") %></small></li>
</li>
</div>
</div>
</ul>
<% end %>
<% else %>
<div class="center">
<p>There are no categories currently defined at this time</p>
</div>
<% end %>
To do this I'd like to be able to dynamically change the return value of the #categories.any? method so as to force the desired execution path. Is there a tool that I could use to do this? Can I do with "debugger"? Pry?. If so could you provide some simple instructions of how it could be done?
NOTE: #categories is an instant variable containing values from a data model (Category). I'd prefer not delete the contents of the Category table just to test the else part of the aforementioned code.
Regards,
Jet
Well, as you will have to edit the file anyway, to add a debugger line, you can just as well add another variable, can't you?
<% if (#categories.any? && !#never_run) || #always_run %>
I am trying to create a todo app that will allow the user to create lists and then "todo" items under each list. However, I want each user to only be able to see his or her lists. While I've been able to partially solve it using the current_user helper, the index page shows empty space where the other users lists are hidden.
Below please find the code for the index.html.erb page inside my todo_lists views.
<% #todo_lists.each do |todo_list| %>
<div class="index_row clearfix">
<% if todo_list.user == current_user %>
<h2 class="todo_list_title"><%= link_to todo_list.title, todo_list %></h2>
<p class="todo_list_sub_title"><%= todo_list.description %></p>
<p><%= todo_list.user.first_name %></p>
<% end %>
</div>
<% end %>
<div class="links">
<%= link_to "New Todo List", new_todo_list_path %>
</div>
Here's my repo on Github, in case you need to see more of the code: https://github.com/jramoscolon/todo
Is there a way to hide these empty spaces, as well as the non-matching todo items?
Given your current view code, you are indiscriminately emitting <div class="index_row clearfix"> elements, even when the todo_list.user does not match the current_user. Simply move the whole<div> outside the current_user check, like so:
<% #todo_lists.each do |todo_list| %>
<% if todo_list.user == current_user %>
<div class="index_row clearfix">
<h2 class="todo_list_title"><%= link_to todo_list.title, todo_list %></h2>
<p class="todo_list_sub_title"><%= todo_list.description %></p>
<p><%= todo_list.user.first_name %></p>
</div>
<% end %>
<% end %>
This way, all of those empty <div> elements aren't included on the page. This should clean up all that empty space.
If your index view is user specific than the instance variable you want should be user specific as well.
Instead of #todo_lists = ToDoList.all
Use the current_user.todo_lists functionality supplied by your has_many/belongs to
So let's say we have a rails view with this code:
<h1>Users who have the <%= #title.name %> Title</h1>
<ul>
<% #title.users.each do |user| %>
<li><%= user.username %> <div class="rw-ui-container rw-urid-X"></div></li>
<% end %>
</ul>
And what we want to do is to automatically print this line:
<div class="rw-ui-container rw-urid-X"></div>
Right aside each username the loop throws at us BUT we want X to be a different unique random number each time...
How can this be accomplished?
May be this solution can help you:
1. You can use user_id as first part of the random X to ensure that
another user doesn't have it
2. Second part is time in UTC format to set X different each time
3. Third part is just a standart random func. You haven't use it if you want.
<h1>Users who have the <%= #title.name %> Title</h1>
<ul>
<% #title.users.each do |user| %>
<li><%= user.username %> <div class="rw-ui-container rw-urid-<%= "#{user_id}#{Time.now.utc.to_i}#{rand(100000)}".to_i %>"></div></li>
<% end %>
</ul>
css_arr = ["rw-ui-container rw-urid-1","rw-ui-container rw-urid-2","rw-ui-container rw-urid-3"]
<div class="<%= css_arr.sample %>"></div>
Well You could easily add a new field to user table call
uuid or give it any name that you like and assign a unique id using the gem called uuid
This help ensure that you have uniqueness in X section and also on refresh the value wont change as you mention in one of your comment
so here how your X code would look like
<% #title.users.each do |user| %>
<li><%= user.username %> <div class="rw-ui-container rw-urid-<%=user.uuid %>"></div></li>
<% end %>
I'm writing some if/elsif statements to display error messages. When I leave the username blank and fill the other fields in correctly, it still goes to the else statement and displays "All fields must be completed." Can someone point out the error in my logic/syntax?
<% if #user.errors.any? %>
<div id="error_explanation" class="round">
<% if #user[:name].nil? and #user[:email].not.nil? and #user[:password].not.nil? and #user[:password_digest].not.nil? %>
<h2>Please enter a valid username. </h2>
<% else %> <!--If any field is left blank -->
<h2>All fields must be completed. </h2>
<% end %>
</div>
<% end %>
Let me know if you need anything else.
I think railscasts has great episode for validations http://railscasts.com/episodes/211-validations-in-rails-3
Check it out, your current approach is sure to make some troubles in the future where you need to check validations a lot.
Try this
<% if #user[:name].nil? and !#user[:email].nil? and !#user[:password].nil? and !#user[:password_digest].nil? %>
I've noticed that in some lines of rails views, this is used:
<% # Code... -%>
instead of:
<% # Code... %>
What is the difference?
<ul>
<% #posts.each do |post| -%>
<li><%=post.title%></li>
<% end -%>
</ul>
There will be no new lines in between the <ul> and first <li> and the last closing </li> and </ul>. If the - was omitted, there would.
The different options for evaluating code in ERB are as follows (they can be accessed in Textmate using Ctrl-Shift-. ):
<% %> Just evaluate the contents.
<%= %> Evaluate the contents and puts the result.
<%= -%> Evaluate the contents and prints the result.
<%# %> The contents is treated as a comment and not outputted.
Notice the difference between puts and print. Puts always adds a new line at the end of the string whereas print doesnt.
Basically, the -%> says don't output a new line at the end.
Consider this
<div>
<% if #some_var == some_value %>
<p>Some message</p>
<% end %>
</div>
The code above yields to the HTML below if the #some_var is some_value
<div>
<p>Some message</p>
</div>
If you've put - in the closing tag, then the ERB interpreter would remove the new lines for those with code tag including - and result in the following
<div>
<p>Some message</p>
</div>
This is useful if you need to have a good looking code for HTML. Sometimes you'll find it useful when working sideby side with a designer
Hope this helps.
A little late, but I think it's worth pointing out that you can also do this:
<%- #posts.each do |post| -%>
<li><%= post.title %></li>
<%- end %>
This strips away any whitespace in front.