I am just learning rails and hope someone can guide me on a theory question.

The course assignment I am working on requires me to initialize a new comment object for a partial that includes a form for submitting a new comment. The form is not to be rendered if the user is not logged in to the application.

From what I know so far, you are encouraged to keep as much code as possible in the controller, so I figured that is the best place for the initialization of @comment = @post.comments.new.

Snippet from posts\_controller.rb:

```
  def show
    @topic = Topic.find(params[:topic_id])
    @post = Post.find(params[:id])
    @comment = @post.comments.new
    authorize @comment
    @comments = @post.comments.all    
  end

```

And here is the partial comments/\_form.html.erb:

```
<h3>Add a comment:</h3>
<%= form_for [topic, post, comment], html: {class: "form-inline"} do |f| %>
  <%= f.label "Body" %>
  <%= text_field(:comment, :body) %>
  <%= f.submit "Create Comment" %>
<% end %>

```

And here is the form that calls the partial post/show.html.erb:

```
<h1><%= markdown_to_html @post.title %></h1>
<div>
  <%= image_tag(@post.image.small.url) if @post.image? %>
</div>
<div class="row">
  <div class="col-md-8">
    <small>
      <%= image_tag(@post.user.avatar.tiny.url) if @post.user.avatar? %>
      submitted <%= time_ago_in_words(@post.created_at) %> ago by
      <%= @post.user.name %>
    </small>
    <p><%= markdown_to_html @post.body %></p>
  </div>
  <div class="col-md-4">
    <% if policy(@post).edit? %>
      <%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %>
    <% end %>   
  </div>
</div>
<div class="row">
  <div class="col-md-1">
    <h1>Comments</h1>
  </div>
</div>
<%= render partial: "comments/comments", collection: @comments, as: :comment %>
<% if policy(Comment.new).create? %>
  <div class="row">
    <div class="col-md-12">
      <%= render partial: "comments/form", locals: { topic: @topic, post: @post, comment: @comment } %>
    </div>
  </div>
<% end %>

```

The problem is that if the user is not logged in to the application, the authorization for @comment fails in the controller, and prevents posts/show.html.erb from rendering.

Maybe I could do something like this in posts/show.html.erb ?:

```
   <% if policy(Comment.new).create? %>
    <% @comment = Comment.new %>
      <div class="row">
        <div class="col-md-12">
          <%= render partial: "comments/form", locals: { topic: @topic, post: @post, comment: @comment } %>
        </div>
      </div>
    <% end %>

```

Is this legit, or is it poor technique?

Any advice to point me in the right moral direction is appreciated!