所以我试图将文章ID传递到comments.article_id,我已经看了一遍 - 并且找不到任何东西(我相当新,所以我怀疑我知道我在找什么)我在过去的几个小时里,我一直试图将文章ID传递给评论控制器,然后将comments.article_id设置为文章ID。这是我目前的代码:
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
我的评论控制器:
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
和我的show.html.erb文章(因为我在那里完成所有这些)
<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 %>
答案 0 :(得分:4)
为了清楚rails关联,你也可以做到:
@comment.article = Article.find(params[:article_id])
这样做的好处是在将{id}值设置为article
comment.article_id
存在
答案 1 :(得分:2)
<强>解决方案<!/强>
所以在这之后的五分钟左右,在我的评论控制器中,我更改@comment.article_id = Article.find(params[:id])
成
@comment.article_id = params[:article_id]
这解决了这个问题,如果我错了,我有一个如何纠正我的理论。 从这张图片中你可以看到我们已经将article_id作为参数(https://i.gyazo.com/fe49dcebbe75c006e95c7f44a9964865.png) 通过使用params [:article_id],我们到达并获得了article_id参数并将其分配给我们的@ comment.article_id值。