Ruby on Rails: Encountered a syntax error while rendering template: - ruby-on-rails

Good night.
I'm doing the getting started guide of RoR, but I have a trouble:
When I click the New Article button that redirects to the form, Rails throw an 'Encountered a syntax error while rendering template.'
The new.html.erb code is
<h1>New Article</h1>
<%= form_with scope: :article, url: articles_path, local: true do |form| %>
<%= if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %>
prohibited this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<P>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
The code is the same as the guide's code, so i don't know where the error or errors can be.
I also attach the error screenshot if it's any help.
Rails error message when I want tho create a new Article

Here two problems:
wrong syntax in if condition (don't need to use =)
extra \n chars in your screenshot
Change your 5 line in app/view/articles/new.html.erb to:
<% if #article.errors.any? %>
<% %> executes Ruby code
<%= %> executes Ruby code and prints out
Also read about the difference on SO

Related

Rails: syntax error, unexpected end, expecting ')' '.freeze; end ^~~

I'm creating a very basic rails app (learning tutorial) and can't understand why I'm getting this error.
I've tried troubleshooting but to no avail.
<div id="page_wrapper">
<h1> Make Something </h1>
<%= form_for :post, url: posts_path do |f| %>
<% if #post.errors.any? %>
<div id="errors">
<h2> <%= pluralize(#post.errors.count, "error" %> stopped this post from saving </h2>
<ul>
<% #post.errors.full_message.each do |msg| %>
<li> <%= msg %> </li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %> <br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %> <br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</div>
I expect the better UI error messages, but no idea what my error is - I'm sure it's a very minor syntax fix, but your help would be much appreciated.
You're not closing the brackets when calling pluralize
change
<h2> <%= pluralize(#post.errors.count, "error" %> stopped this post from saving </h2>
to
<h2> <%= pluralize(#post.errors.count, "error") %> stopped this post from saving </h2>

Rails view error (on nitrous io IDE) syntax error, unexpected keyword_ensure, expecting end-of-input

i'm having problems with my code ,i get this message above.here is my code .i'm new to ruby
app/views/articles/edit.html.erb
<h>Edit existing article</h>
<%=#article.errors %>
<h2>The following errors prevented the article from getting created</h 2>
<ul>
<%#article.errors.full_messages.each do |ms g| %>
<li><% ms g %></l i>
<% end %>
</ul>
<% end %>
<%= form_for #article do |f| %>
<p>
<%= f.label :title %><b r/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br/>
<%= f.text_area :description %>
</p>
<p>
<%=f.submit %>
<% end %>
You seem to have a cut and paste, or perhaps an editor issue - there are several lines with breaks between words.
And you have an extra <% end %> tag that doesn't seem to line up to anything
Try this instead
<h>Edit existing article</h>
<%=#article.errors %>
<h2>The following errors prevented the article from getting created</h2>
<ul>
<%#article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<%= form_for #article do |f| %>
<p>
<%= f.label :title %><br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br/>
<%= f.text_area :description %>
</p>
<p>
<%=f.submit %>
<% end %>
You have a bunch of spaces where you probably didn't intend them (including the text in your post above).
I think you meant for ms g to be msg on this section:
<%#article.errors.full_messages.each do |msg| %> # `ms g` => `msg`
<li><% msg %></li> # `ms g` => `msg`
Also remove the space in </l i> and <b r/>

Rails form validation not show the error messages, taking blank fields

My form::
<h1>New Post</h1>
<%= form_for (#post), url: posts_path do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited
this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :Title%><br>
<%= f.text_field :Title%>
</p>
<p>
<%= f.label :Text%><br>
<%= f.text_area :Text%>
</p>
<p>
<%= f.submit :Save %>
</p>
<% end %>
:: context ::
It is not showing the error messages. please point out the where i am wrong. I want to add basic validations. i have gone through some tutorials but still stuck into this.. please help me out.
In your Post model,Just add these line
validates_presence_of :title,text #doesn't allow the blank values
If you are also looking for validation of unique values for title,add this line also.
validates_uniqueness_of :title #doesn't allow duplicate values
Also Follow these Guides(Rails 4),if you are looking for more validations.
validates :title, uniqueness: {message: "must be uniq"}

Rails: one view, model and it's associated model

So, for example, case from http://guides.rubyonrails.org/getting_started.html As you can see, if you try to create invalid post, you will see error messages:
<%= form_for #post do |f| %>
<% if #post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
How to implement error messages rendering for associated Comment model, keeping in mind that comment creation form is placed in posts/show view?
Form code is usually kept in the folder of the matching model in a _form.html.erb partial that is rendered in both new.html.erb and edit.html.erb (to see a good example, generate a scaffold for a sample model).
What you can do in your case is render this comments form partial in the posts show action.
app/views/posts/show.html.erb
<%= render 'comments/form', comment: #comment || #post.comments.build # Whatever you have here %>
app/views/comments/_form.html.erb
<%= form_for comment do |f| %>
<%= render 'error_messages', target: comment %>
...
<% end %>
In addition, showing error messages usually is the same in all forms, so in order to remove duplication, you can extract this code into a seperate partial.
app/views/application/error_messages.html.slim # here is slim syntax, convert as nescessary
/ error explanation
/
/ = render 'shared/error_explanation', target: #school
/
- if target.errors.any?
.error-messages
h4 Please correct the following fields:
ul
- target.errors.full_messages.each do |message|
li = message
Hope this helps.

Rails Screencast - Completely stuck on "fields_for"

i am following this simple tutorial and i followed all the steps... but the browser simply doesn't show me anything i put inside the fields_for tag.
<%= form_for :activity, :url => activities_path do |f| %>
<% if #activity.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#activity.errors.count, "error") %> prohibited this activity from being saved:</h2>
<ul>
<% #activity.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Foto" %><br />
<%= f.text_field :photo %>
</div>
<div class="field">
<%= f.label "Foto per la home" %><br />
<%= f.text_field :photoHome %>
</div>
<% for info in #activity.infos %>
This is rendered<br>
<% fields_for "activity[info_attributes][]", info do |info_form| %>
This is not rendered<br>
<p>
Titolo: <%= info_form.text_field :title %>
</p>
<p>
Descrizione: <%= info_form.text_field :description %>
</p>
<% end %>
<% end %>
<p><%= submit_tag "Create activity" %></p>
<% end %>
The above code result is this:
What am i missing?
Rails is sometimes (well ok, often) a bit confusing in which helpers want a <% vs which helpers want <%=. Try
<%= fields_for "activity[info_attributes][]", info do |info_form| %>
The tutorial you're following doesn't, but that's from 2007, and in Rails 3 this was changed. Current documentation on fields_for is here.

Resources