原文

1. form_tag

1) 基础 Form

<%= form_tag do %>
Form contents
<% end %>

生成 html

<form accept-charset="UTF-8" action="/" method="post">
<input name="utf8" type="hidden" value="✓" />
<input name="authenticity_token" type="hidden" value="J7CBxfHalt49OSHp27hblqK20c9PgwJ108nDHX/8Cts=" />
Form contents
</form>

  

2) 指定 url与 method

<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>

生成 html

<form accept-charset="UTF-8" action="/search" method="get">
<input name="utf8" type="hidden" value="✓" />
<label for="q">Search for:</label>
<input id="q" name="q" type="text" />
<input name="commit" type="submit" value="Search" />
</form>

3) 指定 controller 与 action

form_tag({controller: "people", action: "search"}, method: "get", class: "nifty_form")
# => '<form accept-charset="UTF-8" action="/people/search" method="get" class="nifty_form">'

  

4)几个 tag

check_box_tag

<%= check_box_tag(:pet_dog) %>
<%= label_tag(:pet_dog, "I own a dog") %>
<%= check_box_tag(:pet_cat) %>
<%= label_tag(:pet_cat, "I own a cat") %> => <input id="pet_dog" name="pet_dog" type="checkbox" value="1" />
<label for="pet_dog">I own a dog</label>
<input id="pet_cat" name="pet_cat" type="checkbox" value="1" />
<label for="pet_cat">I own a cat</label>

radio_button_tag

<%= radio_button_tag(:age, "child") %>
<%= label_tag(:age_child, "I am younger than 21") %>
<%= radio_button_tag(:age, "adult") %>
<%= label_tag(:age_adult, "I'm over 21") %> => <input id="age_child" name="age" type="radio" value="child" />
<label for="age_child">I am younger than 21</label>
<input id="age_adult" name="age" type="radio" value="adult" />
<label for="age_adult">I'm over 21</label>

2. form_for

1)如果设置了 resources :articles

## Creating a new article
# long-style:
form_for(@article, url: articles_path)
# same thing, short-style (record identification gets used):
form_for(@article) ## Editing an existing article
# long-style:
form_for(@article, url: article_path(@article), html: {method: "patch"})
# short-style:
form_for(@article)

2)手动设置 action

<%= form_for @person, url: {action: "create"} do |person_form| %>
<%= person_form.text_field :name %>
<%= fields_for @person.contact_detail do |contact_detail_form| %>
<%= contact_detail_form.text_field :phone_number %>
<% end %>
<% end %>

生成 html

<form accept-charset="UTF-8" action="/people" class="new_person" id="new_person" method="post">
<input id="person_name" name="person[name]" type="text" />
<input id="contact_detail_phone_number" name="contact_detail[phone_number]" type="text" />
</form>

3)手动设置 action 与 class

controller

def new
@article = Article.new
end

app/views/articles/new.html.erb

<%= form_for @article, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, size: "60x12" %>
<%= f.submit "Create" %>
<% end %>

生成 html

<form accept-charset="UTF-8" action="/articles" method="post" class="nifty_form">
<input id="article_title" name="article[title]" type="text" />
<textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea>
<input name="commit" type="submit" value="Create" />
</form>

4)限定命名空间

form_for [:admin, @article]

表格将提交到 在 admin 空间下的  ArticlesController (比如对于 update 方法提交到 admin_article_path(@article) ).

3. form_with

参考:https://m.patrikonrails.com/rails-5-1s-form-with-vs-old-form-helpers-3a5f72a8c78a

1)无 model

<%= form_with url: users_path do |form| %>
<%= form.text_field :email %>
<%= form.submit %>
<% end %>

2)有 model

<%= form_with model: @user do |form| %>
<%= form.text_field :email %>
<%= form.submit %>
<% end %>

3)取消自动 ids与 class,需要手动指定

<%= form_with model: @user do |form| %>
<%= form.text_field :name %>
<%= form.text_field :email, id: :email, class: :email %>
<% end %>

生成 html

<form action="/users" ...>
...
<input type="text" name="user[name]" />
<input id="email" class="email" type="text" name="user[email]" /> </form>

设定 label

<%= form_with model: @user do |form| %>
<%= form.label :name %>
<%= form.text_field :name, id: :user_name %>
<% end %>

注意 rails 5.2 自动生成 ids

4)form id 与 class 不再包裹在 {}中

之前

<%= form_for @user, html: { id: :custom_id, class: :custom_class } do |form| %>
<% end %>

现在

<%= form_with model: @user, id: :custom_id, class: :custom_class do |form| %>
<% end %>

  

5)form_field 适应 model属性

<%= form_with model: @user, local: true do |form| %>
<%= form.text_field :email %>
<%= form.check_box :send_welcome_email %>
<%= form.submit %>
<% end %>

这样就会在 user作用域中添加该参数,如果不想这么做,建议使用  check_box_tag 代替 form.check_box

params[:user][:send_welcome_email]

 

6)form_with 默认使用 ajax 提交(默认设置  remote: true)

如果不需要 ajax远程提交,必须指定 local:true

<%= form_with model: @user, local: true %>
<% end %>

  

而不指定 local:true,会生成 (优点是不刷新页面,缺点是无法显示 error信息)

<form action="/users" data-remote="true" method="post">
...
</form>

