2012-03-15 3 views
0

그래서 사용자 프로필에 대한 링크를 만들려고합니다! 그래서 사용자의 컨트롤러이 있습니다언제든지 사용자 프로필로 리디렉션 할 수 있습니다. Ruby on Rails

class UsersController < ApplicationController 

    def new 
    #defining the "@user" object for the form_for 
    @user = User.new  
    end 

    #defining a function for the "show" html file 
    def show 
    #get user id by URL address 
    @user = User.find(params[:id]) 
    end 

    #handling user login succes or failure 
    def create 
    @user = User.new(params[:user]) 
    if @user.save 

     #using "flash" variable Ruby 
     #flash[:success] = "Welcome to the Sample App!" <<< use this !! 

     #render success page 
     redirect_to @user 
    else 
     #render failure page (using error partial page) 
     render 'new' 
    end 
    end 

    #mine to show user profile 
    def show_user 

    render 'show' 

    end 
end 

내 routes.rb에 내가 가지고 :

DemoApp::Application.routes.draw do 

    get "intergration_test/authentication_pages" 

    #Removed! 
    #get "users/new" 

    #ROOT page 
    root to: 'static_pages#home' 

    #My routes 
    match '/user_profile', to: 'users#show_user' 

    #REST architecture for users 
    resources :users 

    #including the Sessions action 
    resources :sessions, only: [:new, :create, :destroy] 

    match '/signup', to: 'users#new' 
    match '/signin', to: 'sessions#new' 
    #Note the use of via: :delete for the signout route, which indicated that it should be invoked using an HTTP DELETE request 
    match '/signout', to: 'sessions#destroy', via: :delete 

end 

을 왜

#mine to show user profile 
    def show_user 

    render 'show' 

    end 

나에게 오류를 반환하지 :

undefined method `email' for nil:NilClass 

사용자가 null 인 것 같습니다! 이미 로그인했습니다! 그래서 나는 사용자를 활성화시켜야한다. redirect_to @user을 사용해도 작동하지 않습니다! 도움?

답변

1

나는 show 템플릿이 @user 인스턴스 변수를 통해 '사용자'에 액세스 할 수 있다고 가정합니다. 그렇다면, 당신은 인스턴스 변수를 설정해야하는 경우 :

def show_user 
@user = current_user // Or other method for getting the logged in user. 
render 'show' 
end 
1

은의 당신이 인덱스 페이지에 있다고 가정 해 봅시다, 당신은 당신이 단지 같은 것을 사용할 수있는 사용자로 리디렉션 할 수있는 링크를 만들려면 :

<%= link_to 'Show User', current_user %> 

사용자는 사용자 개체이고 기본 나머지 경로에서 페이지의 동작을 보여주기 위해 당신을 데려 갈 것이다, 그래서 필요가 컨트롤러 'show_user'

에서 작업을 만들 수 없습니다 그리고 당신의 뷰를 제공 할 수 있습니다 show_user 액션 호출 방법