Ich habe eine Rails-App mit einem Listing-Modell, das has_many Courses hat, was has_many Course_photos.Rails routing - Erstes Argument in form kann nicht nil enthalten oder leer sein
Die Schienen Weg für neue course_photos Laden ist „listing_course_photos“ oder /listings/:listing_id/courses/:id/course_photos(.:format)
Bei dem Versuch, dieses Formular zu laden course_photos hochzuladen ich folgendes erhalten Fehler ...
Fehler
ArgumentError in Listings#courses
Showing /Users/Jack_R/code/rails/planet_study/app/views/courses/_photo_upload.html.erb where line #1 raised:
First argument in form cannot contain nil or be empty
Die Routen sind wie folgt verschachtelt:
resources :listings, except: [:edit] do
member do
*****
end
resources :courses, except: [:edit] do
#delete :destroy, on: :collection
member do
******
resources :course_photos, only: [:create, :destroy]
end
end
end
ich die Fotos von einem Teilansicht/Kursen/_photo_upload.html.erb bin Hochladen:
<%= form_for @course, url: listing_course_photos_path([@listing, @course]), method: 'post', html: {multipart: true} do |f| %>
<div class="row">
<div class="form-group">
<span class="btn btn-default btn-file text-babu">
Select Photos
<%= file_field_tag "images[]", type: :file, multiple: true %>
</span>
</div>
</div>
<div class="text-center">
<%= f.submit "Add Photos", class: "btn btn-form" %>
</div>
<% end %>
<br/>
<div id="course_photos"><%= render 'course_photos/course_photos_list' %></div>
Was wiederum macht diesen Teil:
<% if @course_photos.count > 0 %>
<br/>
<div class="row">
<% @course_photos.each do |photo| %>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading preview">
<%= image_tag photo.image.url %>
</div>
</div>
<% end %>
</div>
</div>
<% end %>
Course_Photos-Controller
class CoursePhotosController < ApplicationController
before_action :set_listing
before_action :set_course, except: [:index, :new, :create]
def create
@listing = Listing.find(params[:listing_id])
@course = Course.find(params[:course_id])
@listing.course = listing.course
if params[:images]
params[:images].each do |img|
@course.course_photos.create[image: img]
end
@photos = @course.course_photos
redirect_back(fallback_location: request.referer, notice: "Saved...")
end
end
private
def set_course
@listing = Listing.find(params[:listing_id])
@course = Course.find(params[:id])
end
def set_listing
@listing = Listing.find(params[:listing_id])
end
end
Irgendwelche Vorschläge stark
Sie sollten '@ course'-Variable mit' @ listing.courses.new' initialisieren oder so etwas –
danke @PetrBalaban Ich habe versucht mit diesem: "listing_course_photos_path ([@ listing, @ listing.courses.new])" if das hast du gemeint? Ich habe den gleichen Fehler –
Sie müssen '@ listing.courses.new' als erstes Argument in' form_for' verwenden. Aber ich schlage vor, dass Sie diese Zuordnung zu "Listings # courses" extrahieren. –