Rails 3 inline errors - ruby-on-rails-3

I'm new in rails 3. I'm using bootstrap and i want to display the validation errors next to the invalid field.
here is the code
<div class="control-group <%= #pet.errors.has_key?(:nick) ? "error": "" %>">
<%= f.label :nick, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :nick, :class => 'text_field' %>
<% unless !#pet.errors.has_key?(:nick)%>
<span class="help-inline"><%= #pet.errors[:nick]%></span>
<%end%>
</div>
</div>
my problem is that <%= #pet.errors[:nick]%>returns
["no puede estar en blanco"]
and must be just
no puede estar en blanco
here you have an image
http://img848.imageshack.us/img848/8263/inlineerror.jpg
is there any way i can get the error message without the square brackets and the quotation marks?
Thanks in advance

You could try:
<span class="help-inline"><%= #pet.errors[:nick].join(", ")%></span>

The square brackets around the string indicate that it's an array. You could also get rid of the brackets and quotation marks by writing
<%= #pet.errors[:nick][0]%>

Related

EJS syntax error while compiling ejs code in Create Form

I have a syntax error when I try to render this part of my template with EJS (on a node server).::
I'm trying to show a view of a create form, so when the user leaves a blank input or doesn't select the dropdown option gets an error (using express validator).
I works fine with an input, validates an error and keeps the data of all other inputs (so user won't have to retype all over again)
But when using the option select, it's getting an error. Also the data should persist in the option select IF it's in blank or error in any other fields.
I'm quite sure the error is here:
<label class="combo-form_titles_create" for="os_min">OS Minimum Requirement</label>
<select
name="os_min"
class="textArea__Create <%= locals.errors && errors.os_min ? : null %>">
<option value="">Choose an Operating System</option>
<%
let systems = ['Windows XP', 'Windows Vista', 'Windows 7', 'Windows 10'];
%>
<% for (const oneSystem of systems) { %>
<option
value="<%= oneSystem %>"
<%= locals.oldData && (oneSystem == oldData.os_min) ? 'selected' : null %>
> <%= oneSystem %> </option>
<% } %>
</select>
<% if (locals.errors && errors.os_min) { %>
<div class="text-danger">
<%= errors.os_min.msg %>
</div>
<% } %>
When using the inputs only there is no error
<input class="textArea__Create" type="text" name="os_rec" placeholder="OS Recommended Requirement" value="<%= locals.oldData ? locals.oldData.os_rec : null%>"/>
<% if(locals.errors && errors.os_rec){ %>
<p class="tableAdmin_Error"><%= errors.os_rec.msg %> </p>
<% } %>
found it, it's in the line 4
class="textArea__Create <%= locals.errors && errors.os_min ? : null %>">
in the ternary operator Im not passing anything if true

Syntax Error in code for 'bootstrap Custom forms' in Ruby

<%= form_for :content, url: contents_path, method: :post do |f| %>
<%= f.label :inout %> <!-- 수입 지출 목록 -->
<%= f.text_field :inout %><br>
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Options</label>
</div>
<select class="custom-select" id="inputGroupSelect01">
<option selected>Choose...</option>
<option value="1">결혼식</option>
<option value="2">장례식</option>
<option value="3">돌잔치</option>
</select>
</div>
<% if option == 1? %>
<%= f.hidden_field :category, value=>"결혼식" %>
<% end %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :cost %>
<%= f.text_field :cost %>
<%= f.label :memo %>
<%= f.text_area :memo %>
<%= f.submit %>
<% end %>
I'm getting the following syntax error in my code :
Unexpected ";", expected ";" .... syntax error, unexpected keyword_ensure, expecting_end_of_input
enter image description here
If I select option value one, I store "결혼식" in category
and I want to see "결혼식" in the show view
Can you identify what is causing the error?
Did you copy and paste the code from somewhere else? It's quite possible there might be some zero-width space or other hidden characters in your code that the ruby compiler is not liking.
You can use sublime text on OS/X or Notepad++ to try and see if there are hidden characters in your code.
If you're on OS/X or Linux, you can use Sublime Text and this plug-in to identify those characters
https://pastebin.com/ehWxNfMe
# http://sublimetext.userecho.com/topic/104394-is-it-possible-to-show-all-characters-spaces-tabs-cr-lf-etc/#comment_180170
import sublime_plugin
class ShowZeroWidthSpace(sublime_plugin.EventListener):
def on_modified(self, view):
spaces = []
p = 0
while 1:
s = view.find(u'\u200b', p + 1)
if not s:
break
spaces.append(s)
p = s.a
if spaces:
view.add_regions("zero-width", spaces, "string")
else:
view.erase_regions("zero-width")
If you're on Windows, you can try using Notepad++ and changing the encoding to Encode in utf-8 BOM
If allo , you can also create a new ruby file and type in the code manually to ensure there's no zero-width or invisble characters that ruby doesn't like.
Rails 4 - syntax error, unexpected tIDENTIFIER, expecting end-of-input
How to remove non-printable/invisible characters in ruby?

link_to current page rails

I was wondering what the syntax is to link to an attribute on the same page
I have a list of dates
<ul>
<% #response.each_pair do |date, movie| %>
<li><%= link_to date_format(date), date, :class => 'scroll_to' %></li>
<% end %>
</ul>
These then have movies underneath each date like so
<% #response.each_pair do |date, movie| %>
<h3 class="resultTitle fontSize13">Available on <%= date_format(date) %></h3>
<% movie.each do |m| %>
<div class="thumbnail clearfix">
<img class="pull-left" src=<% if m.image_link %> <%= m.image_link %> <% else %> "/assets/noimage.jpg" <% end %>>
<div class="caption pull-right">
<%= link_to m.name, m.title_id, :class => 'resultTitle fontSize11' %>
<p class="bio"><%= m.bio %></p>
<p class="resultTitle">Cast</p>
<p class="bio"><%= m.cast.join(", ") unless m.cast.empty? %></p>
<%= link_to "Remind me", reminders_path(:title_id => m.title_id), :method => :post, :class => 'links button' %>
</div>
</div>
<% end %>
<% end %>
What i would like to achieve is that when a user clicks on a date in the list then it will take them to that date with movies on the same page
My attribute for each date is
"release_date"
Do i need to link to that, maybe a piece of Jquery aswell to scroll down to that date? or would it jump to the date in one step?
Any advice appreciated, I have linked to other pages before but not the same page like this
EDIT
I have tried this but the page just re renders
<li><%= link_to date_format(date), params.merge(:release_date => 'release_date'), :class => 'scroll_to' %></li>
Am i on the right track?
Thanks
All you need is to give your date a unique identifier and link to that in you list. For instance below we give the h3 for each date an id relative to the date object. The browser knows how to handle internal links and will simply jump to the corresponding identifier. Notice how the id of the field you're linking to gets appended to the end of the url when you click on the link.
<ul>
<%- index = 0 %>
<% #response.each_pair do |date, movie| %>
<%- index += 1 %>
<li><%= link_to date_format(date), "##{index}", :class => 'scroll_to' %></li>
<% end %>
</ul>
<%- index = 0 %>
<% #response.each_pair do |date, movie| %>
<%- index += 1 %>
<h3 class="resultTitle fontSize13" id="<%= index %>">Available on <%= date_format(date) %></h3>
<% movie.each do |m| %>
<div class="thumbnail clearfix">
<img class="pull-left" src=<% if m.image_link %> <%= m.image_link %> <% else %> "/assets/noimage.jpg" <% end %>>
<div class="caption pull-right">
<%= link_to m.name, m.title_id, :class => 'resultTitle fontSize11' %>
<p class="bio"><%= m.bio %></p>
<p class="resultTitle">Cast</p>
<p class="bio"><%= m.cast.join(", ") unless m.cast.empty? %></p>
<%= link_to "Remind me", reminders_path(:title_id => m.title_id), :method => :post, :class => 'links button' %>
</div>
</div>
<% end %>
<% end %>
For the added JQuery transition you can replace
<li><%= link_to date_format(date), "##{index}", :class => 'scroll_to' %></li>
with
<li><%= link_to_function date_format(date), "$('html, body').animate({scrollTop:$('##{index}').offset().top }, 'slow');", :class => 'scroll_to'%></li>
The above isn't exactly a DRY approach, but the important thing to abstract out of that is that you're linking to a unique ID elsewhere on the page and not the code itself.

How to display dynamic content in rails label without using javascript

I can't find how to display a dynamic label in Rails, i've tried using the :value => show_name property but it didn't work, it only displays Show name. Here is the view code
<p>
<div class="control-group">
<%= f.label :show_name, :value => :show_name, :class => 'control-label' %>
<%= #this next line fails with undefined method `show_name' for #<ActionView::Helpers::FormBuiler>
#f.label f.send :show_name, :class => 'control-label'
%>
<div class="controls">
<%= f.text_field :variable_value, :class => 'text_field' %>
<%= f.hidden_field :variable_id, :class => 'text_field' %>
<%= f.hidden_field :show_name, :class => 'text_field' %>
</div>
</div>
<p>
and if needed here is the show_name definition inside my model.
def show_name
Variable.find_by_id(self.variable_id).name
end
Ok, so i end up finding a solution that is very DRY, thank to this post. And the only thing im going to do is explain a lit bit more what to do:
First we are going to asume the most complex case in which we have nested forms and so we are using fields_for inside a form_for method:
<!-- f represents the form from `form_for` -->
<%= f.fields_for :nested_model do |builder| %>
<p>
<div class="control-group">
<!-- here we are just calling a helper method to get things DRY -->
<%= builder.label return_value_of_symbol(builder,:show_name), :class => 'control-label' %>
<div class="controls">
<%= builder.text_field :variable_value, :class => 'text_field' %>
<%= builder.hidden_field :variable_id, :class => 'text_field' %>
</div>
</div>
</p>
<% end %>
Note that we included the builder object(as specified in the fields_for call) in the parameters of our helper.
In our helper we define the return_value_of_symbol function
def return_value_of_symbol(obj,sym)
# And here is the magic, we need to call the object method of fields_for
# to obtain the reference of the object we are building for, then call the
# send function so we send a message with the actual value of the symbol
# and so we return that message to our view.
obj.object.send(sym)
end
Use label_tag, put the show_name on a instance variable on your controller and use like this:
<%= label_tag #show_name, nil, :class => 'control-label' %>
EDIT:
On your application_helper.rb, create a helper method similar to this one:
def show_name(name)
content_tag(:label, name, :class => 'control-label')
end
Then you can use the show_name(name) on your views like this:
<%= show_name(#name) %>
Just remember to populate the #name variable.

Is it possible to pass html elements into a partial?

I have a partial that contains a header, a subheader and potentially one or more buttons. It is used on many different pages. Often the pages don't need buttons, so I just pass in the header and an optional subheader to the partial. However sometimes the pages need one or more buttons and the only way I've managed to allow for an arbitrary number of buttons to be passed in is using content_for. My partial looks like this:
<% if defined? page_title %>
<header class="pageHeader">
<div class="page-details">
<h3><%= page_title %></h3>
<% if defined? page_subtitle %>
<p><%= page_subtitle %></p>
<% end %>
</div>
<ul class="crud-menu nav-pills">
<%= content_for :page_header_buttons %>
</ul>
</header>
<% end %>
This use of content_for nasty. Is there any way I can pass the list items / buttons into this partial? How else could I deal with this situation?
You could transform this partial into a layout:
<% if defined? page_title %>
<header class="pageHeader">
<div class="page-details">
<h3><%= page_title %></h3>
<% if defined? page_subtitle %>
<p><%= page_subtitle %></p>
<% end %>
</div>
<ul class="crud-menu nav-pills">
<%= yield %>
</ul>
</header>
<% end %>
And you would render it like that:
<%= render layout: "your_partial_above", locals: { page_title: "page title } do %>
<%= render "partial_with_your_buttons" %>
<% end %>