Rails路由指向两个发布路由到同一个控制器

时间:2018-04-23 14:55:26

标签: ruby-on-rails ajax

我正在尝试使用'post'创建两个路由,因此我可以使用AJAX。我有两种不同的形式,每种形式都指向Userselections Controller中的不同方法。

这两条路线指向两种不同的方法:选择和创建。两人似乎互相打破了。

如何将路线中的两个帖子指向同一个控制器?

路线

# --- User selections routes ---

  get '/user_selections', to: "userselections#select", as: :user_selections
  post '/user_selections', to: "userselections#select", as: :user_selection_category

  post '/user_selections', to: "userselections#create", as: :new_user_selections

  delete '/user_selections/:id', to: "userselections#destroy", as: :destroy_user_selections

Userselections控制器

class UserselectionsController < ApplicationController
  skip_before_action :authenticate_user!, only: [:select, :create, :destroy]

  def select
    @user_selection = UserSelection.new
    @user_selections = UserSelection.all
    @companies = Company.all

    @categories = @companies.map {|z| p z.category}
    @unique_categories = @categories.uniq{|x| p x}

    @test = params[:test]

    policy_scope(Company)
    if params[:query].present?
      @companies = Company.search_by_name_and_category(params[:query])
      authorize @companies
    else
      @companies = Company.all
      authorize @companies
    end

    @selection_array = []
    if @user_selections.present?
      @user_selections.each do |selection|
        @selection_array << selection
      end
    end
  end

  def new
    authorize current_user
    policy_scope(UserSelection)

    @user_selection = UserSelection.new

    user_signed_in? ? @user_selection.user_id = current_user.id : @user_selection.user_id = session[:guest_user_id]
    @user_selection.company_id = params["user_selection"]["company_id"].to_i
    @user_selection.save
  end

  def create
    # Create new user selection
    @companies = Company.all
    @user_selection = UserSelection.new(user_selection_params)
    user_signed_in? ? @user_selection.user_id = current_user.id : @user_selection.user_id = session[:guest_user_id]
    @selection_array = []
    user_signed_in? ? user_selections = UserSelection.where(user_id: current_user.id) : user_selections = UserSelection.where(user_id: session[:guest_user_id])
    if @user_selections.present?
      @user_selections.each do |selection|
        @selection_array << selection
      end
    end

    # Create a list of unselected companies and enable AJAX
    @unselected_companies = []
    @companies.each do |company|
      if @selection_array.include?(company)
        @unselected_companies << company
      end
    end

    authorize @user_selection
    if @user_selection.save
      respond_to do |format|
        format.html { redirect_to select_path }
        format.js
      end
    else
      respond_to do |format|
        format.html { render 'userselections/select'}
        format.js
      end
    end
  end

  def destroy
    @user_selection = UserSelection.find(params[:id])
    @user_selection.destroy
    authorize @user_selection
    policy_scope(UserSelection)
  end

  private

  def user_selection_params
    params.require(:user_selection).permit(:company_id, :test)
  end
end

1 个答案:

答案 0 :(得分:1)

您有两条相同的路线,均为POST:post '/user_selections'

路线的这一部分必须是唯一的。

您可以将两条不同的路线发布到同一个控制器,没问题,但路线部分 - 最终在浏览器(或JS)的URL中的部分必须是唯一的。< / p>

如果你有类似的东西:

 post '/user_selections', to: "userselections#select", as: :user_selection_category
 create '/user_selections', to: "userselections#create", as: :new_user_selections

这样就好了,因为一个是帖子而另一个是创建。

请注意,在创建操作上使用create是标准的。

否则重命名URL部分,例如:

 post '/user_selections/select', to: "userselections#select", as: :user_selection_category
 post '/user_selections', to: "userselections#create", as: :new_user_selections

因为一个名为'/user_selections/select'而另一个名为'/user_selections'