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

[...] <div id="find_dates">   <% form_tag '/find_dates' do %>     <p>       <%= label_tag "start_date", "Start Date" %>       <%= date_select "find_dates", "start_date", :order => [:month, :day, :year] %>     </p>     <p>       <%= label_tag "end_date", "End Date" %>       <%= date_select "find_dates", "end_date", :order => [:month, :day, :year] %>     </p>     <%= submit_tag "Find" %>   <% end %> </div> [...]

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)

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

Colin

How would I go about testing the parameter?

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?

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

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].

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?

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

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.

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

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'

1 Like