I made a ruby on rails blog as part of my project. I wanted to add comments to my blog. However, the site takes the comment and stores it successfully but when it comes to showing the comments, it doesn't work. I checked if the comment was registered using the rails console and it is registered. The github repository for it is : https://github.com/rocka0/blog-in-rails
Note: If specific code is needed, please let me know in the comments
https://github.com/rocka0/blog-in-rails/blob/master/app/views/posts/_comments.html.erb#L1
I think it should be:
<%= div_for #comments do |comment| %>
I'm a bit surprised it does not throw any error here.
Edit: You also don't set #comments instance variable anywhere in the controller: https://github.com/rocka0/blog-in-rails/blob/master/app/controllers/posts_controller.rb#L13 You should do it or use #post.comments instead.
In the _comments.html.erb
<%= div_for #comments do |comment| %>
<p>
<strong>
Posted <%= time_ago_in_words(comment.created_at) %> ago
</strong>
<br/>
<%= comment.body %>
</p>
<% end %>
In the posts_controller.rb
........
def show
#comments = #post.comments
respond_with(#comments)
end
Check Your Comments Controller
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(params[:comment].permit(:name, :body))
redirect_to post_path(#post)
end
def destroy
#post = Post.find(params[:post_id])
#comment = #post.comments.find(params[:id])
#comment.destroy
redirect_to post_path(#post)
end
end
Related
In my Rails app, each post has a comment section where users can leave comments. I want each comment to have a delete link but I just can't get it to work. I am using the acts_as_commentable gem here.
posts/show.html.erb
<% #comments.each do |comment| %>
<p><strong><%= comment.user.username %></strong></p>
<p class="comment"><%= comment.comment %><p>
<%= link_to "Delete", [#post, comment], method: :delete %>
<% end %>
I need help with this line
<%= link_to("Delete", [#post, comment], method: :delete %>
comments_controller.rb
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(comment_params)
#comment.user_id = current_user.id
end
def destroy
#comment = Comment.find(params[:id])
redirect_to :back
end
Thanks for your help! :)
You are not destroying it at all in the destroy action, you need to add #comment.destroy to delete the comment from database
def destroy
#comment = Comment.find(params[:id]
#comment.destroy # add this line
redirect_to :back
end
Hope that helps!
change destroy action something like this:
def destroy
#post = Post.find(params[:post_id])
#comment = #post.comments.find(params[:id])
#comment.destroy
redirect_to :back
end
In this way you can make sure you are deleting comment related to specific post. It's rails proffered way you can look at it here .
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.
There are posts and comments form for each.
I'm trying to add a comment to every post through the form. It's all happening on the same page.
View file code:
<% #posts.each do |post| %>
<%=post.title%>
<%=post.text%>
<%post.comments.each do |com|%>
<h3> <%=com.content%> </h3>
<%end%>
<%= form_for post.comments.build do |f| %>
<p>comments:</p>
<%= f.text_area :content, size: "12x12" %>
<%=f.submit%>
<% end %>
<% end %>
Comments controller code:
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.build(comment_params)
#comment.save
redirect_to root_path
end
It seems that program can not access to :post_id.
I have all associations in my models and :post_id in my db schema.
Github link for this app
You need to add <%= f.hidden_field :post_id %> in your form and permit :post_id in comment_params.
Also, you may want to reduce create method code to one line.
def create
Comment.create(comment_params)
redirect_to root_path
end
You need to permit :post_id for your strong parameters in the comments controller:
def comment_params
params.require(:comment).permit(:content, :post_id)
end
I found a problem.
The mistake is in searching by params[:post_id], while i need to by [:comment][:post_id] after adding the hidden_field
To me this is a strange one.
I have 2 models (with User), I have post and I have comment.
What I am trying to do is to have the form_for comments in the post#show view.
However for some reason when I try to create a comment I get Couldn't find Post without an ID.
When I look at the request parameters though I see:
{"utf8"=>"✓", "authenticity_token"=>"7bAXF66sghTKAF7b61gu08hElC+O1nR6RoT92tqQGOI=", "comment"=>{"content"=>"ok"}, "commit"=>"Add comment", "action"=>"create", "controller"=>"comments", "post_id"=>"23"}
which clearly shows that it does in-fact get the post_id and that it is in this case the id of 23.
After countless hours I thought that I'd see if you guys have a solution.
My comments_controller.rb:
class CommentsController < ApplicationController
before_action :load_post
def create
#comment = #post.comments.build(params[:content])
#comment.user = current_user
if #comment.save
#comment.create_activity :create, owner: current_user
redirect_to root_url, notice: "Comment was created."
else
render :new
end
def load_post
#post = Post.find(params[:id])
end
end
My posts_controller.rb
def show
#post = Post.find(params[:id])
#comment = Comment.new
end
the partial for the forms comments/_form.rb
<%= form_for [#post, #comment] do |f| %>
<%= f.text_area :content %>
<%= f.submit "Add comment" %>
<% end %>
My routes.rb
resources :posts do
resources :comments
end
my posts/show.html.erb
<%= render #post %>
<h3>New comment</h3>
<%= render 'comments/form' %>
my posts/_post.html.erb
<h2><%= #post.title %></h2>
<p><%= #post.content %></p>
<em>written by <%= #post.user.fullname %></em>
You are receiving "post_id" not as "id"
You can make load_post action as private for security concerns
private
def load_post
#post = Post.find(params[:post_id])
end
Access the content like
def create
#comment = #post.comments.build(params[:comment][:content])
....
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