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| %>
Related
So here it is:
For some reason this is not showing the title for my post.
However when I press show option and it goes to post's page you can see al the detail.
_form.html.erb
<%= form_with(model: post, local: true) do |form| %>
<% 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 |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date, id: :post_date %>
</div>
<div class="field">
<%= form.label :name %>
<%= form.text_area :name, id: :post_name %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id, id: :post_user_id, value: current_user.id %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description, id: :post_description %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
#show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User:</strong>
<%= #post.user_id %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<h1>Editing Post</h1>
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
edit.html.erb
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
I've tried changing it <%= form.text_area :name, id: :post_name %>
to string_area :name and also string_field but none of it is working.
In fact when I change it to the last 2, I get a method error if I go on the edit page. I'm still fairly new to rails and to ruby so I would greatly appreciate some help. I did try to google however and was unsuccessful, I guess I didn't know what to search for or maybe I worded it wrong.
How can I place an if condition in the HTML, in pseudocode, I'd like to achieve the following:
if #time.status is pending
place edit and delete below the message
else
not just display
end
This is my current code:
<% #value.each do |s| %>
<p><strong>Message:</strong>
<%= s.message %>
</p>
<p><strong>Date</strong>
<%= s.date %>
</p>
<p><strong>Status:</strong>
<%= s.status %>
</p>
<p>
<%= link_to 'Edit', value(), %>
<%= link_to 'Delete', value(), %>
</p>
If you have access to #time and status is an attribute of it, with a value that can be "pending" (thing you didn't add in the question), then you can do:
<% if #time.status == 'pending' %>
<!-- place edit and delete -->
<% else %>
<% #value.each do |s| %>
<p>
<strong>Message:</strong>
<%= s.message %>
</p>
<p>
<strong>Date</strong>
<%= s.date %>
</p>
<p>
<strong>Status:</strong>
<%= s.status %>
</p>
<p>
<%= link_to 'Edit', value(), %>
<%= link_to 'Delete', value(), %>
</p>
<% end %>
<% end %>
It is same as your iteration
<% if #time.status=="PENDING" %>
#your html code for edit in delete
<%end%>
I'm beginner in Ruby On Rails and I'm reading "Getting Started with Rails" now.
I created models and controllers for Article and Comments, everything ok, I'm able to add comments and they appear on Article view, but besides I see records from database in this format:
[# Comment id: 1, commenter ... updated_at: "2016-05-20 09:26:25"]
Why they appear and how to hide it? Code of Article's view show.html.erb:
<p>
<strong>Title:</strong>
<%= #article.title %>
</p>
<p>
<strong>Text:</strong>
<%= #article.text %>
</p>
<h2>Comments</h2>
<%= #article.comments.each do |comment| %>
<p>
<strong>Commenter:</strong>>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>>
<%= comment.body %>
</p>
<% end %>
<h3>Add a comment</h3>
<%= form_for([#article, #article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit', edit_article_path(#article) %>
<%= link_to 'Back', articles_path %>
Instead of:
<%= #article.comments.each do |comment| %>
It should be:
<% #article.comments.each do |comment| %>
<%= %> used to render something. And you are iterating an object so you need to use <% %>.
Change this:
<% #article.comments.each do |comment| %>
What <%= does is displaying whatever data he's dealing with. If you do <% #article.comments.each do |comment| %> it will deal with the data as well but he won't display anything. And then you can display whatever you want with:
<%= comment.commenter %>
<%= comment.body %>
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 %>
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.