Ruby on Rails:路线未正确更新 [英] Ruby on Rails: Route not updating correctly

查看:31
本文介绍了Ruby on Rails:路线未正确更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下路线:

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

我已经尝试重新启动服务器,但我不确定现在该怎么做?我怎样才能获得去编辑"而不是显示"的路线?

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'?

谢谢,

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:通过以下方式查找记录不是 id 的属性

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

推荐答案

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

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

在 routes.rb 中尝试类似的操作

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 %>

这篇关于Ruby on Rails:路线未正确更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