2014-07-22 4 views
1

Railscast의 Carrierwave 튜토리얼을 시작점으로 사용하여 내 이미지 모델을 내 목록 모델의 자식으로 만들려고합니다. 나는 경로를 변경하지만이 URL의 로컬 호스트로가는 이미지를 업로드 할 때 : 300/명부/1/이미지/새로운이 오류를 얻을 : 정의되지 않은 메서드`images_path를 '# < 번호 : 0x00000109abfd88>정의되지 않은 메소드`images_path '(Carrierwave + Nested Resources)

도움이 필요하십니까? 감사.

image_controller.rb

class ImagesController < ApplicationController 
    before_action :set_image, only: [:show, :edit, :update, :destroy] 

    def index 
     # I changed this so the images would appear on localhost:300/listings/3/images 
     # Don't know if it's the best way to accomplish that 
     @images = Listing.find(params[:listing_id]).images 
    end 

    def new 
     @image = Image.new(:listing_id => params[:listing_id]) 
    end 

    def edit 
    end 

    def create 
     @image = Image.new(image_params) 

     respond_to do |format| 
     if @image.save 
      format.html { redirect_to :back, notice: 'Image was successfully created.' } 
      format.json { render action: 'show', status: :created, location: @image } 
     else 
      format.html { render action: 'new' } 
      format.json { render json: @image.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

    def update 
     respond_to do |format| 
     if @image.update(image_params) 
      format.html { redirect_to @image, notice: 'Image was successfully updated.' } 
      format.json { head :no_content } 
     else 
      format.html { render action: 'edit' } 
      format.json { render json: @image.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

    def destroy 
     @image.destroy 
     respond_to do |format| 
     format.html { redirect_to :back } 
     format.json { head :no_content } 
     end 
    end 

    private 
     # Use callbacks to share common setup or constraints between actions. 
     def set_image 
     @image = Image.find(params[:id]) 
     end 

     # Never trust parameters from the scary internet, only allow the white list through. 
     def image_params 
     params.require(:image).permit(:file, :listing_id) 
     end 
end 

images.rb 양식

<%= form_for [@listing, @image], :html => { :multipart => true } do |f| %> 
    <% if @image.errors.any? %> 
     <div id="error_explanation"> 
     <h2><%= pluralize(@image.errors.count, "error") %> prohibited this image from being saved:</h2> 

     <ul> 
     <% @image.errors.full_messages.each do |msg| %> 
      <li><%= msg %></li> 
     <% end %> 
     </ul> 
     </div> 
    <% end %> 

    <%= f.hidden_field :listing_id %> 

    <div> 
     <%= f.label :file, 'Upload Images' %> 
     <%= f.file_field :file %> 
    </div> 

    <div> 
     <%= f.submit %> 
    </div> 
<% end %> 

답변

0
당신은 당신의 컨트롤러 @listing 객체를 초기화하지 않은

하지만보기에서 사용.

메이크업이 변경

def new 
    @listing = Listing.find params[:listing_id] 
    @image = Image.new(:listing_id => params[:listing_id]) 
end 
관련 문제