私は、ユーザが選択ボックスを通して第1テーブルまたは第2テーブルをクエリできる検索ボックスのように機能するインデックスビューを持つホームコントローラを持っています。ユーザーが検索語を入力すると、そのモデルの検索結果とともに、第1または第2モデルの索引ページにリダイレクトされます。シンプルフォーム:別のコントローラから新しいレコードを作成する
検索レコードは、検索タイプと検索語句を付けてクエリを送信するたびに作成する必要があります。しかし、この場合はHomeコントローラである別のコントローラからsimple_formを使用して新しいSearchオブジェクトを作成する方法はわかりません。
ホームコントローラー
def index
@find = Find.new
ホーム索引ビュー
= 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
それは保存されませんコントローラー
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
を検索します。代わりに、それは与える:
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
あなたの完全な 'finds_cntroller'を投稿してください – Pavan