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

时间:2012-06-15 09:30:58

标签: ruby-on-rails routing controllers

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

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

“HTTP://本地主机:3000 /注册/ 3”

它显示:

“HTTP://本地主机:3000 / erinwalker”

所以我将路线改为

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

我的控制器中的show动作:

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

它确实有效,但是当我现在首先创建或更新注册表时,它会说:

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

即使show动作有效吗?

注册管理机构控制人:     class RegistriesController< ApplicationController的     before_filter:authenticate_user!,:except => :节目     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"

1 个答案:

答案 0 :(得分:0)

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

/registries与您的上一个规则match '/:name' => "registries#show"匹配,因此请求会点击show操作。

如果你运行rake routes,你会看到类似这样的内容:

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

您可以在路线中添加method参数,以便仅hit请求show GET

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

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

P.S。我认为load_and_authorize_resource默认情况下不会对您的show操作有效。因为它希望params [:id]自动加载资源。