首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Rails中路由使用户名成为URL:

在Rails中路由使用户名成为URL:
EN

Stack Overflow用户
提问于 2012-06-15 17:30:58
回答 1查看 924关注 0票数 1

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

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

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

它显示:

"http://localhost:3000/erinwalker

所以我把路线改成

代码语言:javascript
复制
match '/:name' => "registries#show"

在我的控制器中执行show操作,以:

代码语言:javascript
复制
def show
@user = current_user
@user = User.find_by_name!(params[:name])
@registry = @user.registry

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

代码语言:javascript
复制
Couldn't find User with name = 
app/controllers/registries_controller.rb:21:in `show'

即使表演动作起作用了?

注册表控制器: class RegistriesController < ApplicationController before_filter :authenticate_user!,:except => :show load_and_authorize_resource

代码语言:javascript
复制
# 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

我的所有路由:

代码语言:javascript
复制
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"
EN

回答 1

Stack Overflow用户

发布于 2012-06-15 19:19:27

创建或更新模型时,需要发送POST /registriesPUT /registries/1

但是/registries与您的最后一条规则match '/:name' => "registries#show"匹配,因此请求命中show操作。

如果您运行rake routes,您应该会看到如下所示:

代码语言:javascript
复制
                POST   /registries(.:format)                 registries#create
                PUT    /registries/:id(.:format)             registries#update
                DELETE /registries/:id(.:format)             registries#destroy
                       /:name(.:format)                      registries#show

您可以在您的路由中添加method参数,以便仅在GET请求时对show执行hit操作。

代码语言:javascript
复制
match '/:name' => "registries#show", :via => :get

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

附言:默认情况下,我不认为load_and_authorize_resource会为你的show操作工作。因为它期望params:id自动加载资源。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11048063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档