Rails partials: What am I doing wrong? - ruby-on-rails

So, I have this code in a partial called "_form.html.erb" in "app/views/posts":
<%= form_for #post do |f| %>
<% if #post.errors.any? %>
<h2>Errores:</h2>
<ul>
<% #post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %><br />
<br />
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
And I have two views with this code:
"app/views/posts/new.html.erb"
<h1>Nuevo post</h1>
<%= render 'form' %>
<p>
<%= f.submit "Agregar Nuevo Post" %>
</p>
<% end %>
"app/views/posts/edit.html.erb"
<h1>Editar Post</h1>
<%= render 'form' %>
<p>
<%= f.submit "Actualizar Post" %>
</p>
<% end %>
What I want is the form to render in the views, but with the code I have shown to you I'm getting this error output in the server:
SyntaxError in Posts#new
Showing /home/levick/rubyblog/app/views/posts/new.html.erb where line #10 raised:
/home/levick/rubyblog/app/views/posts/new.html.erb:10: syntax error, unexpected keyword_ensure, expecting $end
Extracted source (around line #10):
7: </p>
8: <% end %>
Trace of template inclusion: app/views/posts/new.html.erb
Rails.root: /home/levick/rubyblog
Application Trace | Framework Trace | Full Trace
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
The same with the edit view.
My Question is: What did I do wrong?
Thanks!

Try the following in your partial _form.html.erb
<%= form_for #post do |f| %>
<% if #post.errors.any? %>
<h2>Errores:</h2>
<ul>
<% #post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %><br />
<br />
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<%= f.submit #post.new_record? ? 'Nuevo Post' : 'Actualizar Post' %>
<% end %>

You cannot use the form variable outside of the partial. In both your new and edit pages you reuse the f variable even though it only exists inside the partial.
I would recommend you fix the error by moving the creation of the submit buttons inside the partial. Don't forget to also move the <%- end %> inside the partial too since you are finishing the form inside the partial.

You're missing an <% end %> to close your <%= form_for %> block.

Related

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

form_for details not displaying in view :Ruby on Rails

Hi I am using simple form_for to create new object but on the ads/new.html.erb on h1 tag is displayed but form's information is missing
Here is my new.html.erb file contents
<h1>New Ad</h1>
<% form_for(#ad ,:url=>{:action=>'create',method: :post}) do |f| %>
<p>
<b>Name:</b> <%= f.text_field:name %>
</p>
<p>
<b>Description:</b><%= f.text_area:description %>
</p>
<p>
<b>Price:</b><%= f.text_field:price %>
</p>
<p>
<b>Seller Id:</b><%= f.text_field:seller_id %>
</p>
<p>
<b>Email Address:</b><%= f.text_field:email %>
</p>
<p><%= f.submit "Create Ad" %>
</p>
<% end %>
This line
<% form_for(#ad ,:url=>{:action=>'create',method: :post}) do |f| %>
should be like this
<%= form_for(#ad ,:url=>{:action=>'create',method: :post}) do |f| %>
You are missing = sign
Reference,see the API
Additional note
I guess you can refactor it to
<%= form_for(#ad) do |f| %>

NoMethodError in Discussions#new

Can anyone help me to fix this?Thanks in advance!
I get the following error:
NoMethodError in Discussions#new
Showing C:/Users/punitha/aggregator/app/views/discussions/_form.html.erb where line #1 raised:
undefined method `discussions_index_path' for #<#<Class:0x4375758>:0x437d6d8>
Extracted source (around line #1):
1 <%= form_for #discussions do |f| %>
2 <% if #discussions.errors.any? %>
3 <div id="error_explanation">
4 <h2><%= pluralize(#discussions.errors.count, "error") %> prohibited
Trace of template inclusion: app/views/discussions/new.html.erb
Rails.root: C:/Users/punitha/aggregator
Application Trace | Framework Trace | Full Trace
app/views/discussions/_form.html.erb:1:in `_app_views_discussions__form_html_erb___1058370717_35436840'
app/views/discussions/new.html.erb:3:in `_app_views_discussions_new_html_erb__298093787_35389404'
And the file _form.html.erb is
<%= form_for #discussions do |f| %>
<% if #discussions.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#discussions.errors.count, "error") %> prohibited
this discussion from being saved:</h2>
<ul>
<% #discussions.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :source_url %><br>
<%= f.text_field :source_url %>
</p>
<p>
<%= f.label :discussion_id %><br>
<%= f.text_area :discussion_id %>
</p>
<p>
<%= f.label :discussion_msg %><br>
<%= f.text_area :discussion_msg %>
</p>
<p>
<%= f.label :discussion_type %><br>
<%= f.text_field :discussion_type %>
</p>
<p>
<%= f.label :discussion_link %><br>
<%= f.text_area :discussion_link %>
</p>
<p>
<%= f.label :discussion_thumb %><br>
<%= f.text_area :discussion_thumb %>
</p>
<p>
<%= f.label :discussion_permalink %><br>
<%= f.text_area :discussion_permalink %>
</p>
<p>
<%= f.label :discussion_likecount %><br>
<%= f.number_field :discussion_likecount%>
</p>
<p>
<%= f.label :comment %><br>
<%= f.text_area :comment%>
</p>
<p>
<%= f.label :source_id %><br>
<%= f.number_field :source_id%>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
And my app/views/discussions/new.html.erb is,
<h1>New discussion</h1>
<%= render 'form' %>
<%= link_to 'Back', discussions_path %>
routes.rb file
Aggregator::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
resources:discussions do
resources :comments
end
end
You're form_for is set to an array (#discussions) when it should be to a single instance (#discussion).
In your DiscussionsController's new method, ensure you have something along the following:
#discussion = Discussion.new
Then, in your form, change your current form_for expression to:
form_for #discussion
See if that clears your error.
First of all you should correct your typo mistake in routes.rb.
It should looks like (I have added a space between resources and :discussions):
Aggregator::Application.routes.draw do
get "welcome/index"
root 'welcome#index'
resources :discussions do
resources :comments
end
end
As Craig Kaminsky said, it seems you are having #discussions as an array of discussion records.'
In your controller's action new, you should initialize your object as:
#discussion = Discussion.new
And you form should use same instance variable (#discussion)
<%= form_for #discussion do |f| %>
<% if #discussion.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#discussion.errors.count, "error") %> prohibited
...
<% end %>
...
...
<% 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