0

レール 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通常の方法を使用するabutton、トリガーすることができるかどうかはわかりません。create私のJavaScriptは値を自動的に入力します。

お知らせ下さい。どうもありがとう。

4

1 に答える 1

0

私の状態コントローラーでは:

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

于 2012-04-19T13:49:47.740 に答える