gpt4 book ai didi

ruby-on-rails - 没有路由匹配获取signup_path

转载 作者:行者123 更新时间:2023-12-01 19:30:20 25 4
gpt4 key购买 nike

您好,我正在关注 Michael Hartl 的教程,目前在第 7 章。我刚刚添加了一个注册页面,但收到了错误消息:

编辑:添加更多错误消息没有路由匹配 [GET]“/signup_path”Rails.root:C:/Sites/sample_app

这是我的路线文件

SampleApp::Application.routes.draw do 
resources :users
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'

查看主页文件

<div class="center hero-unit">
<h1>Welcome to the Sample App</h1>

<h2>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</h2>

<%= link_to "Sign up now!", 'signup_path', class: "btn btn-large btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>

路线

users_path  GET /users(.:format)    users#index
POST /users(.:format) users#create
new_user_path GET /users/new(.:format) users#new
edit_user_path GET /users/:id/edit(.:format) users#edit
user_path GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root_path GET / static_pages#home
signup_path GET /signup(.:format) users#new
help_path GET /help(.:format) static_pages#help
about_path GET /about(.:format) static_pages#about
contact_path GET /contact(.:format) static_pages#contact

最佳答案

路线助手

问题是您将 'signup_path' 作为字符串引用:

<%= link_to "Signup", 'signup_path' %>

这里的问题是,当您引用route / path helper时,您正在调用一个方法。 Rails 中的每个助手都是一个方法 - 这意味着如果您引用字符串,系统将无法调用您需要的方法。

要解决此问题,您应该 read up about using route / path helpers in Rails - 并使用 route 的辅助方法:

<%= link_to "Signup", signup_path %>

--

路线

给你一点奖励:

#config/routes.rb
resources :users, path_names: { new: "signup" }
resources :static_pages, only: [] do
collection do
get :help
get :about
get :contact
end
end

您需要使用path_names路由中 users 资源的参数。这将使您的路线更加清洁(DRY)

关于ruby-on-rails - 没有路由匹配获取signup_path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25101390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com