2016-11-28 49 views
0

我對軌道上的ruby非常陌生,所以我的問題可能在我的代碼中看起來很明顯,但對我來說似乎很明顯。Ransack搜索錯誤:param丟失或值爲空:配置文件

我在我的應用程序中使用了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 

我的檔案

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

我的路線index.html.erb(我不知道這是否會是任何關係的問題,或者如果我失去了路線 - 我又不是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 

如果您需要查看我的模型,請告訴我。目前,一個用戶具有一個配置文件,而一個配置文件屬於一個用戶,但該關係看起來不像應該那樣工作。無論如何,這是一個單獨的問題,但想給你儘可能多的背景。

任何幫助將非常感激。

+0

搜索表單動作是創建行動,而不是指數的行動。您在索引中使用了條目搜索方法,所以使用'get'方法搜索表單而不是發佈。 –

+0

@rails_id - 謝謝。這消除了錯誤。我沒有意識到我正在通過發佈帖子來調用配置文件控制器創建方法。用Get代替它。還有幾個問題,我將在下面發佈,但至少這是解決的第一步。 – marg08

回答

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

我收到了有關該問題的用戶'rails_id'的上述答案。貌似我已經在search_form_for方法中放置了一個POST而不是GET。但是我也嘗試了你的代碼,它的工作完美。你能告訴我這兩種方法是否有區別?你的外表更緊湊? 2的最大區別在於該方法未在您的指定中。所以我猜是因爲它引用了@search對象,這足以讓它知道引用Profiles控制器中的索引操作? – marg08

0

我認爲您的拼寫錯誤index.html.erb 您的模型中的任何位置是否有:location_cont?或者是它:location

+0

實際的列是位置,這是正確的,但看起來Ransack需要_cont謂詞來告訴Ransack搜索一個'包含'某個值的列。 cont用於允許Ransack確定哪些信息看似匹配。 – marg08

相關問題