在谷歌搜索和搜索stackoverflow后。仍无法找到解决我问题的方法。我的Post
模型验证错误消息未显示,并且未提供未定义的方法错误。
我有一个Post
模型,它有一些验证(presence, length
)和一些验证消息。当我点击创建Post
而不输入内容而是显示验证消息时,它会给我undefined method error
。但是当满足验证时,就会创建帖子。这意味着NoMethodError
实际上并不存在,如果我的Post
控制器中没有定义方法,那么如何创建帖子?
请参阅下面我的post.rb
,_form.html.erb
,_error_messages.html.erb
,NoMethodError Page
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :question
validates :user_id, presence: true
validates :question_id, presence: true
validates :content, :presence => { :message => "Content is required" }, length: {
minimum: 100,
maximum: 500,
tokenizer: lambda { |str| str.scan(/\w+/) },
too_short: "must have at least %{count} words",
too_long: "must have at most %{count} words",
}
default_scope -> { order('created_at DESC') }
end
<%= @question.title %>
<%= form_for(@post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div>
<%= f.hidden_field :question_id, :value => @question.id %><br>
</div>
<div>
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div>
<%= f.submit %>
</div>
<% end %>
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
NoMethodError in PostsController#create
undefined method `posts' for nil:NilClass
Extracted source (around line #27):
25
26 def create
27 @post = current_user.posts.build(post_params)
28
29
30 respond_to do |format|
Rails.root: /Users/jim/project/test
Application Trace | Framework Trace | Full Trace
app/controllers/posts_controller.rb:27:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"cakWqDu67ZyrJ6KTW8GlexoLrCFgyQnnRljT6wksGc4=",
"post"=>{"question_id"=>"1",
"content"=>""},
"commit"=>"Create Post"}
NoMethodError in Posts#create
Showing /Users/jim/project/test/app/views/posts/_form.html.erb where line #2 raised:
undefined method `title' for nil:NilClass
Extracted source (around line #2):
1
2 <%= @question.title %>
3 <%= form_for(@post) do |f| %>
4
5
Trace of template inclusion: app/views/posts/new.html.erb
Rails.root: /Users/jim/project/test
Application Trace | Framework Trace | Full Trace
app/views/posts/_form.html.erb:2:in `_app_views_posts__form_html_erb___2518287212872033894_2203911920'
app/views/posts/new.html.erb:3:in `_app_views_posts_new_html_erb___3716195397608425729_2203949420'
app/controllers/posts_controller.rb:35:in `block (2 levels) in create'
app/controllers/posts_controller.rb:30:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"fjUYhjW24sFHL/99lnYGjpsLQVa4En2UlNtPvY5imi0=",
"post"=>{"question_id"=>"1",
"content"=>""},
"commit"=>"Create Post"}
答案 0 :(得分:3)
在create
操作中,您需要获取@question
。根据您的代码,当出现任何错误时,应用程序会在create
操作中呈现“new.html.erb”页面。
def create
@post = current_user.posts.new(post_params)
if @post.save
#YOUR CODE GOES HERE
else
@question = Question.where(id: params[:post][:question_id]).first
render 'new'
end
答案 1 :(得分:0)
我认为此处的错误完全不同,它表示current_user
为零,您确定current_user
已初始化吗?
也许你应该有一个处理“no current_user”情况的before_filter。