date_select -> params ok but nil

58 views
Skip to first unread message

tom

unread,
Nov 19, 2014, 5:40:50 PM11/19/14
to Ruby on Rails: Talk
hi,

im kinda stuck...

<%= form_for(@event) do |f| %>
  <p>
    <%= f.label :from_date %><br />
    <%= date_select :event, :from_date %>
  </p>


.....

def create
p params[:event].nil?
p params[:event][:name]
p params[:event][:from_date]

    if params[:event][:from_date].empty?
      params[:event][:from_date] = Date.today
    end
.......
-->
Started POST "/events" for 127.0.0.1 at 2014-11-19 17:37:09 -0500
Processing by EventsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"JannWb1XCE12AKW4KlR56LAAmIOG1raHJh1ss4v0RBA=", "event"=>{"name"=>"", "from_date(1i)"=>"2014", "from_date(2i)"=>"11", "from_date(3i)"=>"19", "to_date(1i)"=>"2014", "to_date(2i)"=>"11", "to_date(3i)"=>"19"}, "commit"=>"Create"}
..........
..............................def instantiate_controller_and_action_names"
"Fullpath: /events"
"events"
"Action: create"
"Controller: events"
false
""
nil


--> why are my event date attributes nil/blank?

tia
tom

Hassan Schroeder

unread,
Nov 19, 2014, 6:10:09 PM11/19/14
to rubyonrails-talk
On Wed, Nov 19, 2014 at 2:40 PM, tom <toma...@gmail.com> wrote:

> Started POST "/events" for 127.0.0.1 at 2014-11-19 17:37:09 -0500
> Processing by EventsController#create as HTML
> Parameters: {"utf8"=>"✓",
> "authenticity_token"=>"JannWb1XCE12AKW4KlR56LAAmIOG1raHJh1ss4v0RBA=",
> "event"=>{"name"=>"", "from_date(1i)"=>"2014", "from_date(2i)"=>"11",
> "from_date(3i)"=>"19", "to_date(1i)"=>"2014", "to_date(2i)"=>"11",
> "to_date(3i)"=>"19"}, "commit"=>"Create"}

> --> why are my event date attributes nil/blank?

What makes you think they are? The parameters are there.

What version of Ruby/Rails are you running?

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

tom

unread,
Nov 19, 2014, 9:20:41 PM11/19/14
to Ruby on Rails: Talk
hi

because of this:
def create
p params[:event].nil?
p params[:event][:name]
p params[:event][:from_date]

results in:
false
""
nil

ENV
/ruby-1.9.3-p194@my_gemset


first i thought it has something to do with gem decent_exposure, but i have disabled that again...





--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCAvR9N6Udei6Ky5HchS%2Bpa7LW0vQ4_d1UszoM%2BTSaSrg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

botp

unread,
Nov 19, 2014, 10:19:36 PM11/19/14
to rubyonra...@googlegroups.com
On Thu, Nov 20, 2014 at 6:40 AM, tom <tomabr
> --> why are my event date attributes nil/blank?

i dont see "from_date".

"from_date(1i)" not same as "from_date"

kind regards -botp

Hassan Schroeder

unread,
Nov 19, 2014, 11:58:21 PM11/19/14
to rubyonrails-talk
On Wed, Nov 19, 2014 at 6:20 PM, tom <toma...@gmail.com> wrote:

> because of this:

> p params[:event].nil?
> ....

p params.inspect would be more generally useful...

> ENV
> /ruby-1.9.3-p194@my_gemset

>> What version of Ruby/Rails are you running?

Again, what version of Rails?

And is there a reason to be using such an old Ruby?

Mike

unread,
Nov 20, 2014, 3:17:44 AM11/20/14
to rubyonra...@googlegroups.com
You're using the f.label and date_select helpers.  Try using f.date_select ?

Matt Jones

unread,
Nov 20, 2014, 11:30:48 AM11/20/14
to rubyonra...@googlegroups.com


On Wednesday, 19 November 2014 17:40:50 UTC-5, der_tom wrote:
hi,

im kinda stuck...

<%= form_for(@event) do |f| %>
  <p>
    <%= f.label :from_date %><br />
    <%= date_select :event, :from_date %>
  </p>


.....

def create
p params[:event].nil?
p params[:event][:name]
p params[:event][:from_date]

    if params[:event][:from_date].empty?
      params[:event][:from_date] = Date.today
    end
.......
-->
Started POST "/events" for 127.0.0.1 at 2014-11-19 17:37:09 -0500
Processing by EventsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"JannWb1XCE12AKW4KlR56LAAmIOG1raHJh1ss4v0RBA=", "event"=>{"name"=>"", "from_date(1i)"=>"2014", "from_date(2i)"=>"11", "from_date(3i)"=>"19", "to_date(1i)"=>"2014", "to_date(2i)"=>"11", "to_date(3i)"=>"19"}, "commit"=>"Create"}


 --> why are my event date attributes nil/blank?

The date is not sent as a single attribute, but rather one per dropdown that date_select sends (thus from_date(1i) etc).

When those attributes are assigned to your model, they are combined into a single object by ActiveRecord.

You can either check for the subparts explicitly, or move the "default to today" behavior to the model instead.

--Matt Jones

tom

unread,
Nov 21, 2014, 9:22:46 AM11/21/14
to Ruby on Rails: Talk
hi

thx for all.

the reason why i asked is because some code i have used from the internet was checking: params[:event][:from_date].empty?  , but mine was always nil (as above). after assigning the params to a new model and checking then fixed it for me.




--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages