如何在Rails中定义自定义路径? [英] How to define a custom path in rails?

查看:64
本文介绍了如何在Rails中定义自定义路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户模型。如果我这样做:

I have a User model. If I do:

def my_action
  @user = User.new
end

然后

  <% form_for(@user) do |f| %>

我知道了

undefined method `users_path' for #<ActionView::Base:0x1b4b878>

这是有道理的,因为我没有将其映射到 map.resources:用户。 ..但我不想这样做,因为我不需要所有资源。

Which make sense because I haven't mapped it going map.resources :users... but I don't want to do it this way because I don't need all the resources.

我怎么能只需在我的路线中定义此 user_path 方法?

How can I just define this user_path method in my routes?

推荐答案

自定义静态路由。例如,在我的应用程序中,只有index和show动作适用于某些控制器。在我的routes.rb文件中,我有一些这样的路由:

You can also customize restful routes. For example in my application only the index and show actions are appropriate for certain controllers. In my routes.rb file I have some routes like this:

map.resources :announcements, :only => [:index, :show]

您也可以使用:except 如果更合适。

You can also use :except if that's more appropriate.

这篇关于如何在Rails中定义自定义路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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