我正在使用simple_form gem并生成我正在指定remote:true选项的表单,如下所示:
<%= simple_form_for @webinar, validate: true, remote:true do |f| %>
Run Code Online (Sandbox Code Playgroud)
因此,表单的输出html是以下片段:
<form accept-charset="UTF-8" action="/webinars" class="simple_form new_webinar" data-remote="true" data-validate="true" enctype="multipart/form-data" id="new_webinar" method="post" novalidate="novalidate"> ... </form>
Run Code Online (Sandbox Code Playgroud)
在我检查时,使用标准的form_for帮助器是在使用remote:true选项时将data-remote ='true'添加到表单中.正如你从生成的html中看到的那样,当我使用simple_form gem时,也有这样的属性.
所以,在我的控制器中,我有:
def create
@webinar = Webinar.new(params[:webinar])
respond_to do |format|
if @webinar.save
format.html { redirect_to @webinar, notice: 'Webinar was successfully created.' }
format.js
format.json { render json: @webinar, status: :created, location: @webinar }
else
format.html { render action: "new" }
format.json { render json: @webinar.errors, status: :unprocessable_entity …Run Code Online (Sandbox Code Playgroud) 所以我玩CoffeeScript,Rails 3.1所有好东西.我有一个资源,包含所有常用路由索引,显示,创建,编辑,更新,销毁.
索引视图的表单使用:remote => true如下:
<%= form_for @todo, :remote => true do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在创建控制器中,我有以下内容:
def create
@todo = Todo.new(params[:todo])
respond_to do |format|
if @todo.save
format.html { redirect_to @todo, notice: 'Todo was successfully created.' }
format.json { render json: @todo, status: :created, location: @todo }
format.js {render json: @todo }
else
format.html { render action: "new" } …Run Code Online (Sandbox Code Playgroud)