user5198598
user5198598

Reputation:

Ruby on rails getting article_id inside comments controller

So I am trying to get the article id to pass into the comments.article_id, I have looked all over - and can't find anything(I am fairly new so I doubt I know what I'm looking for) I have been trying for the last couple of hours to pass the articles id to the comments controller then set the comments.article_id to the article id. Here's my code at the moment:

ArticlesController:

class ArticlesController < ApplicationController
require 'comment'
def new
end

def create
  @article = Article.new(article_params)
  @article.user_id = current_user.id
if @article.save 
  flash[:success] = "Post created successfully!"
  redirect_to article_path(@article)
else
    flash[:danger] = "We could not create you're article!"
    render 'new'
end
end

def show
  @article = Article.find(params[:id])
  @comment = Comment.new
end

private

   def article_params
      params.require(:article).permit(:title, :description)
   end

end

My comments controller:

class CommentsController < ApplicationController

def new

end

def create
    @comment = Comment.new(comment_params)
    @comment.article_id = Article.find(params[:id])
    @comment.user_id = current_user.id
    if @comment.save
        flash[:success] = "Successfully created comment"
        redirect_to article_path(@comment.article)
    else
        debugger
        flash[:danger] = "Failed to create comment"
        redirect_to article_path(2)
    end
end

private

def comment_params
    params.require(:comment).permit(:description)
end
end

and my show.html.erb for articles(because I am doing it all in there)

<h1 align="center">Title: <%= @article.title %></h1>

<div class="well col-xs-8 col-xs-offset-2">
<div class="row">
    <div class="col-xs-4 col-xs-offset-4">
        <div class="well well-sm">
            <div class="user-img">
                <p class="created">Created By:</p>
                <%= gravatar_for @article.user, size: 150 %>
            </div>
            <div class="user-title">
                <%= link_to @article.user.username,  user_path(@article.user) %> <br />
            </div>
        </div>
    </div>
</div>
<h4 class="center description"><strong>Description:</strong></h4>
<hr />
    <%= simple_format(@article.description) %>
<div class="article-actions">
    <% if logged_in? && current_user == @article.user %>
    <%= link_to 'Edit', edit_article_path(@article), class: "btn btn-xs btn-primary" %>
    <%= link_to 'Delete this article', article_path(@article), method: :delete,
                                                                         data: { confirm: "Are you sure you want to delete this article?" },
                                                                         class: "btn btn-xs btn-danger" %>
    <%= link_to 'View all articles', articles_path, class: "btn btn-xs btn-success" %>
    <% else %>
    <%= link_to 'View all articles', articles_path, class: "btn btn-xs btn-success" %>
    <% end %>
</div>

<% @article.comment.each do |f| %>
<div id="commentBorder">
<div class="panel panel-default">
    <div class="panel-body">
        <p><%= f.description %></p>
    </div>
    <div class="panel-footer">
        <div align="center">
        <small>Created by <%= f.user.username %>,<br />
                    <%= time_ago_in_words(f.created_at) %>       ago</small></div>
    </div>
</div>
</div>
<% end %>

<%= form_for(@comment, :html => {class: "form", role: "forms"}, :url => article_comments_path(@article)) do |comm| %>

<%= comm.label :description %>
<%= comm.text_field :description %>

<%= comm.submit "Post comment" %>

<% end %>

Upvotes: 0

Views: 506

Answers (2)

charlysisto
charlysisto

Reputation: 3700

Just to be clear about rails associations, you could also have done :

@comment.article = Article.find(params[:article_id])

This has the advantage of ensuring the article exists before setting it's id value to the comment.article_id

Upvotes: 3

user5198598
user5198598

Reputation:

Solution!

So playing around some more literally five minutes after this, inside my Comments controller, I changed @comment.article_id = Article.find(params[:id]) Into

@comment.article_id = params[:article_id]

This fixed it, I have a theory on how so correct me if I am wrong. From this picture you can see we already have article_id as a parameter(https://i.gyazo.com/fe49dcebbe75c006e95c7f44a9964865.png) By using params[:article_id] we reached and got the article_id parameter and assigned it to our @comment.article_id value.

Upvotes: 3

Related Questions