Rails: syntax error, unexpected end, expecting ')' '.freeze; end ^~~ - ruby-on-rails

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>

Related

Rails Turbo Frame Tag wrong number of arguments error

I've upgraded to rails 6.1 from rails 6.0 and trying out Hotwire, but I am getting an error when trying to run my application:
wrong number of arguments (given 0, expected 1..4)
Extracted source (around line #1):
<%= turbo_frame_tag 'branch' do %>
my code in my edit.html.erb:
<%= turbo_frame_tag 'post' do %>
<h3>Edit post</h3>
<%= render 'form', post: #post %>
<% end %>
my code in my _form.html.erb:
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<h5><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h5>
<ul class="browser-default">
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<div class="form-field">
<div class="input-field">
<%= form.text_field :title %>
<%= form.label :title %>
</div>
</div>
<br>
<div class="form-field">
<div class="input-field">
<%= form.text_field :description %>
<%= form.label :description %>
</div>
</div>
<div class="actions"><%= submit_tag "Submit", :type=>"submit", :class=>"btn", :data => { :disable_with => "Processing" } %></div>
<% end %>
my code in my show.html.erb:
<%= turbo_frame_tag 'branch' do %>
<h3><%= #post.title %></h3>
<p><%= #post.description %></p>
<% end %>
Does anybody have an idea what I might have missed during the upgrade process? I ran rails turbo:install and also rails hotwire:install or am I missing something in the turbo frame tag?

Ruby on Rails: Encountered a syntax error while rendering template:

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

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/>

Passing an ID to controller show method in Rails

I have the following view page in my test_app:
<p id="notice"><%= notice %></p>
<p>
<strong>Box count:</strong>
<%= #job.box_count %>
</p>
<p>
<strong>Install date:</strong>
<%= #job.install_date %>
</p>
<%= link_to "Back", :back %>
I have the following ruby code in my jobs_controller show method:
def show
#job = Job.find(params[:job_id])
end
The parameters being passed through to the view are ("customer_id" and "id" for the job) as follows:
{"customer_id"=>"1", "id"=>"23"}
When I load the view for any job, I get the following error:
Couldn't find Job without an ID
I must have an error in my show method, but I am unsure exactly what I am doing wrong.
Any ideas?
It looks as though the issue is that my form is not saving the data to the table, here is my form:
<%= form_for([#customer, #job]) do |f| %>
<% if #job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% #job.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :box_count %><br>
<%= f.number_field :box_count %>
</div>
<div class="field">
<%= f.label :install_date %><br>
<%= f.text_field :install_date %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Any idea why the data is not being saved?
You should be using :id, not :job_id:
def show
#job = Job.find(params[:id])
end

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