【问题标题】:How to add route to custom controller methods如何将路由添加到自定义控制器方法
【发布时间】:2014-04-01 07:31:58
【问题描述】:

如何向 Rails 中的自定义控制器方法添加路由?

我有以下方法,想通过 AJAX 调用它们。

def w_destroy
    render json: RHoliday.where(holiday_id: params[:holiday_id].to_s, group_id: params[:group_id].to_s).destroy 
end
  def w_create

        @r_holiday = RHoliday.new(r_holiday_params)

        respond_to do |format|
       if @r_holiday.save
         format.html { redirect_to @r_holiday, notice: 'RHoliday was successfully created.' }
         format.json { render action: 'show', status: :created, location: @r_holiday }
       else
         format.html { render action: 'new' }              format.json { render json: @r_holiday.errors, status: :unprocessable_entity }
       end
     end
   end 

问题是: 我该怎么做呢?我想我需要编辑我的 routes.rb 但我不知道要在那里添加什么。我刚刚使用了resources :r_holidays,但这只是创建了默认路由。

【问题讨论】:

    标签: ruby-on-rails activerecord routes


    【解决方案1】:

    您可以通过多种方式add extra routes out of resources scope

    #config/routes.rb
    resources :r_holidays do
        post :w_create
        delete :w_delete
    end
    

    具体来说,resources 调用基本上为您提供了 Rails 的标准 7 条 RESTful 路由 - 您可以根据需要使用代码块来定义额外的路由。您应该read the documentation 获得更多想法

    【讨论】:

    • 谢谢。正是我需要的!
    • 没问题!现在给我一些Unicoins!
    • 哈哈,这是愚人节的玩笑,请查看网站的右上角;)
    • 啊...Hillarious...以为是某种狗狗币假货 :D 我如何发送它们?
    • 我认为你必须找到彩虹,从 iTunes 商店下载 unicoins 应用程序,然后给我一些小妖精会给你的硬币 :)
    猜你喜欢
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2021-06-11
    • 2021-02-06
    • 1970-01-01
    • 2022-10-14
    相关资源
    最近更新 更多