摆脱forms,只使用按钮创建新记录

Rails 3.1。 我有以下内容:

// _form.html.erb  true) do |td| %> 
"next_state" %>
// 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并只使用普通的abutton来触发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