更新后如何申请没有ID的原始路径?

时间:2015-06-09 18:48:22

标签: ruby-on-rails

我正在使用Rails 4.2和Ruby 2.1.5

这是我的路线档案:

Rails.application.routes.draw do
  root to: "services#index"  
  resources :apis, only: [:new, :create]
  resources :commons, path: "/self-care2/commonController" do
      collection do
         post :search, to: "commons#search"
      end
  end
end

这是我的commons控制器的创建动作:

def create
  @api = Api.new(api_params)
  if @api.save
    flash[:info] = request.original_url + ".do?apiname=" + "#{@api.name}"
    redirect_to root_path
  else 
    @api.statuses.new
    render :new
  end
end

这是我的commons控制器的更新操作:

def update
  @api = Api.find(params[:id])
  if @api.update(api_params)
    flash[:info] = request.original_url + ".do?apiname=" + "#{@api.name}"
    redirect_to root_path
  else 
    @api.statuses.new
    render :edit
  end
end

创建新记录后,它会向我发送一个网址:

 http://localhost:3000/self-care2/commonController.do?apiname=loginCSP

在我更新之后,我也得到了一个闪光灯:

 http://localhost:3000/self-care2/commonController/58.do?apiname=loginCSP

有什么办法可以在我更新后摆脱对象ID“/ 58”吗?

我想要它创建和更新两者都返回相同的网址。

2 个答案:

答案 0 :(得分:1)

使用网址助手

,而不是使用request.original_url
flash[:info] = commons_url + ".do?apiname=#{@api.name}"

答案 1 :(得分:0)

您可以使用以下更新功能:

org_url = request.original_url
flash[:info] = org_url[0, org_url.rindex("/")] + ".do?apiname=" + "#{@api.name}"
相关问题