EJS syntax error while compiling ejs code in Create Form - express

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

Related

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?

Rails 3 inline errors

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]%>

RoR no route matches

I have this code in my routes:
controller :active_car do
put 'switch_car' => :update
get 'switch_car' => :edit
end
This is my code in on my edit page.
<% form_tag('switch_car', :method => :put) do%>
<div class="field">
<label for="car_info_id">Car Name:</label>
<%= select("", "car_info_id", #available_cars.collect {|v| [v.name, v.id]})%>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
When I click submit I get the following routing error.
No route matches "/switch_car" with the url pointing to http://localhost:3000/switch_car?method=put
The get is working just fine as I end the url with switch_car I get my page to edit. For some reason the put definition is not working.
After changing method to second argument it just doesn't work. It seems to have post as the method still instead of put. Here is generated HTML
<form accept-charset="UTF-8" action="switch_car" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" />
:method belongs to the "options" hash, which is form_tag's second argument, not the first.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag
Please inspect the html generated by that tag and post it here.

RoR Difficulty accessing params in controller

I have an edit page with the following code, basically a dropdown and button that calls update.
<% form_tag('switch_car', :method => :put) do%>
<div class="field">
<label>Car Name:</label>
<%= select("params", ":id", #available_cars.collect {|v| [v.name, v.id]})%>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
The server reads like the params is being set:
Parameters: {"utf8"=>"Γ£ô", "authenticity_token"=>"8vHXrnICaOKrGns6FfMUcd/dWo5kpNKpA8F5l5ozRkY=", "params"=>{":id"=>"9"}, "commit"=>"Switch Car"}
However, when I put the params into a session I get nothing. It seems to be always nil. Not sure what I am doing wrong? Here is code in the controller.
def update
if params[:id]
session[:car_info_id] = params[:id]
redirect_to entry_url
else
redirect_to switch_car_path
end
end
It always gets redirected to the switch_car_path so I am assuming params[:id] is always nil. When I put if params[:id] == nil it goes to the entry_url.
Thanks in advance.
you want params[:params][":id"]
Alternatively, you could put this in your view:<%= select("car_info", "id", #available_cars.collect {|v| [v.name, v.id]})%>
And then in your controller:if params[:car_info][:id]
While the other answer would work, this is probably what you'd want to be doing (using select_tag(:id) will automatically add an :id key/value to the params hash):
<% form_tag('switch_car', :method => :put) do %>
<div class="field">
<label>Car Name:</label>
<%= select_tag(:id, options_from_collection_for_select(#available_cars, "id", "name")) %>
</div>
<div>
<%= submit_tag "Switch Car" %>
</div>
<% end %>
Then you can easily access params[:id] in the controller.

Weird rendering of .html.erb file

So I have this file:
<h1>Calendar view</h1>
<div class="events">
<% #events.each do |e| %>
<%= raw(e.content)%>
<% end %>
</div>
<br />
<div class="messages">
<% #messages.each do |m| %>
<%= raw(m.content)%>
<% end %>
</div>
With #events and #messages being valid instance variables in the controller...but when I go to the page the html looks like this:
<h1>Calendar view</h1>
<div class="events">
<br>
<div class="messages">
This is another message test
</div
Event Content
</div>
I'm confused. Maybe I'm missing something obvious?
The problem is that raw() will output raw HTML content. The Rails template engine will try to merge that with the .erb template you supplied.
Therefore, if either m.content or e.content are malformed, you will most likely get unexpected output.
The best way would be to look for syntax errors, especially missing closing elements.