2014-11-01 2 views
0

FriendlyID의 wikirailscast에 이어 내 사용자 모델의 이름이 변경된 경우 내 URL을 다시 쓰고 리디렉션을 시도했습니다. 불행하게도 일어나는 일이 없습니다. 적절한 마이그레이션을 수행했습니다.FriendlyID 기록이 레일스에서 ​​업데이트 된 모델 URL에 대해 작동하지 않습니다.

rails generate friendly_id 
rails generate migration add_slug_to_artists slug:string:uniq 
rake db:migrate 

이 코드는 지침에 따라 적용했지만 사용할 수 없습니다. 처음에는 모델 인스턴스를 만든 후 URL이 동일하게 유지됩니다.

아티스트 모델

extend FriendlyId 
friendly_id :name, use: [:slugged, :history] 

사용자 컨트롤러 정말 설명되지 않습니다 FriendlyID 5 컨트롤러의 URL을 변경하는 그들의 docs 코드에 명시된 동안

class ArtistssController < ApplicationController 
    before_action :set_artist, only: [:show, :edit, :update, :destroy] 

    def show 
    if request.path != user_path(@artist) 
     redirect_to @artist, :status => :moved_permanently 
    end 
    end 

    def update 
    if @artist.update_attributes(artist_params) 
     flash[:notice] = 'Artist successfully updated' 
     redirect_to @artist 
    else 
     render 'edit' 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_artist 
     @artist = Artist.friendly.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def artist_params 
     params.require(:artist).permit(:name) 
    end 
end 
+0

,하지만 당신은'설정해야 슬러그 (sllug) '를'없애기 (nil) '로 만들어 재생성시킨다. –

+0

@ViniciusPinto 업데이트 액션 (기본 발상 자료에서 생성 된 코드)을 보여주기 위해 제 질문을 업데이트했습니다. 내가 뭘 권하고 싶니? –

+0

'update_attributes() '를 호출하기 전에'@artist.slug = nil'을 추가하십시오. –

답변

1

. 나는이 걸릴 효과를 @artist.slug = nil를 추가 할 필요 결국 그래서 난이 문제가 있는지 확실하지 않습니다 당신은`update` 방법에 대한 코드를 게시되지 않은

def update 
    @artist.slug = nil 
    if @artist.update_attributes(artist_params) 
    flash[:notice] = 'Artist successfully updated' 
    redirect_to @artist 
    else 
    render 'edit' 
    end 
end 
+0

'@artist.slug = nil'은'update_attributes'를 호출하기 전에 있어야합니다 –

+0

맞습니까? –

+0

보이지만 코드를 실행할 수 없습니까? –