0

I have a join table Writing between tables User and Exercise. My model is

class Writing < ActiveRecord::Base
    belongs_to :user
    belongs_to :exercise
    attr_accessible :writing_date, :exercise_id
end

I followed this Guide for The has_many :through Association

and my table born in

class CreateWritings < ActiveRecord::Migration
  def self.up
    create_table :writings do |t|
      t.integer :user_id
      t.integer :exercise_id
      t.date :writing_date
      t.timestamps
    end
  end
  def self.down
    drop_table :writings
  end
end

my writings_controller is:

 # encoding: utf-8
 class WritingsController < ApplicationController
    def create
        @writing = current_user.writings.build(:exercise_id => params[:exercise_id], :writing_date => params[:writing_date])
       if @writing.save
            flash[:notice] = "added writing."
            redirect_to root_url
       else
            flash[:error] = "not added."
            redirect_to root_url
       end
    end
    def new
       @writing = current_user.writings.new
       @exercise = Exercise.all
       respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @writing }
       end
    end
end

and my _form.html.erb is like

 <%= form_for(@writing) do |f| %>
  <div class="field">
   <%= f.text_field :writing_date %>
  </div>
  <div class="actions">
    <%= f.submit "add a writing", :class => "awesome custom red" %>
  </div>
 <% end %>

The problem? The data is not saving from writing_date to database. I am using jQuery and datepicker. I followed the logs

Started POST "/writings" for 127.0.0.1 at 2012-05-19 19:55:23 +0300
  Processing by WritingsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"q3xXMxVd9PsI346ph9CILLm8x74uCFokz3/2yOA0CeI=", "exercise_id"=>"7", "writing"=>{"writing_date"=>"2012-05-19"}, "commit"=>"add a writing"}

everything ok but ...

AREL (0.3ms)  INSERT INTO "writings" ("user_id", "exercise_id", "writing_date", "created_at", "updated_at") VALUES (1, 7, NULL, '2012-05-19 16:55:23.719410', '2012-05-19 16:55:23.719410')

writing_date is null.

mhatch
  • 4,441
  • 6
  • 36
  • 62

2 Answers2

2

I think you need to configure datepicker to another format. For example:

$(".datepicker").datepicker({
            dateFormat : "dd MM yy"
});

Ruby date formats can be found here: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html And related question: Rails 3.1 text_field form helper and jQuery datepicker date format

Community
  • 1
  • 1
Mik
  • 4,117
  • 1
  • 25
  • 32
  • thanks for reply, I have already configure my file public/javascripts/application.js like this "dateFormat: "yy-mm-dd". I change to "dateFormat: "dd MM yy" but with no luck. – Panagiotis Petridis May 19 '12 at 18:10
  • Ok, sorry :) And now can you try this: `@writing = current_user.writings.build(:exercise_id => params[:exercise_id], :writing_date => params[:writing][:writing_date])` ? Because in your params you have: `"writing"=>{"writing_date"=>"2012-05-19"}` – Mik May 19 '12 at 18:17
0

As per your parameters, "writing"=>{"writing_date"=>"2012-05-19"}, "writing_date" is under the "writing" hash, but you are passing the params[:writing_data] parameter only; that's why it is not working.

@writing = current_user.writings.build(:exercise_id => params[:exercise_id], :writing_date => params[:writing_date])

instead of params[:writing_date] you should use params[:writing][:writing_date]

So you have to change it in the following way:

@writing = current_user.writings.build(:exercise_id => params[:exercise_id], :writing_date => params[:writing][:writing_date])
Asherah
  • 18,948
  • 5
  • 53
  • 72
Sandip Mondal
  • 921
  • 7
  • 12