0

My Web app has two date_select parameters in. These should be passed to the DB for searching later on. However I cannot get this to function. You see I can see from the inspection that the parameters being passed are date(3i) (2i) (1i). When I create my migrate file with my tables in it fails if I try to add (3i) etc.. I've read that in rails we need to manipulate this in the controller. I'm trying but failing miserably. I would have thought that this is basic attribute of a DB table. dates etc..

Any Pointers? What code do you need to see.

Here are the parameters I'm passing...

 "date(3i)"=>"19",
 "date(2i)"=>"4",
 "date(1i)"=>"2014",
 "date_of_change(3i)"=>"19",
 "date_of_change(2i)"=>"4",
 "date_of_change(1i)"=>"2014",


 class CreatePosts < ActiveRecord::Migration
 def change
create_table :posts do |t|
  t.text :title
  t.text :requester
  t.text :requester_email
  t.text :customer
  t.text :contact
  t.text :customer_email
  t.text :customer_phone
  t.string :type_of_change


  t.timestamps

OK seem to be getting further here.... so on the rails console I can see the following date has been passed but the date_of_change is still nil.

  => #<Post id: 7, title: "", requester: "", requester_email: "", customer: "", contact:     
 customer_email: "", customer_phone: "", type_of_change: "Service Change", created_at: 

 "2014-04-19 21:32:53", updated_at: "2014-04-19 21:32:53", implementer: "", ticket: "", 

 date: "2014-04-19", date_of_change: nil> 

OK. I have a typo on my permitted values. let me change that.

user3216968
  • 85
  • 1
  • 6

1 Answers1

1

Say your Model is Concert, your migration file should look something like this. Does it?

class CreateConcerts < ActiveRecord::Migration
  def change
    create_table :concerts do |t|
      t.string :artist
      t.string :venue
      t.date :date       #correct format for date
      t.date :date_of_change

      t.timestamps
    end
  end
end

So in your case I believe it should be

 class CreatePosts < ActiveRecord::Migration
 def change
    create_table :posts do |t|
       t.text :title
       t.text :requester
       t.text :requester_email
       t.text :customer
       t.text :contact
       t.text :customer_email
       t.text :customer_phone
       t.string :type_of_change
       t.date :date
       t.date :date_of_change

       t.timestamps
       end
    end
  end

then run rake db:migrate

In your Post controller at the bottom it should be

def post_params
      params.require(:post).permit(:title, :requester, :requester_email, :customer, :contact, :customer_email, :customer_phone, :type_of_change, :date, :date_of_change)
end
parameter
  • 894
  • 2
  • 18
  • 35
  • It does, with the exception of the t.date :date line. Is this what I need do you think? are the **'s required? – user3216968 Apr 19 '14 at 20:52
  • no sorry the ** was an error I was trying to bold it. but yes you would need something like this for date and date_of_change. I edited my answer for you @user3216968 – parameter Apr 19 '14 at 20:57
  • can you post the migration file for your model that you're trying to do this with? I should also add that this works for postgresql, not sure about sqlite – parameter Apr 19 '14 at 21:08
  • as above. No date info in there – user3216968 Apr 19 '14 at 21:13
  • edited my answer, let me know if that works now when you try to create a Post – parameter Apr 19 '14 at 21:16
  • You may also need to add the date and date_of_change parameters to your Post controller – parameter Apr 19 '14 at 21:18
  • I'll try that now. can you add an example of what to add to the post controller. – user3216968 Apr 19 '14 at 21:19
  • Good Grief That's it. I have read so much stuff about multiple attributes and putting stuff in the controller to make the values an integer etc... I'll save this because in 90% of web apps I'm goign to be passing a date for sure. Many Thanks for your help. I'll mark it as answered. – user3216968 Apr 19 '14 at 21:38
  • no worries, I'm new to rails applications too and had some similar issues as well when it came to passing/extracting dates but when it comes to just creating them in the database this should be all you need. glad I could help! – parameter Apr 19 '14 at 21:42