简体   繁体   中英

Ruby on Rails: Getting a route with Devise

TL;DR How can I achieve this to always return routes with Devise User model ID in custom controllers (ie redirecting to localhost:3000/profiles/504026426 ) with Mongoid?

I have a project that has a social profile controller that returns of a user id with Devise but it will always complain that Mongoid needs a valid ID number in order to work with this route: 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

Is this the right way to do it?

Firstly, the 'params[:id]' you're trying to find the user by won't work in the index view, because you're not passing in any parameter via the url ('profiles/:id' or: localhost:3000/profiles/504026426). If you want to store a user to be available in the index view, use sessions and the current user helper method. See here .

If you want your GET method to work with the 'params[:id]' it would look like:

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

make sure it matches a show function in your controller

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM