本文介绍了Rails 中的路由使用户名成为 URL:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 应用中,有设备模型 - 用户和注册表模型(每个用户有一个注册表).

In my Rails App there is Device Model - User, and a Registry model( Each user has one registry).

我想改变我的路线,而不是:

I wanted to change my routes so that instead of:

"http://localhost:3000/registries/3"

"http://localhost:3000/registries/3"

显示:

"http://localhost:3000/erinwalker"

"http://localhost:3000/erinwalker"

所以我把路线改为

match '/:name' => "registries#show"

我的控制器中的 show 动作:

And the show action in my controller to:

def show
@user = current_user
@user = User.find_by_name!(params[:name])
@registry = @user.registry

它可以工作,但是当我现在首先创建或更新注册表时,它说:

And it works, but when I create or update the registry now first it says:

Couldn't find User with name =
app/controllers/registries_controller.rb:21:in `show'

即使表演动作有效?

注册表控制器:类注册表控制器 :showload_and_authorize_resource

The registries controller: class RegistriesController < ApplicationController before_filter :authenticate_user!, :except => :show load_and_authorize_resource

# GET /registries
# GET /registries.json
def index
@registries = Registry.all
@user = current_user
respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @registries }
end
end

# GET /registries/1
# GET /registries/1.json
def show
@user = current_user
@user = User.find_by_name!(params[:name])
@registry = @user.registry


respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @registry }
end
end

# GET /registries/new
# GET /registries/new.json
def new
@registry = Registry.new
@user = current_user

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @registry }
end
end

# GET /registries/1/edit
def edit
@registry = Registry.find(params[:id])
@user = current_user
end

# POST /registries
# POST /registries.json
def create
@registry = current_user.build_registry(params[:registry])
@user = current_user

respond_to do |format|
  if @registry.save
    format.html { redirect_to @registry, notice: 'Registry was successfully created.' }
    format.json { render json: @registry, status: :created, location: @registry }
  else
    format.html { render action: "new" }
    format.json { render json: @registry.errors, status: :unprocessable_entity }
  end
end
end

# PUT /registries/1
# PUT /registries/1.json
def update
@registry = Registry.find(params[:id])
@user = current_user


respond_to do |format|
  if @registry.update_attributes(params[:registry])
    format.html { redirect_to @registry, notice: 'Registry was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @registry.errors, status: :unprocessable_entity }
  end
end
end

我所有的路线:

Mystorkparty::Application.routes.draw do

devise_for :users


resources :registries


root :to => "static_pages#home"

match '/home',    to: 'static_pages#home'

match '/:name' => "registries#show"

推荐答案

当您创建或更新模型时,您发送 POST/registriesPUT/registries/1.

When you create or update your models, you send POST /registries or PUT /registries/1.

但是 /registries 与您的最后一条规则匹配 match '/:name' =>"registries#show",所以请求命中了 show 动作.

But /registries is matched by your last rule match '/:name' => "registries#show", so the request hits the show action.

如果你运行 rake routes 你应该看到这样的:

If you run rake routes you should see something like this:

                POST   /registries(.:format)                 registries#create
                PUT    /registries/:id(.:format)             registries#update
                DELETE /registries/:id(.:format)             registries#destroy
                       /:name(.:format)                      registries#show

您可以将method 参数添加到您的路由中,以便它仅在GET 请求时hit show.

You can add method parameter to your route, so that it will hit show only on GET request.

match '/:name' => "registries#show", :via => :get

但未来仍有可能发生碰撞.例如,如果您有注册表名称 users.因此,通常建议使用前缀(match '/r/:name')或定义一组允许的名称,或为注册表选择安全名称.

But there are still can be collisions in the future. For example, if you have registry name users.So, it's commonly suggested to use prefixes (match '/r/:name') or define set of allowed names, or choose safe names for registries.

附言我不认为 load_and_authorize_resource 默认情况下适用于您的 show 操作.因为它期望 params[:id] 自动加载资源.

P.S. I don't think load_and_authorize_resource will work for your show action by default. Because it expects params[:id] to load the resource automatically.

这篇关于Rails 中的路由使用户名成为 URL:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 00:25