摆脱形式,仅使用按钮创建新记录
Getting rid of form and use just button to create new record
Rails 3.1.我有以下几点:
// _form.html.erb
<%= form_for ([@country, @state], :remote => true) do |td| %>
<div id= "state_errors" style="display:none"></div>
<%= td.text_field :position, :id => "next_state" %>
<div class="actions">
<%= td.submit %>
</div>
<% end %>
// global.js
function nextState() {
Array.max = function( array ) {
return Math.max.apply( Math, array );
};
var getLastState = $("input.sortable_state_item_position").map(function() {
return $(this).val();
}).get();
getLastState.push("0");
return Array.max(getLastState) + 1;
}
$("input#next_state").val(nextState());
// states_controller.rb
class StatesController < ApplicationController
before_filter :load
def load
@country = Country.find(params[:country_id])
@states = State.all
end
def create
@state = @country.states.build(params[:state])
@state.save
end
...
end
您会注意到,我创建了一个form
标签,供用户在单击提交按钮时创建记录。但我不确定我是否可以摆脱整个form_for
,只使用正常的a
或button
来触发create
,因为我有点觉得整个表单都是多余的,因为用户不需要输入任何东西。我的javascript会自动输入值。
请指教。非常感谢。
在我的状态控制器中:
def create
@state = @country.states.build(params[:trip_day])
@state.position = State.where(:country_id => @country.id).maximum(:position).to_i + 1
@state.save
end
将form
替换为以下内容:
<%= link_to "Add new state", country_states_path(@country), :remote => true, :method => :post %>
我删除了nextState()
功能,整个form
.
相关文章:
- 如何删除我用按钮创建的内容
- 单击按钮创建多实例jQuery UI对话框
- 如何为滑块的分页按钮创建简单的下一个和上一个触发器按钮
- Javascript使用变量通过按钮创建url
- 按钮创建无法定位功能代码错误
- 使用按钮创建新的下拉列表
- 点击按钮创建一个带有css动画的放射状进度条
- 从执行jscript列表按钮创建多个相关对象
- 在完整日历的Dayclick事件上触发jQuery qTip,然后使用其中的按钮创建事件
- 通过摇晃按钮创建小摆动
- 摆脱形式,仅使用按钮创建新记录
- 如何使用下一个和上一个按钮创建 jquery 滑块
- Chrome 扩展程序:通过单击按钮创建新标签页
- 使用按钮创建列表
- ExtJS:如何使用MessageBox按钮创建自定义模式窗口
- 如何在ASP.Net中单击按钮创建动态表
- carouFredSel:为next/prev按钮创建一个函数
- extjs如何使用单选按钮创建菜单
- 单击按钮创建表时,请选中“获取值”复选框
- 如何使按钮创建一个不同的文本,每次它是按下