2
간단한 Comment 모델과 컨트롤러가 있습니다. 내 응용 프로그램에서 주석을 만들면 할당 된 유효성 검사를하지 않습니다. 콘솔에서 코멘트를 만들 때유효성 확인 문제
class Comment < ActiveRecord::Base
belongs_to :post
validates_presence_of :commenter
validates_presence_of :body
end
가 여기 내 출력입니다 :
>> comment = Comment.new
=> #<Comment id: nil, commenter: nil, body: nil, post_id: nil, email: nil, created_at: nil, updated_at: nil>
>> comment.save
=> false
>> comment.errors
=> #<OrderedHash {:body=>["can't be blank"], :commenter=>["can't be blank"]}>
모든 것이 멋지다
이 내 댓글 모델입니다. 그러나 응용 프로그램 내에 빈 메모를 만들면 성공적으로 만들어졌으며 실제로는 주석을 만들지 않는다고 말합니다.
이 그것을 기록 무슨이에
Started POST "/posts/19/comments" for 127.0.0.1 at Tue Aug 16 23:10:26 -0400 2011
Processing by CommentsController#create as HTML
Parameters: {"comment"=>{"body"=>"", "commenter"=>"", "email"=>""}, "commit"=>"Create Comment", "authenticity_token"=>"V/EinZAi2NNYx7AokikTpQFkNtADNiauW5vcNGdhTug=", "utf8"=>"\342\234\223", "post_id"=>"19"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = 19 LIMIT 1
Redirected to http://localhost:3000/posts
Completed 302 Found in 23ms
어떤 생각? 아무리 도움이된다면 실제 양식 코드도 추가 할 수 있습니다.
UPDATE 컨트롤러 코드 :
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
flash[:notice] = "Your comment has been saved."
redirect_to (:back)
end
end
UPDATE 보기 코드 : 수동으로 오류가 있는지 확인하고이를 표시해야
<%= form_for([post, post.comments.build]) do |f| %>
<div class="field">
<h4><%= f.label :name %></h4>
<%= f.text_field :commenter %>
</div>
<div class="field">
<h4><%= f.label :email_address %></h4>
<%= f.text_field :email %>
</div>
<div class="field">
<h4><%= f.label :body %></h4>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
<%= link_to 'Cancel', nil, :class => 'cancel' %>
</div>
<% end %>
는 컨트롤러의 코드를 추가합니다. –
컨트롤러를 제공하고 code..comment가 실제로 생성되지는 않았지만 앱이 생성되었다고 말합니다.이 문제가 무엇입니까? .. – rubyprince
그게 올바른 rubyprince입니다. –