2012-10-02 5 views
0

현재 사진을 업로드하는 프로젝트를 진행 중입니다. 해당 앨범에는 사용자, 앨범 및 그림이 있습니다. 나는 앨범으로 사용자를 만들 때까지 (세션, 인증 또는 아직 로그인하지 않았지만 등록을 마쳤습니다). 나는 내 new.html.erb의 URL이 괜찮다는 것을 알 수레일 : ActiveRecord :: RecordNotFound - ID없이 사용자를 찾을 수 없습니다.

Couldn't find User without an ID

레일 내 양식 제출 후 오류를 수신하지 해요 :

http://localhost:3000/users/13/albums/new (문제)

하지만, 내가 제출 한 후, 그것은 나에게 오류를 제공하고, 페이지는 다음과 같습니다

http://localhost:3000/albums/58 (. 문제 URL에 어떤 USER_ID)

,

누군가 내 루트가 방금 왜 갑자기 변했고 어떻게 고쳐야하는지 알 수 있습니까? 오류는 내 app/controllers/albums_controller.rb : 14 : @user = User.find (params [: user_id]) 행을 '표시'합니다.

new.html.erb

<%= form_for (@album), url: user_albums_path, :html => { :id => "uploadform", :multipart => true } do |f| %> 
<div> 
    <%= f.label :name %> 
    <%= f.text_field :name %> 


    <%= f.label :description %> 
    <%= f.text_area :description %> 

    <br> 

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

albums_controller

def show 
    @user = User.find(params[:user_id]) 
    @album = @user.albums.find(params[:id]) 
    @photo = @album.photos.build(params[:photo]) 
    respond_to do |format| 
    if @user.save 
     format.html { redirect_to album_photo_path(@album), notice: 'Album was successfully created.' } 
     format.json { render json: @album, status: :created, location: @album} 
    else 
     format.html { render action: "new" } 
     format.json { render json: @album.errors, status: :unprocessable_entity } 
    end 
    end 
end 

def update 
end 

def edit 
end 

def create 
    @user = User.find(params[:user_id]) 
    @album = @user.albums.build(params[:album]) 
    respond_to do |format| 
    if @user.save 
     format.html { redirect_to @album, notice: 'Album was successfully created.' } 
     format.json { render json: @album, status: :created, location: @album} 
    else 
     format.html { render action: "new" } 
     format.json { render json: @album.errors, status: :unprocessable_entity } 
    end 
    end 
end 

루트

Pholder::Application.routes.draw do 
resources :users do 
    resources :albums 
end 

resources :albums do 
    resources :photos 
end 
+0

당신이 대답을 본 적이 있나요? http://stackoverflow.com/questions/1303980/activerecordrecordnotfound-couldnt-find-user-without-an-id?rq=1 –

+0

예전에 이미 보았지만 나에게는 아무 것도하지 않았습니다. 같은 오류 – Edmund

+0

나는 문제가 내 앨범 # 리디렉션을 만드는 것 같아요 ... – Edmund

답변

0
당신이 (단지 2 단계 깊이) 정의과 같이 중첩 된 경로를 지정할 수 있습니다

하지만 혼란이 증가합니다. 그것은 때문에 당신이 당신의 작성 방법에 리디렉션하는 방식이다

resources :users do 
    resources :albums do 
    resources :photos 
    end 
end 

체크 아웃 http://guides.rubyonrails.org/routing.html#nested-resources

0

: 그것은으로 지정하는 것이 좋습니다. redirect_to @album을 수행하면 개체, 상태 및 메서드를 사용하여 리디렉션 할 올바른 경로를 찾습니다. 예 : @album.persisted?이고 메소드가 GET이면 경로는 album_path이됩니다. 지속되지 않으면 new_album_path이됩니다.

사용자 개체도 추가하면 오른쪽 ID가있는 user_album_path의 경로를 평가합니다.

format.html { redirect_to [@user, @album], 
       notice: 'Album was successfully created.' } 

경로를 더 잘 만들 수 있습니다. 이미 앨범에 대한 액세스가 중첩 된 user_album 경로를 통해 자원 지정한 등 :

resources :albums do 
    resources :photos 
end 

를 지정하여 실제로 같은 "/ 앨범", "ID/앨범 /"같은 앨범에 대한 경로를 선언한다. 하나 더 좋은 방법은 무엇 surase mentioned in his answer, 또는, just scope your photos route을 수행

scope "/album" do 
    resources :photos 
end 
관련 문제