ich ein Formular haben ein Album für die Erstellung wie folgt vor:HTML-Formular Datei-Eingabe nicht richtig Mitarbeiter mit Parametern Server übergeben [Rails]
# app/views/albums/new.html.erb
<h2>Details of Album</h2>
<% form_tag albums_path, :multipart => true do %>
<%= label_tag "Name" %><br />
<%= text_field_tag :name %><br />
<br />
<%= render(:partial => 'photos/photo_form') -%>
<%= link_to "Add another photo", '#', :id => 'addPhotoLink' %><br />
<br />
<%= submit_tag "Create Album" %>
<% end %>
In meiner application.js Datei Ich habe folgende Funktion
addPhotoForm: function() {
$('#addPhotoLink').click(function() {
$.get('/photos/new', null, function (data) {
$('#addPhotoLink').before(data);
}, 'script');
return false;
});
}
Die Photos # neue sieht aus wie folgt
def new
respond_to do |format|
format.js
end
end
Es folgt v entspricht iew:
<%= render(:partial => 'photo_form') -%>
Und photo_form
Teil
# app/views/photos/_photo_form.html.erb
<%= label_tag "Photo" %><br />
<%= file_field_tag 'photos[][image]' %><br />
<br />
<%= label_tag "Caption"%><br />
<%= text_field_tag 'photos[][caption]' %><br />
<br />
<%= label_tag "Tags(comma separated)" %><br />
<%= text_field_tag 'photos[][tags]' %><br />
<br />
Wenn ich auf "Hinzufügen ein weiteres Foto", erscheint photo_form sehr gut. Wenn ich Bilddatei, Beschriftung und Tags für alle Instanzen von Fotoformularen angeben, werden Attribute, die an den Server gesendet werden, sehr gut ankommen. Wie unten:
Processing AlbumsController#create (for 127.0.0.1 at 2010-12-08 18:39:52) [POST]
Parameters: {"name"=>"Crap", "commit"=>"Create Album", "photos"=>[{"tags"=>"skeleton, haddi, raja", "caption"=>"Hockey", "image"=>#<File:/tmp/RackMultipart20101208-10630-spoii-0>}, {"tags"=>"yoda, star, wars, star wars", "caption"=>"Yoda", "image"=>#<File:/tmp/RackMultipart20101208-10630-2rwp4s-0>}], "action"=>"create", "authenticity_token"=>"RaK8fdFo0cPoYOTIU7amt6GVnF0/5ss7xgX3Nfd9FjI=", "controller"=>"albums"}
Allerdings, wenn ich nicht zuerst Dateieingabetyp mit Pfad zu einer Datei auf der Platte füllen Sie und einen Pfad zu einer Datei auf dem zweiten Foto Formularinstanz angeben, Attribute an den Server gesendet in nicht ankommen korrekten Reihenfolge. Wie unten:
Processing AlbumsController#create (for 127.0.0.1 at 2010-12-08 19:59:20) [POST]
Parameters: {"name"=>"", "commit"=>"Create Album", "photos"=>[{"tags"=>"", "caption"=>"", "image"=>#<File:/tmp/RackMultipart20101208-10630-1iozpwx-0>}, {"tags"=>"reddit, alien, ,", "caption"=>"Reddit Alien idle"}], "action"=>"create", "authenticity_token"=>"Rrk88xJdXpp96SN8zS7dySTWwnmp8UJlTBksu4xtkkU=", "controller"=>"albums"}
siehe die 'Fotos' Taste in der obigen Parameterliste. Das Bild, das in der zweiten Fotoformularinstanz angegeben wurde, kommt in den ersten Hash, während es im zweiten Hash vorhanden sein sollte.
Was mache ich falsch?
Wenn ich die JavaScript-Funktion ändern, um wie folgt aussehen, funktioniert alles perfekt.
addPhotoForm: function() {
$('#addPhotoLink').click(function() {
$(this).before('<label for="Photo">Photo</label><br /><input id="photos__image" name="photos[][image]" type="file" /><br /><br /><label for="Caption">Caption</label><br /><input id="photos__caption" name="photos[][caption]" type="text" /><br /><br /><label for="Tags_comma_separated_">Tags(comma separated)</label><br /><input id="photos__tags" name="photos[][tags]" type="text" /><br /><br />')
return false;
});
Wo before
html entspricht app/views/photos/_photo_form.html.erb
geben wird.
EDIT: Nach dem HTML-Code, dies vor hergestellt wird durch Browser (Firefox 3.6.12)
<h2>Details of Album</h2>
<form action="/albums" enctype="multipart/form-data" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="Rrk88xJdXpp96SN8zS7dySTWwnmp8UJlTBksu4xtkkU=" /></div>
<label for="Name">Name</label>
<input id="name" name="name" type="text" /><br />
<br />
<label for="Photo">Photo</label><br />
<input id="photos__image" name="photos[][image]" type="file" value="" /><br />
<br />
<label for="Caption">Caption</label><br />
<input id="photos__caption" name="photos[][caption]" type="text" /><br />
<br />
<label for="Tags_comma_separated_">Tags(comma separated)</label><br />
<input id="photos__tags" name="photos[][tags]" type="text" /><br />
<br />
<a href="#" id="addPhotoLink">Add another photo</a><br />
<br />
<input name="commit" type="submit" value="Create Album" />
</form>
Können Sie die resultierende HTML (wie vom Browser erhalten), bitte, ich 'RoR' nicht lesen. –
Als Faustregel möchten Sie fragen: "Warum produziert dieser Ruby diesen HTML-Code nicht?" oder "Warum wird dieser HTML-Code nicht so von meinem Browser behandelt?" anstatt von Ruby direkt zu Verhalten. – Quentin
Dort habe ich den empfangenen HTML eingefügt. – Waseem