rubyonrailsin

A Ruby and Rails talk

Monday, March 22, 2010


Re: [Rails] How do I set defaults for view parameters?

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

On 22 March 2010 16:34, Civ2boss <civ2boss@gmail.com> wrote:
> 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

>
>    @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>
> [...]
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive