簡體   English   中英

Ruby on Rails:通過Devise獲得路線

[英]Ruby on Rails: Getting a route with Devise

TL; DR我該如何實現始終在帶有Mongoid的自定義控制器中將具有Devise用戶模型ID的路由返回(即,重定向到localhost:3000/profiles/504026426 )?

我有一個帶有社交配置文件控制器的項目,該項目使用Devise返回了一個用戶ID,但它總是會抱怨Mongoid需要有效的ID號才能使用此路由: localhost:3000/profiles/

# Let's say I want to return a route with id (Devise) without having
# Rails to complain that Mongoid needs to a id of some number.

# So, I have controller containing profiles in the files.
# It goes like..
class ProfilesController < ApplicationController
  before_filter :authenticate_user!
    # GET /profile/
    def index
      @user = User.find(params[:id])
    end
    # .. Snipped for brevity.
end

這是正確的方法嗎?

首先,您試圖通過其查找用戶的'params [:id]'在索引視圖中不起作用,因為您沒有通過url傳遞任何參數('profiles /:id'或:localhost :3000 /簡檔/ 504026426)。 如果要存儲可在索引視圖中使用的用戶,請使用會話和當前的用戶幫助器方法。 這里

如果您希望GET方法與'params [:id]'一起使用,它將看起來像:

get 'profile/:id' => 'profiles#show'

確保它與控制器中的顯示功能匹配

def show
  # this would work with: localhost:3000/profiles/504026426
     @user = User.find(params[:id])
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM