Ruby on Rails:路由未正确更新

时间:2015-01-09 13:32:41

标签: ruby-on-rails ruby-on-rails-4

我已定义以下路线:

get 'invites/search', to: 'invites#edit', controller: :invites

我改变了:

get 'invites/search', to: 'invites#show', controller: :invites

控制器如下:

def show
  @invite = Invite.find(params[:id])
end

...

def edit
  # If the request is a search containing an 'invite_code'
  if params.has_key?(:invite_code)
    @invite = Invite.where(invite_code: params[:invite_code]).first
  else
    @invite = Invite.find(params[:id])
  end
end

我收到以下错误:

ActiveRecord::RecordNotFound in InvitesController#show

Couldn't find Invite with 'id'=search

Extracted source (around line #7):         

6  def show
7    @invite = Invite.find(params[:id])
8  end

def new

这表明路线还没有更新?

以下是佣金路线:

invites_search GET    /invites/search(.:format)                     invites#edit

我尝试重新启动服务器,但我现在还不确定该怎么做?我怎样才能获得去编辑'不是'显示'?

谢谢,

LM。

编辑:

这是索引页面中的表单:

<%= form_tag invites_search_path, method: :get do %>
  <%= label_tag :invite_code, "#" %>
  <%= text_field_tag :invite_code, nil %>
  <%= submit_tag "Search", name: nil %>
<% end %>

编辑2:

请参阅此处,了解我尝试使用此代码实现的目标: Ruby on Rails: Find a record by an attribute not an id

1 个答案:

答案 0 :(得分:1)

为什么不尝试为该方法创建另一个名称,因为resources :invites总是生成完整的CRUD,所以可能是冲突

在routes.rb中尝试类似的东西

resources :invites do
  resources :guests
  collection do
    get 'search'
  end
end

然后你用这样的形式称呼它

<%= form_tag search_invites_path, method: :get do %>