我正在使用 Friendly id 来允许在我的 Rails 应用程序中从用户名进行自定义查询。
所以说用户的用户名是loremipsum
rails 会显示这个http://localhost:3000/users/loremipsum
,这工作得很好
但说用户的用户名是lorem.ipsum
或lorem-ipsum
我怎样才能让rails显示,lorem.ipsum
或者lorem-ipsum
在url中,因为它目前需要第一部分,然后说Couldn't find User with id=lorem
我的用户控制器
class UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @movies }
end
end
def search
@movies = @search.result
respond_to do |format|
format.html # index.html.erb
format.json { render json: @movies }
end
end
def show
@user = User.find(params[:id])
@movies = @search.result
end
def sign_up
@user = User.new
end
def sign_in
end
def create
@user = User.create( params[:user] )
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = @current_user
end
def update
if @user.update_attributes(params[:user])
sign_in @user
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed"
redirect_to users_path
end
def edit
end
end