Так что я пытаюсь получить идентификатор статьи, чтобы перейти в комментарии.article_id, я посмотрел по всему - и ничего не могу найти (я довольно новый, поэтому я сомневаюсь, что знаю что я ищу) Я пытался за последние пару часов передать идентификатор статей в контроллер комментариев, а затем установить comment.article_id в идентификатор статьи. Вот мой код на данный момент:Ruby on rails get article_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 %>
Просто, чтобы добавить я где-то есть article_id: https: //i.gyazo.com/fe49dcebbe75c006e95c7f44a9964865.png – StormViper