Ruby on Rails: Route not updating correctly

2019-09-11 03:18发布

I've got the following route defined:

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

Which I changed from:

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

The controller is as follows:

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

I get the following error:

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

Which suggests the route hasn't updated?

Here's the rake route:

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

I've tried restarting the server, but I'm not really sure what to do now? How can I get the route to go to 'edit' not 'show'?

Thanks,

LM.

EDIT:

Here's the form from the index page:

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

EDIT 2:

See here for the explantion of what I'm trying to achieve with this code: Ruby on Rails: Find a record by an attribute not an id

1条回答
成全新的幸福
2楼-- · 2019-09-11 03:58

Why don't you try to create another name for that method because resources :invites always generate the complete CRUD so maybe it's a conflict

Try something like that in routes.rb

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

Then you call it in the form like that

<%= form_tag search_invites_path, method: :get do %>
查看更多
登录 后发表回答