【问题标题】:Routing in Rails making the Username an URL:Rails 中的路由使用户名成为 URL:
【发布时间】:2012-06-18 08:52:09
【问题描述】:

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

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

“http://localhost:3000/registries/3”

它显示:

“http://localhost:3000/erinwalker”

所以我改变了路线到

match '/:name' => "registries#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'

即使表演动作有效?

注册表控制器: 类 RegistriesController :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"

【问题讨论】:

  • 如何创建/更新注册表?可以使用注册表的所有路由和所有控制器代码更新您的问题吗?

标签: ruby-on-rails routing controllers


【解决方案1】:

当您创建或更新模型时,您发送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 参数添加到您的路由中,以便它仅在GET 请求时才会hit show

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

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

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

【讨论】:

  • 谢谢,我加了,:via => :get,但是没有效果?
  • 我不应该更改创建和更新部分吗?如果我更改对@user.name 的响应,它也不起作用。我知道它只是我必须重写的一些小东西!
猜你喜欢
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 2011-02-24
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
相关资源
最近更新 更多