2015-05-02 30 views
1

對於new method下面我運行的問題是,current_user沒有organization_id,如果該用戶直接訪問新路徑而不是通過提供的鏈接(鏈接傳遞id但直接訪問url沒有id )。如果沒有ID,則會顯示錯誤消息Couldn't find Organization with 'id'=如何調整新的方法來提供id參數不存在的情況?

def new 
    if current_user && current_user.admin? 
    @organization = Organization.find(params[:organization_id]) 
    @member = @organization.members.build 
    elsif current_member && current_member.admin? 
    @member = current_organization.members.build 
    else 
    flash[:danger] = "Error" 
    end 
end 

我怎樣才能調整new method,這樣當沒有提供的organization_ID有一個錯誤閃光燈和重定向?在現有的if ... line中,我嘗試過:

if params[:organization_id].nil 
    flash[:danger] = "Please select an organization first" 
    redirect_to organizations_path 
end 

但是,這給出了錯誤undefined method 'nil' for nil:NilClass

回答

1

的方法被稱爲.nil?,不.nil

params[:organization_id].nil? 
相關問題