【问题标题】:Rails link to page not routing to where I wantRails 链接到页面没有路由到我想要的位置
【发布时间】:2017-05-08 06:02:53
【问题描述】:

我想要显示的页面不是索引、显示、编辑等。我想要的页面是 adoption.html.erb,但由于某种原因它正在获取显示页面 ID。在我的views/shared/_header.html.erb 文件中,我有<li><%= link_to "More About Dog Adoption", adoption_info_path, class: "dog-adopt-info" %></li>。应该去这里:adoption.html.erb,但错误的是:Couldn't find Dog with 'id'=adoption。我真的很困惑,不知道为什么它会从节目中获取 id。想法?附言如果您需要更多信息,请告诉我。

class DogsController < ApplicationController

  def index
    @dogs = Dog.all
  end

  def show
    @dog = Dog.find(params[:id])
  end

  def adoption
  end

end

routes.rb

resources :dogs, only: [:index, :show]
get "dogs/adoption", as: "adoption_info"

app/views/shared/_header.html.erb

 <a class="page-scroll" href="#about">About Dog Adoption</a>
 <ul class="about-dog-adoption">
   <li><%= link_to "More About Dog Adoption", adoption_info_path, class: "dog-adopt-info" %></li>
 </ul>

当我点击上面的链接时,出现错误:

我的 rspec 测试通过了,这似乎是误报:

context "#dogs_adoption" do
  it "renders more info about dog adoptions" do
     get :adoption
     expect(response).to have_http_status(200)
  end
end

【问题讨论】:

    标签: ruby-on-rails routes capybara erb rspec-rails


    【解决方案1】:

    试试下面的代码:

    get "dogs/adoption", as: "adoption_info"
    resources :dogs, only: [:index, :show]
    

    问题:您的路线“/dogs/adoption”进入狗控制器,显示为带有 id:“adoption”的操作。

    【讨论】:

    • 这很容易哈哈。所以对于路线,顺序很重要。我现在才记得差不多一年前遇到过类似的事情。谢谢你,@puneet18!
    猜你喜欢
    • 2022-01-16
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    相关资源
    最近更新 更多