Rails 中的路由与 Controller/action/id 路由有问题
Routing in Rails has problems with Controller/action/id route
在我的 Rails 4 应用程序中,我通过 PagesController 创建页面以形成菜单。该菜单包含指向相应控制器的 redirect_to 索引操作的链接。该页面模型的列是 :id、:title、:access_level、:html(实际上是指控制器)、:icon
示例数据为:5, Employees, 3, employees, users
注意:"resources :employees" 等不相关的代码部分不可见!
路线文件:
resources :pages
resources :access
页面控制器:
def show
@pages = Page.find(params[:id])
redirect_to controller: "#{@page.html}", action: "index"
end
阻止生成链接
<% @pages = Page.all %>
<% @pages.each do |page| %>
<li>
<%= link_to({ controller: "pages", action: "show", id: "#{page.id}" }) do %><i class="fa fa-<%= page.icon %> fa-fw"></i> <%= page.title %>
<% end %>
</li>
<% end %>
HTML 根据例子生成link_to:
<a href="/pages/5"><i class="fa fa-users fa-fw"></i> Employees</a>
除了我尝试实现基本登录系统但出现以下错误外,一切正常:
ActionController::UrlGenerationError in Access#index
Showing C:/Users/Samsung/Documents/rails/AjansTakip/app/views/access/index.html.erb where line #9 raised:
No route matches {:action=>"attempt_login", :controller=>"access"}
Index.html.erb 访问视图的第 9 行
<%= form_for :access, url: {action: "attempt_login"} do |f| %>
AccessController(:id_number是identification_number的缩写,与:id无关)
def attempt_login
if params[:id_number].present? && params[:password].present?
found_user = Employee.where(id_number: params[:id_number]).first
if found_user
authorized_user = found_user.authenticate(params[:password])
end
end
if authorized_user
redirect_to controller: 'pages', action: 'index'
else
redirect_to action: 'login'
end
end
我正在关注 Lynda Rails 4 Essential Training,我无法通过基本登录系统实施的第一部分,因此请不要评论除路由以外的任何编码部分,因为我是尝试自学。
就解决方案而言,我尝试了这个解决方案,它可以将错误传递给另一个解决方案
get 'access/attempt_login', to: 'access#attempt_login'
但我很确定有更好的方法来处理这类情况。
我在 index.html.erb:
上通过 post 提交表单时出现另一个错误
No route matches [POST] "/access/attempt_login"
我认为问题的出现是因为默认路由是 :controller(/:id(/:action)) 并且控制器期望 :id 在 :action 之前。对于不需要 :id.
的所有类型的操作,我认为路由 "get" 毫无意义
我使用的另一个解决方案是将默认路由更改为“:controller(/:action(/:id))”,但这导致的问题比它解决的问题更多(我们甚至可以说它没有解决任何问题根本)。甚至菜单中的链接也会中断,因为 link_to :action "show" 没有被解析为 href="pages/show/5"。我还尝试将链接生成为 link_to({ controller: "pages", action: "show", id: "show/#{page.id}" }) 但这并没有我想要的那样实用和动态。
请指出正确的方向。我也愿意改变 Paging 与 PagesController 的工作方式,我想不出更好的方法来将页面存储在数据库中并将它们全部重定向到一个地方。
谢谢
是的,不要使用默认控制器的东西,那是老派。相反,试试这个:
删除获取行:
get 'access#attempt_login', to: 'access#attempt_login'
现在,既然你想把它放在资源集合上,试试这个:
resources :access do
collection do
get :attempt_login
end
end
然后在你的表单中:
<%= form_tag attempt_login_access_path, method: :get do |f| %>
当涉及到不用于创建或更新操作的 url 时,我使用 form_tag。 Form_for 当您有模特在场时很好。 get 方法也很重要,因为表单默认为 post。您可能需要仔细检查该路由路径:
bundle exec rake routes | grep attempt
并确保方法正确。
在我的 Rails 4 应用程序中,我通过 PagesController 创建页面以形成菜单。该菜单包含指向相应控制器的 redirect_to 索引操作的链接。该页面模型的列是 :id、:title、:access_level、:html(实际上是指控制器)、:icon
示例数据为:5, Employees, 3, employees, users
注意:"resources :employees" 等不相关的代码部分不可见!
路线文件:
resources :pages
resources :access
页面控制器:
def show
@pages = Page.find(params[:id])
redirect_to controller: "#{@page.html}", action: "index"
end
阻止生成链接
<% @pages = Page.all %>
<% @pages.each do |page| %>
<li>
<%= link_to({ controller: "pages", action: "show", id: "#{page.id}" }) do %><i class="fa fa-<%= page.icon %> fa-fw"></i> <%= page.title %>
<% end %>
</li>
<% end %>
HTML 根据例子生成link_to:
<a href="/pages/5"><i class="fa fa-users fa-fw"></i> Employees</a>
除了我尝试实现基本登录系统但出现以下错误外,一切正常:
ActionController::UrlGenerationError in Access#index
Showing C:/Users/Samsung/Documents/rails/AjansTakip/app/views/access/index.html.erb where line #9 raised:
No route matches {:action=>"attempt_login", :controller=>"access"}
Index.html.erb 访问视图的第 9 行
<%= form_for :access, url: {action: "attempt_login"} do |f| %>
AccessController(:id_number是identification_number的缩写,与:id无关)
def attempt_login
if params[:id_number].present? && params[:password].present?
found_user = Employee.where(id_number: params[:id_number]).first
if found_user
authorized_user = found_user.authenticate(params[:password])
end
end
if authorized_user
redirect_to controller: 'pages', action: 'index'
else
redirect_to action: 'login'
end
end
我正在关注 Lynda Rails 4 Essential Training,我无法通过基本登录系统实施的第一部分,因此请不要评论除路由以外的任何编码部分,因为我是尝试自学。
就解决方案而言,我尝试了这个解决方案,它可以将错误传递给另一个解决方案
get 'access/attempt_login', to: 'access#attempt_login'
但我很确定有更好的方法来处理这类情况。 我在 index.html.erb:
上通过 post 提交表单时出现另一个错误No route matches [POST] "/access/attempt_login"
我认为问题的出现是因为默认路由是 :controller(/:id(/:action)) 并且控制器期望 :id 在 :action 之前。对于不需要 :id.
的所有类型的操作,我认为路由 "get" 毫无意义我使用的另一个解决方案是将默认路由更改为“:controller(/:action(/:id))”,但这导致的问题比它解决的问题更多(我们甚至可以说它没有解决任何问题根本)。甚至菜单中的链接也会中断,因为 link_to :action "show" 没有被解析为 href="pages/show/5"。我还尝试将链接生成为 link_to({ controller: "pages", action: "show", id: "show/#{page.id}" }) 但这并没有我想要的那样实用和动态。
请指出正确的方向。我也愿意改变 Paging 与 PagesController 的工作方式,我想不出更好的方法来将页面存储在数据库中并将它们全部重定向到一个地方。
谢谢
是的,不要使用默认控制器的东西,那是老派。相反,试试这个:
删除获取行:
get 'access#attempt_login', to: 'access#attempt_login'
现在,既然你想把它放在资源集合上,试试这个:
resources :access do
collection do
get :attempt_login
end
end
然后在你的表单中:
<%= form_tag attempt_login_access_path, method: :get do |f| %>
当涉及到不用于创建或更新操作的 url 时,我使用 form_tag。 Form_for 当您有模特在场时很好。 get 方法也很重要,因为表单默认为 post。您可能需要仔细检查该路由路径:
bundle exec rake routes | grep attempt
并确保方法正确。