文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>simple_form使用

simple_form使用

时间:2010-10-08  来源:最初的模样

1.安装simple_form

 

sudo gem install simple_form  

2.修改Gemfile:

 

 

gem "simple_form" 

 3.生成配置文件

 

 

rails generate simple_form:install  
#生成文件   
 create  config/initializers/simple_form.rb  
 create  config/locales/simple_form.en.yml  
 create  lib/templates/erb/scaffold/_form.html.erb

4.以User model为例说明使用方法

 

 

<%= simple_form_for @user do |f| %>  
  <%= f.input :username %>  
  <%= f.input :password%>  
  <%= f.input :email%>  
  <%= f.input :profile,:as=>:text%> #as使用  
  <%= f.input :age, :collection => 18..60 , :prompt => "Select your age"%> #collection使用  
  <%= f.simple_fields_for :adress do |adress_form| %>  #simple_fields_for使用  
      <%= adress_form.input :name %>  
  <% end %>  
  <%= f.button :submit %>  
<% end %> 

  如果 不想生成 label,可以使其设置为false 

<%= f.input :login,:label=>false %>  

  同样,如果想使required为false

 

<%= f.input :login, :required => false %> 

5.关于as 
  在activerecord下,会自动对应相关映射,例如:string生成单行文本,text生成多行文本,不过如果使用mongodb,我使用的是mongoid,没有text 
类型,全是string,所以要自己手动as,例如 

 

 

<%= f.input :profile,:as=>:text%> 

6. Associations 使用 

 

 

class User < ActiveRecord::Base  
    belongs_to :company  
    has_and_belongs_to_many :roles  
  end  
  
  class Company < ActiveRecord::Base  
    has_many :users  
  end  
  
  class Role < ActiveRecord::Base  
    has_and_belongs_to_many :users  
  end 

  表单中我们可以使用 

 

 

<%= simple_form_for @user do |f| %>  
   <%= f.input :name %>  
   <%= f.association :company %>  
   <%= f.association :roles %>  
   <%= f.button :submit %>  
 <% end %> 

7.配置 config/initializers/simple_form.rb 
   Wrapper,如果想修改生成的标签为P(默认为div),只要修改 

 

SimpleForm.wrapper_tag = :p  
#默认情况下 
config.components = [ :label_input, :hint,:error]  
  如果不想使用error,只要去掉 即可 
config.components = [ :label_input, :hint]  
8.国际化,只要修改对应本地化文件即可
zh:  
  simple_form:  
    labels:  
      user:  
        username: '用户名'  
        password: '密码'  
    hints:  
      user:  
        username: '登录用户名.'  
        password: '输入正确的字符.'  
    placeholders:  
      user:  
        username: '你的用户名'  
        password: '****' 





 

 



 

相关阅读 更多 +
排行榜 更多 +
正太寿司屋游戏安卓版下载

正太寿司屋游戏安卓版下载

模拟经营 下载
波比的游戏时间第七章手游下载

波比的游戏时间第七章手游下载

休闲益智 下载
像素火影(次世代)千手扉间下载

像素火影(次世代)千手扉间下载

飞行射击 下载