So I am making a rails app that lets users search logs by date. So in
my controller I have set it to look for logs from the DB using the
parameters. However when it first loads, there are no parameters
passed to the controller, so it gives a nil error.
Is there a way to set the default parameters so that it won’t have
this error?
Here are the controller and view snippets:
user_sessions_controller.rb
[…]
def admin
@start_date = Date::civil(params[:find_dates]
[‘start_date(1i)’].to_i, params[:find_dates][‘start_date(2i)’].to_i,
params[:find_dates][‘start_date(3i)’].to_i)
@end_date = Date::civil(params[:find_dates]["end_date(1i)"].to_i,
params[:find_dates][“end_date(2i)”].to_i, params[:find_dates]
[“end_date(3i)”].to_i)
@logs = Log.all(:conditions => ["updated_at between ? and ?",
@start_date, @end_date], :order => “updated_at DESC”)
respond_to do |format|
format.html { render :action => 'admin' }
format.csv { render :csv => @logs }
end
end
[…]
admin.html.erb
[…]
<%= label_tag "start_date", "Start Date" %> <%= date_select "find_dates", "start_date", :order => [:month, :day, :year] %>
<%= label_tag "end_date", "End Date" %> <%= date_select "find_dates", "end_date", :order => [:month, :day, :year] %>
<%= submit_tag "Find" %> <% end %>