Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:虚拟属性和表单值_Ruby On Rails_Activerecord_Attributes_Virtual - Fatal编程技术网

Ruby on rails Rails:虚拟属性和表单值

Ruby on rails Rails:虚拟属性和表单值,ruby-on-rails,activerecord,attributes,virtual,Ruby On Rails,Activerecord,Attributes,Virtual,我有一个带有虚拟属性的模型书,用于从书表单创建编辑器。 代码如下所示: class Book < ActiveRecord::Base has_many :book_under_tags has_many :tags, :through => :book_under_tags has_one :editorial has_many :written_by has_many :authors, :through => :written_by def e

我有一个带有虚拟属性的模型书,用于从书表单创建编辑器。 代码如下所示:

class Book < ActiveRecord::Base
  has_many :book_under_tags
  has_many :tags, :through => :book_under_tags
  has_one  :editorial
  has_many :written_by
  has_many :authors, :through => :written_by

  def editorial_string
   self.editorial.name unless editorial.nil?
   ""
  end
  def editorial_string=(input)
    self.editorial = Editorial.find_or_create_by_name(input)
  end
end

第一个问题是

def editorial_string
  self.editorial.name unless editorial.nil?
  ""
end
将始终返回“”,因为这是最后一行

def editorial_string
  return self.editorial.name if editorial
  ""
end

会解决这个问题。至于为什么验证没有通过,我不知道,你在控制器里做什么?您得到了哪些验证错误?

我相信这是因为您的书《编辑字符串方法》将始终返回“”。可以简化为以下内容:

  def editorial_string
   editorial ? editorial.name : ""
  end
根据评论更新:

听起来你想做嵌套表单。(请参阅)注意,这是Rails 2.3中新增的

所以如果你更新你的图书类

class Book < ActiveRecord::Base
  accepts_nested_attributes_for  :editorial
  ...
end
classbook
(现在还可以删除editive_string=,editive_string方法)

并将您的表单更新为如下内容

...
<% f.fields_for :editorial do |editorial_form| %>
  <%= editorial_form.label :name, 'Editorial:' %>
  <%= editorial_form.text_field :name %>
<% end %>
...
。。。
...

看看这个播客。我认为您应该将find\u或create从编辑字符串=(输入)中移动
方法在保存后回调。

感谢您的修复。但我有同样的问题:我丢失了在编辑表单中插入的值。在控制器中,我有:def create@book=book.new(params[:book])respond_to do | format | if@book.save flash[:notice]=“book已成功创建。”format.html{redirect_to(@book)}else format.html{render:action=>“new”}您可以查看开发服务器日志并粘贴参数吗。从那里,我会尝试在脚本/控制台上用这些参数创建一本新书,看看你是否能找到任何看起来不合适的东西(或修复它的东西)。这里:参数:{“提交”=>“Crear”,“真实性令牌”=>“1E0B41988B8C3908A52CE5422E0852A76AFF1A4”,“操作”=>“创建”,“作者字符串”=>,“控制器”=>“书籍”=>{“上传图片”=>,“标题”=>,“isbn”=>“00”,“发布”=>,“页面”=>“100”,“说明”=>,“编辑字符串”=>“AAAAA”}好的,打开脚本/控制台,将这些参数分配给一个变量,然后尝试在控制台上创建一本书。你所做的应该可以。是的。可以。但我的问题是,如果我在提交的数据中遇到验证错误(例如ISBN no validates),当重新显示显示错误时,我丢失了编辑字段的值(此字段的提交值)。我认为@rnicholson的答案是正确的,我需要接受\u嵌套的\u属性\u,但我还没有rails的版本:(.谢谢。但是,我有一个问题,当表单重新显示时,由于验证错误,我丢失了字段编辑的值。而且我也不知道如何防止在书未验证时创建编辑。谢谢你的回复,我使用的是UbuntuAptitude工具安装的rails 2.1.0-6,所以我没有这个功能.我想我会改变形式,不允许以书的形式创建编辑。
class Book < ActiveRecord::Base
  accepts_nested_attributes_for  :editorial
  ...
end
...
<% f.fields_for :editorial do |editorial_form| %>
  <%= editorial_form.label :name, 'Editorial:' %>
  <%= editorial_form.text_field :name %>
<% end %>
...