Search code examples
ruby-on-railsrubysearchransack

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


I'm very new to ruby on rails so the issue I have may seem obvious from my code but not to me.

I am using the Ransack search gem in my application. I want to search a profile by 'location' but for some reason when I search I receive the following error:

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 

I have looked up this error and I can't seem to figure out what might be causing it. All the fields are there that need to be.

My Profiles Controller:

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

My index.html.erb for Profiles

<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 %>

My routes (I'm not sure if this would be of any relation to the issue or if I'm missing routes - again I'm not 100% but see below):

Rails.application.routes.draw do

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

My schema:

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

If you need to see my models let me know. Currently one user has one profile and a profile belongs to a user but that relationship doesn't appear to be working as it should. Anyway, that's a separate issue to this but wanted to give you as much background as possible.

Any help would be very much appreciated.


Solution

  • Try something like this:

     #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 %>