Rails:虚拟属性和表单值

时间:2022-10-17 15:57:01

I have a Model Book with a virtual attribute for create a Editor from the Book form. The code looks like:

我有一个带有虚拟属性的Model Book,用于从Book表单创建一个Editor。代码如下:

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

And the new form:

而新形式:

<% form_for(@book,
            :html => { :multipart => true }) do |f| %>
  <%= f.error_messages %>

...
  <p>
    <%= f.label :editorial_string , "Editorial: " %><br />
    <%= f.text_field :editorial_string, :size => 30  %> <span class="eg">Ej. Sudamericana</span>
  </p>
 ...

With this, when the form data no pass the validations I lost the data submited in the editorial field when the form is redisplayed, and also a new Editor is created. How I can fix this two problems? I am pretty new in ruby and I can't find a solution.

有了这个,当表单数据没有通过验证时,我丢失了在重新显示表单时在编辑字段中提交的数据,并且还创建了一个新的编辑器。我如何解决这两个问题?我是红宝石的新手,我找不到解决方案。

UPDATE my controller:

更新我的控制器:

  def create
    @book = Book.new(params[:book])
    respond_to do |format|
      if @book.save
        flash[:notice] = 'Book was successfully created.'
        format.html { redirect_to(@book) }
        format.xml  { render :xml => @book, :status => :created, :location => @book }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
      end
    end
  end

3 个解决方案

#1


I believe its cause your Book#editorial_string method will always return "". Could simplify to the following:

我相信它的原因你的Book#editorial_string方法将始终返回“”。可以简化为以下内容:

  def editorial_string
   editorial ? editorial.name : ""
  end

Update based on comment:

根据评论更新:

Sounds like you want to do nested forms. (See accepts_nested_attributes_for in api docs) Note this is new in Rails 2.3.

听起来你想做嵌套表格。 (请参阅api文档中的accepts_nested_attributes_for)注意这是Rails 2.3中的新增内容。

So if you update your Book class

因此,如果您更新您的Book类

class Book < ActiveRecord::Base
  accepts_nested_attributes_for  :editorial
  ...
end

(You could also now remove the editorial_string=, editorial_string methods too)

(您现在也可以删除editorial_string =,editorial_string方法)

And update your forms to something like the following

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

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

#2


The first problem is that

第一个问题是

def editorial_string
  self.editorial.name unless editorial.nil?
  ""
end

will always return "" because that is the last line.

将始终返回“”,因为这是最后一行。

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

would fix that problem. As far as why the validations don't pass, I don't know, what are you doing in the controller? What validation errors are you getting?

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

#3


Take a look at this podcast http://railscasts.com/episodes/167-more-on-virtual-attributes. I think you should move your find_or_create from the editorial_string=(input) method to call back after the save.

看一下这个播客http://railscasts.com/episodes/167-more-on-virtual-attributes。我认为你应该将你的find_or_create从editorial_string =(input)方法移到save之后回调。

#1


I believe its cause your Book#editorial_string method will always return "". Could simplify to the following:

我相信它的原因你的Book#editorial_string方法将始终返回“”。可以简化为以下内容:

  def editorial_string
   editorial ? editorial.name : ""
  end

Update based on comment:

根据评论更新:

Sounds like you want to do nested forms. (See accepts_nested_attributes_for in api docs) Note this is new in Rails 2.3.

听起来你想做嵌套表格。 (请参阅api文档中的accepts_nested_attributes_for)注意这是Rails 2.3中的新增内容。

So if you update your Book class

因此,如果您更新您的Book类

class Book < ActiveRecord::Base
  accepts_nested_attributes_for  :editorial
  ...
end

(You could also now remove the editorial_string=, editorial_string methods too)

(您现在也可以删除editorial_string =,editorial_string方法)

And update your forms to something like the following

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

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

#2


The first problem is that

第一个问题是

def editorial_string
  self.editorial.name unless editorial.nil?
  ""
end

will always return "" because that is the last line.

将始终返回“”,因为这是最后一行。

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

would fix that problem. As far as why the validations don't pass, I don't know, what are you doing in the controller? What validation errors are you getting?

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

#3


Take a look at this podcast http://railscasts.com/episodes/167-more-on-virtual-attributes. I think you should move your find_or_create from the editorial_string=(input) method to call back after the save.

看一下这个播客http://railscasts.com/episodes/167-more-on-virtual-attributes。我认为你应该将你的find_or_create从editorial_string =(input)方法移到save之后回调。