2

我正在使用simple_form与twitter boostrap结合起来用于我的rails应用程序。我有一个问题阻止了我。simple_form选择字段在验证后变成输入字段

<%= f.input :title %> 
<%= f.input :url %> 
<%= f.input :tag_list, :label => 'Tags' %> 
<%= f.input :type_id, :collection => @types, :label_method => :type_name, :value_method => :id, :include_blank => false %> 
<%= f.input :description %> 

这使我很好的形式。但是,如果验证失败,则每个输入字段都会显示其错误。 但不是选择字段,它只是从一个选择字段变成一个正常的输入字段填充ID,我不知道为什么。

任何想法?

回答

1

那么我需要更多的信息来帮助你。 你可以发布你的模型?或解释什么是@types是什么和它是什么。

这是用于关联的基本简单:

型号/ type.rb belongs_to :post

型号/ post.rb has_many :types

的意见/后/ _form .html.haml = f.association :types

确保您有一个名为“标题”或“名称”的类型的列。

+0

好的这个表单用于创建书签和类型描述书签类型如博客文章或论坛帖子。 和关联是: type.rb'has_many:书签' bookmark.rb'belongs_to:type' – Poisoned 2012-08-13 10:18:35

+0

你试过'= f.association:types'吗? – hellocodes 2012-08-13 14:34:24

0

您需要设置@types创建更新方法之前,渲染动作分别

编辑例如

def new 
    @your_object = YourObject.new 
    @types = Type.all 
    .... 
end 

def create 
    @your_object = YourObject.new(your_object_params) 

    respond_to do |format| 
     if @your_object.save 
     format.html { redirect_to ... } 
     format.json { render action: 'show' ... } 
     else 
     ### ! init data for the form ! ### 
     @types = Type.all 
     format.html { render action: 'new' } 
     format.json { render json: ... } 
     end 
    end 
end 
2

对于一些原因,看来Rails + Bootstrap + Simple_form让你在验证集合被放入时选择字段领域本身,而不是你的控制器的行动。这应该工作:

<%= f.input :title %> 
<%= f.input :url %> 
<%= f.input :tag_list, :label => 'Tags' %> 
<%= f.input :type_id, :collection => Type.all.order('type_name'), :label_method => :type_name, :value_method => :id, :include_blank => false %> 
<%= f.input :description %> 

希望这会有所帮助。

+0

奇怪的错误,只是面对它自己,不得不使用这个解决方案。不管怎么说,还是要谢谢你! ;) – chemic 2015-10-22 22:09:43