gpt4 book ai didi

ruby-on-rails - Rails 验证错误消息未显示并给出未定义的方法错误

转载 作者:行者123 更新时间:2023-12-04 06:10:54 24 4
gpt4 key购买 nike

在谷歌搜索和 stackoverflow 上搜索之后。仍然找不到解决我的问题的方法。我的 Post 模型验证错误消息未显示,并给出未定义的方法错误。

我有一个 Post 模型,它有一些验证( presence, length )和一些验证消息。当我单击创建 发布 而不输入内容,而是显示验证消息时,它给我一个未定义的方法错误。但是当满足验证时,就会创建帖子。这意味着 NoMethodError 实际上不存在,如果我的 Post Controller 中没有定义该方法,如何创建帖子?

请看下面我的post.rb , _form.html.erb, _error_messages.html.erb,NoMethodError Page

post.rb

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

_form.html.erb

<%= @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 %>

_error_messages.html.erb

<% 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 页面

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 2

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"}

最佳答案

在您的创建 操作中,您需要获取@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

关于ruby-on-rails - Rails 验证错误消息未显示并给出未定义的方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22143903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com