【问题标题】:ruby on rails controller doesn't execute model methodruby on rails 控制器不执行模型方法
【发布时间】:2023-03-07 23:58:01
【问题描述】:

controller.rb

if @object.save
    Post.method_name(@object)
end

post.rb

def self.method_name(object)
    post_id = 1
    p = Post.new
    p.post_id = post_id
    p.save
end

Post.method_name(@object) 执行时,我看不到新帖子添加到表中,即使我也看不到任何错误...

【问题讨论】:

  • 尝试使用p.save! 以在保存失败时出现异常;否则测试p.save的结果
  • 发帖有验证吗?
  • @Baldrick 尝试过,但我收到以下错误 Validation failed: Post type is not included in the list @Rubyman 但如果我正确填写所有字段,为什么它们会影响记录保存?

标签: ruby-on-rails methods model controller execute


【解决方案1】:

解决方案:

我收到以下错误

Validation failed: Post type is not included in the list

当我使用p.save!

由于validates :post_type, :presence => true, :inclusion=> { :in => @allowed_post_types }

所以当我将使用的帖子类型添加到 @allowed_post_types 的数组中时

问题解决了

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    相关资源
    最近更新 更多