when I attempt to run my code, I got the error above.
As you can see on the following image, the page posts/index leaves appear some information before the footer of the page :
I don't understand where it may come, because it's the only page where it appeared.
Here's my view :
<%= #post.each do |post| %>
<h1 class="Blog_post_title"><%= link_to post.title, post %></h1>
<p class="date"><%= post.created_at.strftime("%B, %d, %Y") %></p>
<p><%= image_tag post.landing_image.url , class: "landing_image_blog" %></p>
<p><%= post.body %></p>
<%= link_to post do %>
<div class="button12">Continuer la lecture</div>
<% end %>
<div class="border_grey_blog"></div>
<% end %>
</div>
<%= render "footer" %>
my posts_controller.rb:
class PostsController < ApplicationController
def index
#post = Post.all.order('created_at DESC')
end
def new
#post = Post.new
end
def show
#post = Post.find(params[:id])
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(params[:post].permit(:title,:title2,:title3,:title4,:title5,:title6,:title7,:title8,:title9, :body, :image,:image1,:image2,:image3,:image4,:image5,:image6,:image7,:image8, :landing_image, :some_text1, :some_text2, :some_text3, :some_text4, :some_text5, :some_text6, :some_text7, :some_text8, :some_text9, :some_text0))
redirect_to #post
else
render 'edit'
end
end
def create
#post = Post.new(post_params)
if #post.save
redirect_to #post
else
render 'new'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to "/posts"
end
private
def post_params
params.require(:post).permit(:title, :body, :image,:image1,:image2,:image3,:image4,:image5,:image6,:image7,:image8, :landing_image, :some_text1, :some_text2, :some_text3, :some_text4, :some_text5, :some_text6, :some_text7, :some_text8, :some_text9, :some_text0, :title2,:title3,:title4,:title5,:title6,:title7,:title8,:title9)
end
end
Any idea ?
Your mistake is in the very first line of the view:
<%= #post.each do |post| %>
should be
<% #post.each do |post| %>
since <%= opens an expression tag that will display its content.
It looks like your outputting the result of #posts.each. Notice how you did this in your view:
<%= #post.each do |post| %>
# other code
<% end %>
This will actually output all the data in the array as a ruby object. If you want to change this to not output everything, you need to change it to
<% #post.each do |post| %>
# other code
<% end %>
This will now not return the #post.each in the html.
Related
I'm not really sure how to quote this question, but I'm making a clone of Reddit in Ruby on Rails.
On the index page I am displaying all of the posts I have in the table "Posts". But when I display them all, at the end of the last post a text shows up which is wrapped in square brackets and contains all of the information from the table. I just want to know how to remove this text.
Picture of problem
This is the code used to display the post:
<%= #posts.each do |post| %>
<div class="card p-3">
<small class="mb-2"><strong><%= link_to "s\\" + post.sub.name, sub_path(post.sub) %></strong> Posted by <%= link_to "u\\" + post.user.username, profile_path(post.user.username) %> <%= time_ago_in_words post.created_at %> ago</small>
<h4><%= link_to post.title, sub_post_path(post.sub, post) %></h4>
<p><%= truncate post.body, length: 200 %></p>
</div>
<% end %>
My posts controller:
class PostsController < ApplicationController
before_action :authenticate_user!, except: [ :index, :show ]
before_action :set_post, only: [:show]
before_action :auth_subscriber, only: [:new]
def index
#posts = Post.all
end
def show
end
def new
#sub = Sub.find(params[:sub_id])
#post = Post.new
end
def create
#post = Post.new post_values
#post.user_id = current_user.id
#post.sub_id = params[:sub_id]
if #post.save
redirect_to subs_path(#post.sub_id)
else
#sub = Sub.find(params[:sub_id])
render :new
end
end
private
def set_post
#post = Post.find(params[:id])
end
def auth_subscriber
unless Subscription.where(sub_id: params[:sub_id], user_id: current_user.id).any?
redirect_to root_path, flash: { danger: "You are not authorized to view this page" }
end
end
def post_values
params.require(:post).permit(:title, :body)
end
end
<% #posts.each do |post| %>
<div class="card p-3">
<small class="mb-2"><strong><%= link_to "s\\" + post.sub.name, sub_path(post.sub) %></strong> Posted by <%= link_to "u\\" + post.user.username, profile_path(post.user.username) %> <%= time_ago_in_words post.created_at %> ago</small>
<h4><%= link_to post.title, sub_post_path(post.sub, post) %></h4>
<p><%= truncate post.body, length: 200 %></p>
</div>
<% end %>
In ERB <%= is used to output the result of an expression to the buffer. #each in Ruby returns self so what you're seeing on the page is the result of implicitly calling #to_s on an array.
#each is always used for its side effects and not its return value. In the case of an ERB template each iteration writes to the buffer.
This can be somewhat hard to grasp but remeber that any plaintext in a template is written directly to the buffer - using each just does it n number of times.
Change <%= #posts.each do |post| %> for <% #posts.each do |post| %>, note the missing '=' in the second snippet.
When you include the '=' the output of .each is printed in the HTML, in this case the array of posts.
I am new to Ruby and I am trying to display comments on my show page but when I create a comment it does not display. I have rendered a partial on my show page in order to display the comments but they do not appear.
The strange thing is that the create action works but it takes me to this page: http://localhost:3000/hairstyles/2/comments which has nothing on it (in my app this page is under views>comments>create.html.erb) instead i'd like it to go to the view page for the hairstyle and display the comment..........
If anyone can help and see if there are any errors in my code I'd be grateful. Thanks in advance.
Comments Controller:
class CommentsController < ApplicationController
def new
#hairstyle = Hairstyle.find(params[:hairstyle_id])
#comment = Comment.new
end
def create
#comment = Comment.new(comment_params)
#hairstyle = Hairstyle.find(params[:hairstyle_id])
#comment.save
end
def destroy
end
end
private
def comment_params
params.require(:comment).permit(:content)
end
Hairstyle view page where I want to display the comments:
<div class="container">
<div>
<%= cl_image_tag #hairstyle.photo, width: 300, height: 200, crop: :fill %>
<h1><%= #hairstyle.name %></h1>
<p><%= #hairstyle.description%></p>
<p><%= #hairstyle.category%></p>
<p><%= #hairstyle.video_url %></p>
</div>
</div>
<div>
<%= link_to 'Back', hairstyles_path %>
</div>
<h6>Comments for: <%= #hairstyle.name %> <small><%= %></small></h6>
<h2>
<%= pluralize #hairstyle.comments.size, "comment" %>
</h2>
<div id="comments">
<% if #hairstyle.comments.blank? %>
Be the first to leave a comment for <%= #hairstyle.name %>
<% else %>
<% #hairstyle.comments.each do |comment| %>
<%= render 'comments/show', comment: comment %>
<% end %>
<% end %>
</div>
<%= render 'comments/form', comment: #comment %>
Comments form i am rendering which does work and is displayed:
views>comments>_form.html.erb
<div class="flex-box">
<%= simple_form_for([ #hairstyle, comment ]) do |form| %>
<%= form.input :content, as: :text %>
<%= form.button :submit %>
<% end %>
</div>
Comments content that I am rendering that does not display once I have added a comment to my hairstyle on the show page:
views>comments>_show.html.erb
<p><%= comment.content %></p>
The default behavior of rails controllers is it will redirect to the index page after the create method. that's why you are redirected to that path.
you can simply use redirec_to in your create method in your comment controller like below
def create
#comment = Comment.new(comment_params)
#hairstyle = Hairstyle.find(params[:hairstyle_id])
#comment.save
redirect_to hairstyle_path(#comment.hairstyle)
end
You never link your comment to your hairstyle, your hairstyle_id from your comment is nil this is why #hairstyle.comments returns an empty array
def create
#hairstyle = Hairstyle.find(params[:hairstyle_id])
#comment = #hairstyle.comments.build(comment_params)
# equivalent to:
# comment = Comment.new(comment_params)
# comment.hairstyle = #hairstyle
if comment.save
redirect_to hairstyle_path(#hairstyle)
else
# handle errors
end
end
I'm making a blog type site, and I'd like the posts to be displayed newest first(descending order) Ive tried a few different things and haven't been able to figure it out. This is my current code:
post.html.erb
<%= #posts.each do |post|%>
<div>
<p><%= avatar_for(post.user, size: 40) %>
<%= link_to post.username, post.user %></p>
<h2>
<%= link_to post.title, post %>
</h2>
</div>
<% end%>
post controller
class PostsController < ApplicationController
before_action :authenticate_user!
def posts
#posts = Post.all.order("created_at DESC")
end
def new
#post = Post.new
end
def create
#post = current_user.posts.new(post_params)
if #post.save
redirect_to post_path(#post)
else
render :new
end
end
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :body, :all_tags)
end
end
I believe, based on the Guide, that the syntax you're looking for is:
Post.order(created_at: :desc)
See if that works for you.
This should be #posts as you are calling #posts in you post.html.erb to retrieve and display all the posts:
def posts
#posts = Post.all.order("created_at DESC")
end
The method should look like
def posts
# Plural #posts
#posts = Post.all.order(created_at: :desc)
end
Just a small fix, in your posts method you're declaring #post variable but in the ERB file you're calling #posts.
That should be giving you a Nil error. Declare #posts (plural), and then your ERB file with better indentation:
<%= #posts.each do |post| %>
<div>
<p>
<%= avatar_for(post.user, size: 40) %>
<%= link_to post.username, post.user %>
</p>
<h2>
<%= link_to post.title, post %>
</h2>
</div>
<% end%>
In case this question hasn't been answered yet:
It looks like the problem is coming from the name of the method in your PostsController.
You're expecting to have access to #posts from a method called posts, but your file name for the view you're trying to render is post.html.erb. I believe it should be posts.html.erb.
Why not call it index? That's the Rails convention.
As others have said, the query is correct. Hope this helps.
I'm a Rails newbie and this seems like a trivial thing, but I can't figure out how to render this.
I have a form that takes in a string input:
<%= form_for #idea do |f| %>
<div>
<%= f.label :idea_list %>
<%= f.text_field :idea_list %>
</div>
<%= f.submit %>
<% end %>
And a controller:
class IdeasController < ApplicationController
def index
end
def new
#idea = Idea.new
end
def create
#idea = Idea.new(idea_params)
if #idea.save
render :index
else
render 'new'
end
end
private
def idea_params
params.require(:idea).permit(:idea_list)
end
end
I then want to render the idea_list parameter in index.html.erb, but cannot figure out how to do so. I've tried:
<p><%= #idea %></p>
but I get an output of #<Idea:0x007f8b16d98638>.
<% #idea do |idea| %>
<p><%= idea.idea_list %></p>
<% end %>
I've tried many variations of this but keep getting various errors.
Please help.
So #idea is an instance of an object. idea_list is an attribute. So you need to call #idea.idea_list. Any time you want to reference an instance of an object / model from a controller it's going to #instance.attribute.
In your case, it's going to be
<p><%= #idea.idea_list %></p>
If you want to show them from a list of the all, in your view it's
<%#ideas.each do |idea| %>
<p><% idea.idea_list%></p>
<% end%>
And in your controller it's
def index
#ideas = Idea.all
end
I am trying to write a rails app and it keeps bombing on this one line of code in my controller.rb file:
posts GET /posts(.:format) posts#show
Can some one help me?
I am running ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-darwin12.4.0]
with rails 3.2.13
UPDATE
I took out the line of code above and now I can't get rails to post the value (tag) of the selected check box. Can I get some guidance?
Here is my posts_controller.rb file:
class PostsController < ApplicationController
def new
end
def create
#post = Post.new(params[:post].permit(:check_box, :label))
#post.save
redirect_to #post
end
def show
#post = Post.find(params[:id])
end
def index
#posts = Post.all
end
private
def post_params
params.require(:post).permit(:check_box, :label)
end
end
Here is my new.html.erb file:
<h1>SWORD Mock Device Page</h1>
<%= form_for :post, url: posts_path do |f| %>
<p>
<h2>Android Phones</h2>
<%= f.check_box(:razr_max1) %>
<%= f.label(:razr_max1, "Droid Razr Max #1") %>
</p>
<p>
<%= f.check_box(:galaxyS2) %>
<%= f.label(:galaxyS2, "Samsung Galaxy S2") %>
</p>
<p>
<h2>Android Tablets</h2>
<%= f.check_box(:asusprime3) %>
<%= f.label(:asusprime3, "Asus Transormer Prime #3") %>
</p>
<p>
<%= f.check_box(:motoxoom1) %>
<%= f.label(:motoxoom1, "Motorola Xoom #1") %>
</p>
<p>
<%=f.submit "Select" %>
</p>
<% end %>
here is my routes.rb:
SWORDMockDev::Application.routes.draw do
resources :posts
root to: "landing#index"
end
and my show.html.erb:
<p>
<strong>Device:</strong>
<%= #post.title %>
</p>
Any help is greatly appreciated!!
Thanks!!
ironmantis7x
Instead of:
def create
#post = Post.new(params[:post].permit(:check_box, :label))
#post.save
redirect_to #post
end
You can do:
def create
#post = Post.new(post_params)
#post.save
redirect_to #post
end
The label is not going to submit in the post, only the value of the checkbox
I recommend you to use pry https://github.com/pry/pry and in the controller in the create you can do:
def create
binding.pry
#post = Post.new(post_params)
#post.save
redirect_to #post
end
And you can see what comes in the params, and what's going on. Also checkout your routes and see if everything is ok with:
rake routes