wrong number of arguments (1 for 0) in sunspots - ruby-on-rails

My Controller:
def index
#search = Onj.search do
fulltext params [:search]
end
#onjs = #search.results
logger.debug params
end
index.html.erb:
<%= form_tag onj_index_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Error is:
ArgumentError in OnjController#index
wrong number of arguments (1 for 0)
" def index
#search = Onj.search do
fulltext params [:search]
end
#onjs = #search.results "

Remove the space in params [:search] on line 3 in index method.

Related

Making dynamic search form to stop bloating Controller

I have a search form with 2 ways of searching records, name and email.
<%= form_tag(clients_path, method: :get, class: "d-flex") do %>
<%= text_field_tag(:client_name, params[:client_name], placeholder: "Buscar por nombre", class: "form-control") %>
<%= text_field_tag(:client_email, params[:client_email], placeholder: "Buscar por email", class: "form-control") %>
<%= submit_tag ("Buscar"), class: "btn btn-primary" %>
<% end %>
In my controller
if params[:client_name]
#clients = User.where(name: params[:name])
else
#clients = User.all
end
if params[:client_email]
#clients = User.where(email: params[:client_email])
else
#clients = User.all
end
This works only when searching for 1 condition.
How can I make my controller code dynamic and check if any of conditions are made, show the corresponding results in my #clients variable? I don't want to bloat my controller...
Thanks!
I would change it to:
#clients = User.all
#clients = #clients.where(name: params[:name]) if params[:name]
#clients = #clients.where(email: params[:client_email]) if params[:client_email]

rails faux active record

I am trying to use faux active record (a model that has no persistence in db).
I followed this example:
https://quickleft.com/blog/using-faux-activerecord-models-in-rails-3/
And I have this error:
ArgumentError in SearchController#new
wrong number of arguments (2 for 1)
app/models/search.rb:32:in assign_attributes'
app/models/search.rb:27:ininitialize'
app/controllers/search_controller.rb:4:in new'
app/controllers/search_controller.rb:4:innew'
It seems that when you call Search.new it needs the parametters but my goal is to make it an empty Search object.
Here are my model, controller, view, route:
Model
class Search
include ActiveModel::MassAssignmentSecurity
TRUE_VALUES = ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES
attr_accessor :query
attr_reader :safe_search, :results_per_page
alias_method :safe_search?, :safe_search
attr_accessible :query, :safe_search, :results_per_page
RESULTS_PER_PAGE = [10, 25, 50, 100].freeze
include ActiveModel::Conversion
include ActiveModel::Validations
validates :query, presence: true
validates :results_per_page, presence: true, inclusion: { in: RESULTS_PER_PAGE }
def persisted?
false
end
def initialize(attributes = {})
assign_attributes(attributes)
yield(self) if block_given?
end
def assign_attributes(values, options = {})
sanitize_for_mass_assignment(values, options[:as]).each do |k, v|
send("#{k}=", v)
end
end
def results_per_page=(value)
#results_per_page = value.to_i
end
def safe_search=(value)
#safe_search = TRUE_VALUES.include?(value)
end
end
Controller
class SearchController < ApplicationController
def new
#search = Search.new
render "search/new.html.erb"
end
def create
#search = Search.new(params[:search])
# TODO: use the #search object to perform a search. Adding
# a `results` method on the search object might be a good starting point.
end
end
View: search/new.html.erb
<%= form_for #search do |f| %>
<%= f.label :query %>
<%= f.text_field :query %>
<%= f.label :safe_search %>
<%= f.check_box :safe_search %>
<%= f.label :results_per_page %>
<%= f.select_box :results_per_page %>
<%= end %>
Here is the solution that I found:
In the model I am using this:
def initialize(attributes = {})
assign_attributes(attributes)
end
def assign_attributes(attributes)
sanitize_for_mass_assignment(attributes).each do |k, v|
send("#{k}=", v)
end
end
Here are my new and create methods in the controller:
def new
#search = Search.new
render "search/new.html.erb"
end
def create
#search = Search.new(params[:search])
respond_to do |format|
format.html { render :template => "search/create.html.erb" }
end
end
By the way i had to rename my controller to SearchesController because controllers must be pluralized.
And here is my new.html.erb view:
<%= form_for #search, url: { action: "create" }, :method => :post do |f| %>
<%= f.label :query %>
<%= f.text_field :query %>
<%= f.label :safe_search %>
<%= f.check_box :safe_search %>
<%= f.label :results_per_page %>
<%= f.select(:results_per_page, [10, 25, 50, 100]) %>
<%= f.submit %>
<% end %>
I hope this will be useful to somebody.

First argument in form cannot contain nil or be empty form_for :method=>"get"