该类属性由 rails-ujs 处理, rails-ujs 的其他处理技巧,参见 https://m.patrikonrails.com/a-definitive-guide-to-railss-unobtrusive-javascript-adapter-ef13bd047fff

如何对 ajax提交做响应参考文档 http://guides.rubyonrails.org/working_with_javascript_in_rails.html

如上!

rails 杂记 - erb 中的 form_helper的更多相关文章

  1. rails 杂记 - erb 中的 link_to 的 ActiveRecord 与 render 中的 partial

    路由及路由参数 <%= link_to 'My Blog', {controller: 'articles', demo: "lidsi"}, class: "bl ...

  2. rails 杂记 - model 中的exists?

    1. exists? 用法 有一段代码 参考 def generate_token(column) begin self[column] = SecureRandom.urlsafe_base64 e ...

  3. 【Ruby on Rails】Model中关于保存之前的原值和修改状态

    今天在Rails的Model中遇到了一个问题—— 当我从Model类中获取了一个ActiveRecord对象,对其进行了一系列修改(尚未保存),我该如何确定究竟哪些修改了呢? (设Model为Opti ...

  4. rails项目编写中的一些小技巧小心得

    1. 如果form中有数据要传回服务器可以用隐藏属性的控件: form_for(xxx) do |f| f.hidden_field :xxx,value:xxx end 2. 如果你需要一些信息放在 ...

  5. rails 杂记 - render and layout

    官方文档:http://guides.rubyonrails.org/layouts_and_rendering.html 渲染 view 渲染 html.rb 与相应的 action control ...

  6. 剖析 Rails 3 MVC 中的数据传递

    引用链接:https://www.ibm.com/developerworks/cn/web/1108_linhx_rails3mvc/ 如果读者已经开发过基于 Rails 的应用,但对其 MVC 间 ...

  7. rails 项目部署中 nginx 报错及解决方法

    错误1 1. 报403错误,是因为启动nginx的用户默认是nobody,没有对项目目录的访问权限. user myName; worker_processes ; 错误2 2. 报404错误,是因为 ...

  8. Swing杂记——Swing中引入Android的NinePatch技术,让Swing拥有Android的外观定制能力

    [摘要] 本文诣在展示如何在Swing中引入 NinePatch技术(早期有文章里中文译作九格图,暂且这么叫吧^_^,但此术非传统移动手机上的功能布局——九格图哦). [准备篇] Q:何为 NineP ...

  9. 杂记 C中的volatile

    volatile 就象大家更熟悉的const一样,volatile是一个类型修饰符(type specifier).它是被设计用来修饰被不同线程访问和修改的变量.如果没有volatile,基本上会导致 ...

随机推荐

  1. ABAP非Unicode系统中字符串拼接(CONCATENATE)时吃字符问题

    系统是老R3,非Unicdoe系统,某些表字段是从外界系统过来的,由于接口设计的固定长度,外界系统传超长字符串过来后,就可能从最后一个中文字符中间截断,这问题到还没什么,只不过显示时最后一个字符显示成 ...

  2. Error-MVC: 未能找到路径“D:\\DsWeb\DS.Web\dist\bin\roslyn\csc.exe”的一部分。

    ylbtech-Error-MVC: 未能找到路径“D:\\DsWeb\DS.Web\dist\bin\roslyn\csc.exe”的一部分. 1.返回顶部 1, “/”应用程序中的服务器错误. 未 ...

  3. 【问题与解决】Mac OS通过 npm 安装 React Native 报错(checkPermissions Missing write access to /usr/local/lib/node_modules)

    报错情况: 当Mac OS通过 npm 安装 React Native 报错,警告文字为:checkPermissions Missing write access to /usr/local/lib ...

  4. git强制提交本地分支覆盖远程分支

    git push origin 分支名 --force eg: cd 代码目录 git push origin master --force 运行结果: Total 0 (delta 0), reus ...

  5. py3下怎么用StringIO

    try: from StringIO import StringIO except ImportError: from io import StringIO

  6. 评分模型的检验方法和标准&信用评分及实现

    评分模型的检验方法和标准通常有:K-S指标.交换曲线.AR值.Gini数等.例如,K-S指标是用来衡量验证结果是否优于期望值,具体标准为:如果K-S大于40%,模型具有较好的预测功能,发展的模型具有成 ...

  7. MATLAB 统计不同区间中元素的个数

    使用 find 命令: x = :;%生成数组 k = find( x > & x < );%查找大于2小于5的元素的数组下标 size(k,) %统计的元素的个数

  8. PHP操作二进制字节数据

    在PHP开发中大都是操作字符类数据,极为方便,但操作二进制又如何呢,下面代码举例看看. 函数:  pack(format,args+) pack()和unpack()函数的第一个参数表如下 Bash ...

  9. JSSDK微信支付封装的支付类方法,代码比较齐全,适合收藏

    第1肯定是配置的参数类 public class JsApiConfig { #region 字段 private string mch_id = string.Empty; private stri ...

  10. EL表达式具体解释

    在 JSP 页面中,使用标签库取代传统的 Java 片段语言来实现页面的显示逻辑已经不是新技术了,然而.由自己定义标签非常easy造成反复定义和非标准的实现.鉴于此.出现了 JSTL ( JSP St ...