2012-04-11 6 views
0

Rails 3.1. Ich habe folgende:Formular loswerden und nur Schaltfläche zum Erstellen eines neuen Datensatzes verwenden

// _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 

Sie werden feststellen, dass ich einen form Tag für Benutzer erstellt einen Datensatz zu erstellen, wenn der Submit-Button geklickt wird. Aber ich bin mir nicht sicher, ob ich die ganze form_for loswerden könnte und nur eine normale a oder button verwenden, um die create auszulösen, weil ich irgendwie denke, dass das gesamte Formular redundant ist, da es keine Notwendigkeit für den Benutzer gibt, etwas einzugeben. Mein Javascript gibt den Wert automatisch ein.

Bitte beraten. Danke vielmals.

Antwort

0

In meinem Staat Controller:

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 

Ersetzen Sie die form mit diesem:

<%= link_to "Add new state", country_states_path(@country), :remote => true, :method => :post %> 

Ich entfernte die nextState() Funktion, ganze form.

Verwandte Themen