I'm getting an error "First argument in form cannot contain nil or be empty" for
<%= form_for #assessment, :method=>"get", :url => url_for(:action => 'new') do |f| %>
I get the error when re-rendering the :options page after an invalid submission. What's wrong with the way I've set this up?
Step 1: User needs to choose a template and a patient_id in Assessment#Options
def options
#assessment = current_user.assessments.new
#patients = current_user.patients.sort_by{|e| e.first_name.downcase}
#patient = current_user.patients.new
#templates = Template.all
end
Assessment#options view:
<%= form_for #assessment, :method=>"get", :url => url_for(:action => 'new') do |f| %>
<% if #assessment.errors.any? %>
<div>
<%= pluralize(#assessment.errors.count, "error") %> prevented the account from being created:
<ul>
<% #assessment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
...
<%= f.submit "Create Assessment", :class => "btn btn-primary", :style=>"width:100%;", :id=>"options-submit" %>
<% end %>
On submit, I GET this action:
def new
if params[:assessment][:template_id].present? && params[:assessment][:patient_id].present?
#assessment = current_user.assessments.new
#assessment.template = Template.find(params[:template_id])
#assessment.patient = current_user.patients.find(params[:patient_id])
else
render :options
end
end
You need #assessment to be instantiated. You can move it outside the condition.
def new
#assessment = current_user.assessments.new
if params[:assessment][:template_id].present? && params[:assessment][:patient_id].present?
#assessment.template = Template.find(params[:template_id])
#assessment.patient = current_user.patients.find(params[:patient_id])
else
render :options
end
end
The line
render :options
does not execute the action options in your controller. It only renders the view using the variables in the new action. As a result none of your instance variables are actually being being set, so your form craps out.
Try moving the stuff in the options method into your if-then loop.

Solr not working Rails

I have this on my view:
<div class='container'>
<div class='row upper_container'>
<div class='search_container'>
<%= form_tag deals_path, :method => :get, :class => 'navbar-form navbar-left' do %>
<div class='form-group'>
<%= text_field_tag :search, params[:search], class: 'form-control' %>
</div>
<%= submit_tag 'Search', :name => nil %>
<% end %>
</div>
</div>
<% #deals.each_with_index do |d, i| %>
<% if i % 3 == 0 %>
<div class='row middle_container'>
<% end %>
<div class='col-md-4'>
<div class='deal_container'>
<%= d.title %>
<img src='<%= d.photo %>', class='deal_img'>
</div>
</div>
<% if (i % 3 == 2) || (i == (#deals.length - 1)) %>
</div>
<% end %>
<% end %>
</div>
this in my controller:
class DealsController < ApplicationController
def index
# #deals = Deal.paginate(:page => params[:page])
#search = Deal.search do
fulltext params[:search]
end
#deals = #search.result
end
private
def deal_params
params.require(:deal).permit(:title)
end
end
and this in my model:
class Deal < ActiveRecord::Base
searchable do
text :title
end
end
when I want to do a seach by some word, like 'Treatment', the #deals variable, in the controller is null, but the param is being sent: Parameters: {"utf8"=>"✓", "search"=>"Treatment"}
any idea?
Try this:
query = params[:search]
#search = Deal.search do
fulltext query
end
#deals = #search.result
Please check this answer for details.

Customize in filename rails issue

I'm trying to customize the format when i'm saving to xls :
I want help customizing= "Date" + "ejecutive selected" + ".xls"
My models
class Policy < ActiveRecord::Base
unloadable
belongs_to :ejecutive
has_many :policy
def self.search(search)
if search
find(:all, :conditions => ["ejecutive_id = ? ", search.to_i ] )
else
find(:all)
end
end
end
class Ejecutive < ActiveRecord::Base
has_many :policies
end
Here is my controller.
Here i put my format that i'm trying to customize with date + ejecutive selected + .xls
class PolicyManagement::PolicyController < ApplicationController
def generate_print_ejecutive_comercial
#ejecutives = Ejecutive.find(:all)
#search = Policy.search(params[:search])
#policies = #search.paginate( :page => params[:page], :per_page =>10)
#results= Policy.search(params[:search])
respond_to do |format|
format.html
format.xls { send_data render_to_string(:partial=>"report_by_ejecutive"), :filename => Date + #ejecutives.name"reporte.xls" }
end
end
Here is my view
<% form_tag :controller=>"policy_management/policy",:action =>"generate_print_ejecutive_comercial", :method => 'get' do %>
<%= select_tag "search", options_for_select(#ejecutives.collect {|t| [t.name.to_s+" "+t.lastname1.to_s,t.id]}) %>
<%= submit_tag "Search", :name => nil %>
<% end %>
Results
<% #policies.each do |policy| %>
<p> <%= policy.num_policy%> </p>
<p> <%= policy.ejecutive.name %> </p>
<p> <%= policy.ejecutive.last_name %> </p>
<% end %>
<%= will_paginate #policies %>
<%= link_to "Export", :controller=>"policy_management/policy",:action=>"generate_print_ejecutive_comercial" ,:format=>"xls",:search => params[:search],:page => params[:page] %>
I tried this
respond_to do |format|
format.html
format.xls { send_data render_to_string(:partial=>"report_by_ejecutive"), :filename => Date + #ejecutive.name + ".xls" }
end
You cannot + something to the Date class or a specific date like Date.today. But the following works.
:filename => "#{Date.today}#{#ejecutive.name}.xls"
"#{something}" evaluates something, calls to_s on the result and inserts its into the string.

Resources