所以我想尝试建立用户个人资料的链接!所以我在用户的控制器中有这个:
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
就像用户是空的!我已经登录了!所以我应该有一个用户活跃。
即使我使用redirect_to @user
它也不起作用!帮助
答案 0 :(得分:1)
我假设show
模板可以通过@user
实例变量访问'user'。如果是这样,那么您需要设置实例变量:
def show_user
@user = current_user // Or other method for getting the logged in user.
render 'show'
end
答案 1 :(得分:1)
假设您在索引页面中,并且您想创建一个链接以重定向到用户,您可以使用以下内容:
<%= link_to 'Show User', current_user %>
用户是用户对象,从默认的休息路径开始,您将显示页面的操作,因此无需在控制器“show_user”中创建操作
您可以提供控制器的视图以及如何调用show_user操作