ruby-on-rails - 向路由添加新操作

标签 ruby-on-rails ruby routes

我在用户 Controller 中得到了这些 Action

class UsersController < ApplicationController
  def index #default action
    ...
  end

  def new #default action
    ...
  end

  def another_new
    ...
  end

  def create
    ...
  end

  def another_create
    ...
  end
end

我希望能够 /users/another_new 并从某种链接调用 :method => :another_create 使 /users/another_new

我得到了以下 config/routes.rb

get '/users/another_new' :to => 'users#another_new'
resources :users

我的问题是这是否是添加 get 的正确方法以及我如何添加 another_create 方法。

最佳答案

在你的 config/routes.rb 文件中这样做

resources :users do
  collection do
    get 'another_new'
    post 'another_create'
  end
end

也看看HERE以便清楚地理解概念。

希望这对你有帮助老兄:)

关于ruby-on-rails - 向路由添加新操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13498981/

相关文章:

ruby-on-rails - 测试期间 nil 类的未定义方法 '[]'

ruby-on-rails - 将@连接到rails形式的字符串

sql - 从 ActiveRecord 获取排名

ruby-on-rails - gem install pg 在执行 bundle install/RoR 时不工作

ruby-on-rails - 尝试在 rails 中使用 Gmail API 进行身份验证时,获取没有路由匹配 [GET] "/auth/google_oauth2"错误

ruby-on-rails - 缺少注册/创建、应用/创建的模板

ruby-on-rails - 除了在 Capybara 中使用 sleep 之外,还有更好的选择吗?

ruby-on-rails - 在模型 : how do I include helper dependencies? 中使用助手

ruby-on-rails - Rails 没有路由匹配 PUT,但 rake 路由显示它存在

javascript - JavaScript 中的高级路由