Ransack搜索错误:param丢失或值为空:profile

时间:2016-11-28 01:10:30

标签: ruby-on-rails ruby search ransack

我对轨道上的红宝石很新,所以我的问题看起来很明显,但我的代码却没有。

我在我的应用程序中使用Ransack搜索gem。我想通过“地点”搜索个人资料。但由于某种原因,当我搜索时,我收到以下错误:

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

如果您需要查看我的模型,请告诉我。目前,一个用户有一个配置文件,一个配置文件属于一个用户,但该关系似乎没有按预期工作。无论如何,这是一个单独的问题,但希望给你尽可能多的背景。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

尝试这样的事情:

 #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 %>
相关问题