gpt4 book ai didi

ruby-on-rails - 为什么我在 Rails 模型回调中收到此 "undefined method"错误,将方法作为符号传递?

转载 作者:数据小太阳 更新时间:2023-10-29 07:30:11 26 4
gpt4 key购买 nike

我有一个名为“Post”的类,它应该在更改或尚未转换时将其 Markdown 内容转换为 HTML。我正在尝试将 before_save 回调与 if: 一起使用参数,但我在传递给 if 的任何内容时都会收到此错误当我尝试运行测试时:

Testing started at 1:55 ... rake aborted!
undefined method 'markdown_changed_or_html_nil?' for #<Post:0x000000053603d0> C:/Users/user/Documents/GitHub/jw/app/models/post.rb:7:in <class:Post> C:/Users/user/Documents/GitHub/jw/app/models/post.rb:1:in <top (required)> C:/Users/user/Documents/GitHub/jw/test/test_helper.rb:12:in <class:TestCase> C:/Users/user/Documents/GitHub/jw/test/test_helper.rb:5:in <top (required)> C:/Users/user/Documents/GitHub/jw/test/models/post_test.rb:1:in <top (required)>
-e:1:in 'load'
-e:1:in '' Tasks: TOP => test:run => test:units (See full trace by running task with --trace) Run options: --seed 13458

# Running tests:

Finished tests in 0.002000s, 0.0000 tests/s, 0.0000 assertions/s.

0 tests, 0 assertions, 0 failures, 0 errors, 0 skips

Process finished with exit code 1

这是有问题的模型:

class Post < ActiveRecord::Base
include ActiveModel::Dirty

before_save :convert_markdown, if: :markdown_changed_or_html_nil?

belongs_to :user
validates :user, :title, :content_markdown, { presence: true, on: create }
validates_associated :user

protected
def convert_markdown
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, space_after_headers: true, underline: true)
self.content_html = markdown.render(content_markdown)
end

def markdown_changed_or_html_nil?
content_markdown.changed? || content_markdown.nil?
end
end

我正在使用 Ruby 2.0.0 和 Rails 4.0.2。

我很可能犯了一个非常非常基本的错误 - 我仍在学习 Rails。


编辑:这是post_test.rb

require 'test_helper'

class PostTest < ActiveSupport::TestCase
test 'saving an empty object fails' do
new_post = Post.new

assert_not new_post.save
end

test 'validates that given user id corresponds to user' do
# This will create a user with a given id so we can use the next one up
test_user = User.create({ name: 'Johnny Test', email: 'johnny.test@example.com',
password: 'password', password_confirmation: 'password' })

# Use next id up - there's no reason it should be taken at this point
given_user_id = test_user.id + 1

post_with_invalid_user = Post.new({ title: 'Look at my glorious title', content_markdown: 'Sick content',
user_id: given_user_id })

assert_not post_with_invalid_user.save
end

test 'converts markdown into html' do
# This is a really really basic test just to make sure a conversion happens
new_post = Post.new({ title: 'Check out this markdown, baby', content_markdown: 'I got some *sick* markdown',
user_id: users(:paul).id })

assert_equal '<p>I got some <em>sick</em> markdown</p>', new_post.content_html
end
end

最佳答案

这可能不是您的主要问题(因为您的错误消息似乎与此无关),但您没有正确使用 changed?changed? 需要在您的模型对象上调用,可以选择使用您的属性名称作为前缀。所以你的条件方法应该是这样的:

def markdown_changed_or_html_nil?
# based on your method name, shouldn't this be:
# content_markdown_changed? || content_html.nil?
content_markdown_changed? || content_markdown.nil?
end

http://api.rubyonrails.org/classes/ActiveModel/Dirty.html 找到更多关于 Dirty 方法的信息.

还有

我很确定 Rails 4 没有将 Dirty 移出 ActiveRecord::Base,因此您不需要手动 include ActiveModel: :Dirty 在你的模型中。

还有

这一行:

validates :user, :title, :content_markdown, { presence: true, on: create }

应该是:

validates :user, :title, :content_markdown, { presence: true, on: :create }

关于ruby-on-rails - 为什么我在 Rails 模型回调中收到此 "undefined method"错误,将方法作为符号传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24016214/

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