【问题标题】:Ransack Search Error: param is missing or the value is empty: profileRansack 搜索错误:参数丢失或值为空:配置文件
【发布时间】:2017-04-11 16:10:34
【问题描述】:

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

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

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

非常感谢任何帮助。

【问题讨论】:

  • 您的搜索表单操作是创建操作而不是索引操作。您在索引中有条目搜索方法,因此请使用 get 方法搜索表单而不是发布。
  • @rails_id - 谢谢。这消除了错误。我没有意识到我通过放置 post 来调用配置文件控制器创建方法。将其替换为获取。还有一些问题,我将在下面发布,但至少这是解决的第一步。

标签: ruby-on-rails ruby search ransack


【解决方案1】:

试试这样的:

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

【讨论】:

  • 我从用户“rails_id”那里收到了与该问题相关的回答。似乎我在 search_form_for 方法中放置了 POST 而不是 GET。但是我也尝试了您的代码,并且效果很好。你能告诉我这两种方法是否有区别吗?你的看起来更紧凑? 2 之间的最大区别在于您的方法中没有指定该方法。所以我猜是因为它引用了 @search 对象,这足以让它知道引用 Profiles 控制器中的索引操作?
【解决方案2】:

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

【讨论】:

  • 实际的列是位置,这是正确的,但似乎 Ransack 需要 _cont 谓词来告诉 Ransack 搜索“包含”某些值的列。 cont 用于让 Ransack 确定看似匹配的信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多