Rails的:虚拟属性和表单值 [英] Rails: Virtual attributes and form values
问题描述
我有一个模型书与创建图书形式编辑虚拟属性。 在code是这样的:
类图书<的ActiveRecord :: Base的
的has_many:book_under_tags
的has_many:标签,:通过=> :book_under_tags
HAS_ONE:编辑
的has_many:written_by
的has_many:作者,:通过=> :written_by
高清editorial_string
self.editorial.name除非editorial.nil?
结束
高清editorial_string =(输入)
self.editorial = Editorial.find_or_create_by_name(输入)
结束
结束
和新的形式:
<%的form_for(@book,
:HTML => {:多部分=>真})办| F | %>
&其中;%= f.error_messages%GT;
...
&其中p为H.;
<%= f.label:editorial_string,社论:%>< BR />
<%= f.text_field:editorial_string,:大小=> 30%> <跨度类=,例如> EJ。联盟杯< / SPAN>
&所述; / P>
...
通过这一点,当表单数据没有经过我失去了submited在编辑字段中的数据时的形式重新显示,并且也创造了一个新的编辑器的验证。我怎样才能解决这个两个问题?我是pretty的新的红宝石,我无法找到一个解决方案。
更新我的控制器:
高清创建
@book = Book.new(PARAMS [:书])
respond_to代码做|格式|
如果@ book.save
闪光[:通知] ='书已成功创建。
的format.html {redirect_to时(@book)}
format.xml {渲染:XML => @book,:状态=> :创建:位置=> @书 }
其他
的format.html {渲染:行动=> 新 }
format.xml {渲染:XML => @ book.errors,:状态=> :unprocessable_entity}
结束
结束
结束
我相信它的原因你的书#editorial_string方法总是返回。能简化为以下内容:
高清editorial_string
社论 ? editorial.name:
结束
根据评论更新:
听起来像是你想要做的嵌套形式。 (见 accepts_nested_attributes_for的API文档)请注意,这是在新的Rails 2.3。
所以,如果你更新你的书类
类图书<的ActiveRecord :: Base的
accepts_nested_attributes_for:编辑
...
结束
(你也可以现在取出editorial_string =,editorial_string方法太)
和更新您的形式,以类似下面的
...
<%f.fields_for:编辑做| editorial_form | %>
<%= editorial_form.label:姓名,社论:%>
<%= editorial_form.text_field:名称%>
<%结束%GT;
...
I have a Model Book with a virtual attribute for create a Editor from the Book form. The code looks like:
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
I believe its cause your Book#editorial_string method will always return "". Could simplify to the following:
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.
So if you update your Book class
class Book < ActiveRecord::Base
accepts_nested_attributes_for :editorial
...
end
(You could also now remove the editorial_string=, editorial_string methods too)
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 %>
...
这篇关于Rails的:虚拟属性和表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!