我有一个isuue。我正在创建一个搜索表单。表格中有餐厅名称,城市和州。禁止的属性错误参数红宝石搜索表单
是这样的:
<%= 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
什么版本的Rails的是你的应用程序,你可以发表你的restaurants_controller? – 2015-03-13 20:25:18
4.我会马上发布它。 – user2958278 2015-03-13 20:31:41
我第一次没有约束hash – user2958278 2015-03-13 20:33:36