How do I set defaults for view parameters?

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

[…]

<% form_tag '/find_dates' do %>

<%= 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 %>
[...]

On 22 March 2010 16:34, Civ2boss [email protected] wrote:

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)

Test params[:find_dates] before you use it and take appropriate action
if nil.

Colin

params[:find_dates].nil?

Thanks that works! I was trying params[:find_dates] == nil,
params[:find_dates].blank?, params[:find_dates] == " ", but obviously
none of them worked.

Here’s an sort of off-topic question how would I go about getting
format.csv { render :csv => @logs } to work? Rails gives a method not
found error.
I did use the routes.rb file to give that admin page a direct link,
but even if I use the full “controller/action” url, it still says not
found.
Do I need to give that a direct link in the routes.rb file also or am
I missing something?

On 23 March 2010 15:04, Civ2boss [email protected] wrote:

Thanks that works! I was trying params[:find_dates] == nil,

params[:find_dates] == nil is the same as params[:find_dates].nil?

Colin

How would I go about testing the parameter?

On 23 March 2010 15:20, Robin T. [email protected] wrote:

Well that’s strange then, because it still gave me a unexpected nil
found error when I use that. Actually now that I think about it, I
might have been using it on params[:find_dates][‘start_date(1i)’]
instead of just the params[:find_dates].

That would explain it, params[:find_dates][‘start_date(1i)’] == nil
would generate an error if params[:find_dates] is nil because it would
attempt to evaluate nil.[‘start_date(1i)’] first.

Colin

Yeah thanks for the help!

By the way do you know how I can get the “format.csv { render :csv =>
@logs }” part to work? I have tested it on an index page and that
works, but when I try it on this admin page it says it doesn’t
understand the URL. I have set a direct path to the admin page instead
of the standard “controller/action” path in the routes.rb file, but
even if i go through the “controller/action” path it still says it
can’t find it. Is it because of that direct path in the routes.rb that
it’s giving me the error?

On 23 March 2010 16:03, Robin T. [email protected] wrote:

Yeah thanks for the help!

By the way do you know how I can get the “format.csv { render :csv =>
@logs }” part to work? I have tested it on an index page and that
works, but when I try it on this admin page it says it doesn’t
understand the URL. I have set a direct path to the admin page instead
of the standard “controller/action” path in the routes.rb file, but
even if i go through the “controller/action” path it still says it
can’t find it. Is it because of that direct path in the routes.rb that
it’s giving me the error?

Have you got the .:format bit in the route? Otherwise I would suggest
a new thread for this question as routing is not my high point.

Colin

Well that’s strange then, because it still gave me a unexpected nil
found error when I use that. Actually now that I think about it, I
might have been using it on params[:find_dates][‘start_date(1i)’]
instead of just the params[:find_dates].

I don’t, but I figure there must be a way to just have it in the
controllers. Oh well thanks, I’ll start another thread.

On 23 March 2010 16:31, Robin T. [email protected] wrote:

I don’t, but I figure there must be a way to just have it in the
controllers. Oh well thanks, I’ll start another thread.

You don’t what? It is best to insert your reply at the appropriate
point in the comment so that it is easier to follow the thread. If
you mean that you don’t have the .:format in the route where you are
using some_url.csv then that is the problem.

Colin

On Tue, Mar 23, 2010 at 12:43 PM, Colin L. [email protected]
wrote:

On 23 March 2010 16:31, Robin T. [email protected] wrote:

I don’t, but I figure there must be a way to just have it in the
controllers. Oh well thanks, I’ll start another thread.

You don’t what? It is best to insert your reply at the appropriate
point in the comment so that it is easier to follow the thread. If
you mean that you don’t have the .:format in the route where you are
using some_url.csv then that is the problem.

hmm yeah that’s what I meant. Thanks I think I figured it out.

This is the code I will use in the routes.rb file:
map.admin ‘admin.:format’, :controller => ‘user_sessions’, :action =>
‘admin’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs