禁止属性错误参数红宝石搜索表单

时间:2015-03-13 20:19:43

标签: ruby-on-rails ruby-on-rails-4

我有一个问题。我正在以表格形式创建搜索表单。表格中有餐馆名称,城市和州。

看起来像这样:

<%= form_tag(restaurants_path, :method => "get", id: "search-form") do %>
    <%= label_tag(:restaurant_name) %> 
    <%= text_field_tag :restaurant_name, params[:restaurant_name]  %> <br />
    <%= label_tag(:city, "Restaurant City") %> 
    <%= text_field_tag :city, params[:city] %> <br />
    <%= label_tag(:state, "Restaurant State") %>
    <%= text_field_tag :state, params[:state] %> <br />
    <%= submit_tag "Search", :name => nil %>
<% end %>

现在我正在尝试获取部分餐馆名称/位置的信息。

我已经看到,如果我的表单编码正确,您可以执行以下操作:

Restaurant Controller:
class RestaurantsController < ApplicationController
  before_action :set_restaurant, only: [:show, :edit, :update, :destroy, :search]

  # GET /restaurants
  # GET /restaurants.json
  def index
    constraints = Hash.new
    constraints[:restaurant_name] = params[:restaurant_name] 
    constraints[:city] = params[:city]

    if params[:restaurant_name] && params[:city]
    #@restaurants = Restaurant.search(params[:restaurant_name]).order('restaurant_name desc').paginate(:per_page => 5, :page => params[:page])
        @restaurants = Restaurant.where(params).order('restaurant_name desc').paginate(:per_page => 5, :page => params[:page])
    else
        @restaurants  = Restaurant.order('restaurant_name desc').paginate(:per_page => 5, :page => params[:page])

    end
  end

  # GET /restaurants/1
  # GET /restaurants/1.json
  def show
  end

  # GET /restaurants/new
  def new
    @restaurant = Restaurant.new
  end

  # GET /restaurants/1/edit
  def edit
  end

  # POST /restaurants
  # POST /restaurants.json
  def create
    @restaurant = Restaurant.new(restaurant_params)

    respond_to do |format|
      if @restaurant.save!
    session[:page] = 0
        format.html { redirect_to restaurants_url, notice: 'Restaurant was successfully created.' }
        format.json { head :no_content }
      else
        format.html { render :new }
        format.json { render json: @restaurant.errors, status: :unprocessable_entity }
      end
    end
  end
  # GET /restaurants/1/search
  # GET /restaurants/1.json
  def gotosearch



  end

 def self.search

        Restaurant.where("restaurant_name like ? AND city like ? ", params[:restaurant_name], params[:city]) 


    end

  # PATCH/PUT /restaurants/1
  # PATCH/PUT /restaurants/1.json
  def update
    respond_to do |format|
      if @restaurant.update(restaurant_params)
        format.html { redirect_to @restaurant, notice: 'Restaurant was successfully updated.' }
        format.json { render :show, status: :ok, location: @restaurant }
      else
        format.html { render :edit }
        format.json { render json: @restaurant.errors, status: :unprocessable_entity }
      end
    end
  end



  # DELETE /restaurants/1
  # DELETE /restaurants/1.json
  def destroy
    @restaurant.destroy
    respond_to do |format|
      format.html { redirect_to restaurants_url, notice: 'Restaurant was successfully destroyed.' }
      format.json { head :no_content }
    end
  end




  private
    # Use callbacks to share common setup or constraints between actions.
    def set_restaurant
      @restaurant = Restaurant.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def restaurant_params
      params.require(:restaurant).permit(:restaurant_name, :street, :city, :state, :zip, :split_the_check, :dont_split_the_check)
    end

end

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

@restaurants = Restaurant.where(params).order('restaurant_name desc').paginate(:per_page => 5, :page => params[:page])

您正在使用名为params的变量进行搜索,该变量包含请求的所有参数。你需要做一些更具体的事情:

@restaurants = Restaurant.where(params[:restaurant_name]).order('restaurant_name desc').paginate(:per_page => 5, :page => params[:page])

或者你可以通过约束[:restaurant_name]进行搜索,因为你已经宣布了这个变量。

如果您根据多个属性进行搜索,则可以执行以下操作:

@restaurants = Restautanr.where("restaurant_name = ? OR name = ? OR state = ?", params[:restaurant_name], params[:city], params[:state])