vendredi 25 décembre 2015

Rail Application having blogs with comments

i am learning Ruby on Rails and i am using Rails 4. Following a Rails tutorial on Making a Blog App with Comments, the author wrote this to create a comment in comments_controller.rb

def create
@post=Post.find(params[:post_id])
@comment=@post.comments.build(params[:post].permit[:body])
redirect_to @post_path(@post)
end

and in the partial : _form.html.erb

<%= form_for([@post, @post.comments.build]) do |f| %>
<h1><%= f.label :body %></h1><br />
<%= f.text_area :body %><br />
<%= f.submit %>
<% end %>

I was wondering if i could only let the current user to comment on a post, having made all appropriate associations between User Model and Comment Model, so that while displaying the comments, i could retreive information from the User through Comment. Clearly, i do not just want to use a

before_action :authenticate_user!

as i want an association between User and Comment.

Aucun commentaire:

Enregistrer un commentaire