다음 오류가 발생합니다. 내가 원하는 무엇Rails4 : 폼의 첫 번째 인수는 사이드 바의 form_for에 nil을 포함하거나 비워 둘 수 없습니다.
ArgumentError in Articles#index
Showing e:/xxx/app/views/shared/_comment_form.html.erb where line #1 raised:
First argument in form cannot contain nil or be empty
는 주석을 입력 할 수 있도록 사용자의 모든 시간에 사이드 바에 text_area를 표시하는 것입니다.
comments
과 어떤 관련도 없습니다.
@comment
을 articles_controller
과 comments_controller
으로 설정해야합니까? 제가 몇 가지 시도했지만, 그들은 작동하지 않습니다.
\ 응용 프로그램 \ 전망 \ 레이아웃 \ application.html.erb
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<div class="row">
<aside class="span3">
<section>
<%= render 'shared/comment_form' %>
</section>
</aside>
<aside class="span9">
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</aside>
</div>
</div>
</body>
</html>
\ 응용 프로그램 \ 전망 \ shared_comment_form.html.erb
<%= form_for(@comment) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Enter your comment..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
\ 응용 프로그램 \ 컨트롤러 \ comments_controller.rb
class CommentsController < ApplicationController
def new
@comment = Comment.new
end
def create
@comment = Comment.new(comment_params)
if @comment.save
flash[:success] = "Sent your comment."
redirect_to root_url
else
redirect_to root_url
end
private
def comment_params
params.require(:comment).permit(:content)
end
end
\ app \ models \ comment.rb
class Comment < ActiveRecord::Base
default_scope -> { order('created_at DESC') }
validates :content, length: { maximum: 100 }
end
\ 응용 프로그램 \ 컨트롤러 \ articles_controller.rb
class ArticlesController < ApplicationController
..
def index
@articles = Article.all(limit: 10)
end
..
이 오류를 방지하는 방법을 나에게 알려 주시기 바랍니다.
답변 해 주셔서 감사합니다. @spickermann. 그것은 작동합니다! – SamuraiBlue
왜 작동합니까? 컨트롤러에서 객체를 생성하고 뷰에 변수를 전달하는 것과 같은 것은 아닙니다. – dehq
@dehq : 물론 OP는 다른 경로와 다른 컨트롤러에서이 양식을 사용하려고합니다. 모든 컨트롤러에서'@ comment'를 초기화하는 것은별로 의미가 없습니다. – spickermann