简单表单:从另一个控制器创建新记录

时间:2022-05-13 10:41:11

I have a Home controller with an index view that acts like a search box that lets the user query a First Table or Second Table through a select box. After a user enters a search term, he will be redirected to the index page of the First or Second model with the search results for that model.

我有一个带有索引视图的Home控制器,其作用类似于一个搜索框,允许用户通过选择框查询First Table或Second Table。在用户输入搜索词后,他将被重定向到第一个或第二个模型的索引页面以及该模型的搜索结果。

A Search record must be created each time a query is submitted with the search type and search term. However, I don't know how to create a new Search object using simple_form from a different controller, which is the Home controller in this case.

每次使用搜索类型和搜索词提交查询时,都必须创建搜索记录。但是,我不知道如何使用来自不同控制器的simple_form创建一个新的Search对象,在这种情况下是Home控制器。

Home Controller

家庭控制器

def index
  @find = Find.new

Home Index View

主页索引视图

= simple_form_for @find, url: finds_path, method: :post do |f|
  = f.input :search_type, :collection => [['First', 'First'], ['Second', 'Second']]
  = f.input :search_term
  = f.button :submit

Finds Controller

找到控制器

def new
  @find = Find.new
end

def create
  @find = Find.new(find_params)
  if params[:search_type] == 'First'
    redirect_to first_path
  elsif params[:search_type] == 'Second'
    redirect_to second_path
  else
    redirect_to root_path
  end
end

private

def find_params
  params.permit(:search_term, :search_type, :utf8, :authenticity_token, 
    :find, :commit, :locale)
  # the params seem to come from the Home controller so I added them just to see if they will go through :(
end

It doesn't save. Instead it gives:

它没有保存。相反,它给出:

Started POST "/en/finds"
Processing by FindsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"..", "find"=>{"search_type"=>"First", "search_term"=>"Something"}, "commit"=>"Create Find", "locale"=>"en"}
Unpermitted parameter: :find
Redirected to http://localhost:3000/en

2 个解决方案

#1


1  

Unpermitted parameter: :find

未允许的参数:: find

Your find_params should be just

你的find_params应该是公正的

def find_params
  params.require(:find).permit(:search_type, :search_term)
end

You should access the search_type with params[:find][:search_type]

您应该使用params [:find] [:search_type]访问search_type

if params[:find][:search_type] == 'First'
  redirect_to first_path
elsif params[:find][:search_type] == 'Second'
  redirect_to second_path
  else
  redirect_to root_path
end

Also, I would suggest renaming the Find model as it conflicts with ActiveRecord#FinderMethods

另外,我建议重命名Find模型,因为它与ActiveRecord#FinderMethods冲突

#2


0  

You need to save, you are just initializing the attributes..

你需要保存,你只是初始化属性..

@find = Find.new(find_params)
@find.save!

OR

要么

@find = Find.create!(find_params)

Also, strong parameters should be

此外,强大的参数应该是

def find_params
  params.require(:find).permit(:search_term, :search_type)
end

#1


1  

Unpermitted parameter: :find

未允许的参数:: find

Your find_params should be just

你的find_params应该是公正的

def find_params
  params.require(:find).permit(:search_type, :search_term)
end

You should access the search_type with params[:find][:search_type]

您应该使用params [:find] [:search_type]访问search_type

if params[:find][:search_type] == 'First'
  redirect_to first_path
elsif params[:find][:search_type] == 'Second'
  redirect_to second_path
  else
  redirect_to root_path
end

Also, I would suggest renaming the Find model as it conflicts with ActiveRecord#FinderMethods

另外,我建议重命名Find模型,因为它与ActiveRecord#FinderMethods冲突

#2


0  

You need to save, you are just initializing the attributes..

你需要保存,你只是初始化属性..

@find = Find.new(find_params)
@find.save!

OR

要么

@find = Find.create!(find_params)

Also, strong parameters should be

此外,强大的参数应该是

def find_params
  params.require(:find).permit(:search_term, :search_type)
end