Ransack Search Error: param is missing or the value is empty: profile

Ransack Search Error: param is missing or the value is empty: profile

我在 rails 上对 ruby 很陌生,所以我遇到的问题在我的代码中可能看起来很明显,但对我来说却不是。

我在我的应用程序中使用 Ransack 搜索 gem。我想按 'location' 搜索个人资料,但由于某种原因,我在搜索时收到以下错误:

ActionController::ParameterMissing in ProfilesController#create
param is missing or the value is empty: profile

 def profile_params
   params.require(:profile).permit(:full_name, :contact_number, :location, :makeup_type, :bio, :user_id, :image)
 end 

我查过这个错误,但我似乎无法弄清楚是什么原因造成的。所有需要的字段都在那里。

我的配置文件控制器:

class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]

 def index
   @search = Profile.search(params[:q])
   @profiles = @search.result(distinct: true)
 end

 def show
   @profile = Profile.find(params[:id])
 end

 def new
   @profile = Profile.new  
 end

 def create
   @profile = Profile.new(profile_params)

   respond_to do |format|
     if @profile.save
       format.html { redirect_to @profile, notice: 'Your Profile was successfully created' }
       format.json { render :show, status: :created, location: @profile }
     else
       format.html { render :new }
       format.json { render json: @profile.errors, status: :unprocessable_entry }
     end
   end   
 end

 def edit
   @profile = Profile.find(params[:id])
 end

 def update

  respond_to do |format|  
    if @profile.update(profile_params)
      format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
      format.json { render :show, status: :ok, location: @profile }
    else
      format.html { render :edit }
      format.json { render json: @profile.errors, status: :unprocessable_entity }
    end
  end
 end

 def destroy
   @profile.destroy

   respond_to do |format|
     format.html { redirect_to profile_url, notice: 'Profile was successfully destroyed.' }
     format.json { head :no_content }
   end
 end

 def set_profile
   @profile = Profile.find(params[:id])
   #@profile = Profile.find(profile_params)
 end

 private
   def profile_params
     params.require(:profile).permit(:full_name, :contact_number, :location, :makeup_type, :bio, :user_id, :image)
   end    
end

我的 index.html.erb 配置文件

<h1>Profiles#index</h1>

 <%= search_form_for @search, url: profiles_path, html: { method: :post, :class => 'course-finder-form' } do |f| %>
   <%= f.text_field :location_cont %>
   <%= f.submit "Search" %>
 <% end %>

我的路线(我不确定这是否与问题有任何关系,或者我是否缺少路线 - 同样我不是 100% 但见下文):

Rails.application.routes.draw do

 resources :profiles 
 root to: 'pages#index'
 devise_for :users, :controllers => { :registrations => "registrations" }
end

我的架构:

ActiveRecord::Schema.define(version: 20161126221219) do

 create_table "profiles", force: :cascade do |t|
   t.string   "full_name"
   t.string   "contact_number"
   t.string   "location"
   t.string   "makeup_type"
   t.string   "bio"
   t.datetime "created_at",     null: false
   t.datetime "updated_at",     null: false
   t.integer  "user_id"
   t.string   "image"
 end

如果您需要查看我的模型,请告诉我。目前,一个用户有一个个人资料,一个个人资料属于一个用户,但这种关系似乎没有发挥应有的作用。无论如何,这是一个单独的问题,但希望为您提供尽可能多的背景知识。

非常感谢任何帮助。

我认为你的 index.html.erb 有错别字 您的模型中有任何地方有 :location_cont 吗?还是 :location

尝试这样的事情:

 #profile controller
  def index
    @search = Profile.search(params[:q])
    @profiles = @search.result(distinct: true)
  end

  #profile index
  <%= search_form_for @search do |f| %>
    <%= f.label :location_cont, "location in profile" %><br>
    <%= f.submit "Search", class:"btn btn-info btn-block" %>
  <% end %>

  <% @profiles.each do |profile| %>
    <%= profile.name %>
  <% end %>