How to transfer a javascript value into a symbol used in Ruby on Rails

Hi,

I am currently using Rails 2.0.2 with Ruby 1.8.7. My Prototoype.js
version is 1.7(if required). I am basically trying to integrate the
calendar_date_select plugin in my app.

The link/tutorial I am referring is:
http://ianli.com/site/HowTo/UseCalendarDateSelectRailsPlugin

I am trying to save the date I receive through the date select plugin
and store in a rails symbol :publishing_date.

I am selecting the date using the calendar_date_select plugin. The
selected date gets uploaded into the text field, but I am not sure how
to transfer that returned value onto my publishing_date
attribute(:publishing_date symbol) of my books table.

On manually entering a date I am able to see it reflected in the DB. I
am getting stuck when I am trying to save it through the plugin.

   <%= f.text_field :publishing_date %>

On clicking on the create button I get a null value for the date
selected via the calendar_date_select plugin. I am able to properly
insert values for name of book and author.

My code for a new book looks like this:


<%= javascript_include_tag :defaults %>


<script

src=’/javascripts/calendar_date_select/calendar_date_select.js’
type=‘text/javascript’>

<h1>New book</h1>

<%= error_messages_for :book %>

<% form_for(@book) do |f| %>
  <p>
    <b>Title</b><br />
    <%= f.text_field :title %>
  </p>

  <p>
    <b>Author</b><br />
    <%= f.text_field :author %>
  </p>


<p>
    <b>Publishing Date</b><br />


 <%=calendar_date_select_tag f.text_field :publishing_date %>
</p>

   <br />
   <br />
   <br />
   <br />

   <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

<%= link_to 'Back', books_path %>

Also once I save it perfectly, could you also please tell me how would I
be able to display this date it in my index.html.erb

My current index.html.erb looks like this:


<h1>Listing books</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Author</th>
    <th>Publishing Date</th>
  </tr>

<% for book in @books %>
  <tr>
    <td><%=h book.title %></td>
    <td><%=h book.author %></td>
    <td><%= book.publishing_date %></td> <!-- Will this work? -->
    <td><%= link_to 'Show', book %></td>
    <td><%= link_to 'Edit', edit_book_path(book) %></td>
    <td><%= link_to 'Destroy', book, :confirm => 'Are you sure?',

:method => :delete %>

<% end %>

<%= will_paginate @books %>

<br />

<%= link_to 'New book', new_book_path %>

An interesting excerpt from the aforementioned tutorial link is given
below:

The value of the selected date can be accessed by getting the value

of the text field named calendar.

Using the Prototype Javascript Library, you can do the following to

get the value.

$F('calendar')

The roadblock I am facing is how to call the $F('publishing_date') in
my new.html.erb file using the rails <%=> tag and then assign that to
my :publishing_date symbol which will eventually be use to update the
publishing_date attribute in my books table.

Thanks for your support.

On Feb 3, 4:36pm, Mohnish G j [email protected] wrote:

The roadblock I am facing is how to call the $F('publishing_date') in
my new.html.erb file using the rails <%=> tag and then assign that to
my :publishing_date symbol which will eventually be use to update the
publishing_date attribute in my books table.

You can’t. Javascript is client side <%= is server side. You don’t
need to though. When the form containing the calendar gets submitted,
there will be a calendar parameter (the first argument passed controls
the name).

Fred

hey,
i would suggest two checks -
-check of published date column to be of the type datetime.

  • check your log files for parameters beimg sent after submitting new
    book form. in your case date shpuld be sent as book[published_date]

i am sure second check should help.

On Friday, February 4, 2011, Mohnish G j [email protected] wrote:

need to though. When the form containing the calendar gets submitted,
create button. It’s taking a NULL value currently and not the value

You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Ratnadeep Deshmane.
http://rtdptech.com

On Feb 4, 1:36am, Mohnish G j [email protected] wrote:

need to though. When the form containing the calendar gets submitted,
create button. It’s taking a NULL value currently and not the value
selected via the plugin date interface.

According to the examples at http://2007.electronicholas.com/calendar,
you can do calendar_date_select_tag :publishing_date

Fred

Frederick C. wrote in post #979492:

On Feb 3, 4:36pm, Mohnish G j [email protected] wrote:

The roadblock I am facing is how to call the $F('publishing_date') in
my new.html.erb file using the rails <%=> tag and then assign that to
my :publishing_date symbol which will eventually be use to update the
publishing_date attribute in my books table.

You can’t. Javascript is client side <%= is server side. You don’t
need to though. When the form containing the calendar gets submitted,
there will be a calendar parameter (the first argument passed controls
the name).

Fred

HI Fred,

If I can’t call the $F('publishing_date'), how else do you think can I
assign the calendar parameter value to the :publishing_date symbol such
that the value is then stored in the database on submitting via the
create button. It’s taking a NULL value currently and not the value
selected via the plugin date interface.

Thanks for your time…

Mohnish

[email protected] wrote in post #979552:

hey,
i would suggest two checks -
-check of published date column to be of the type datetime.

  • check your log files for parameters beimg sent after submitting new
    book form. in your case date shpuld be sent as book[published_date]

i am sure second check should help.

On Friday, February 4, 2011, Mohnish G j [email protected] wrote:

need to though. When the form containing the calendar gets submitted,
create button. It’s taking a NULL value currently and not the value

You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Ratnadeep Deshmane.
http://rtdptech.com

Hi Ratna,

Thanks for your reply,

The publishing_date variable is of type “date” and not “datetime”, I
just observed that the built in attributes(like created_at,updated_at)
are of type “datetime”. Could you please tell me how would this make a
difference or how could it impact the value retrieved from the plugin
which will be eventually stored in the DB.

Frederick C. wrote in post #979561:

On Feb 4, 1:36am, Mohnish G j [email protected] wrote:

need to though. When the form containing the calendar gets submitted,
create button. It’s taking a NULL value currently and not the value
selected via the plugin date interface.

According to the examples at http://2007.electronicholas.com/calendar,
you can do calendar_date_select_tag :publishing_date

Fred

Hi Fred,

it doesn’t work. I have tried it out earlier also.

On Feb 4, 10:05am, Mohnish G j [email protected] wrote:

Fred

Hi Fred,

it doesn’t work. I have tried it out earlier also.

What happens? What do the parameters look like in the log file?

Fred

Hi Fred,

I am really sorry for replying late…

In the parameters wrt the development.log for books doesn’t consider
publishing_date to be as part of books.

Have a look at this:

Processing BooksController#create (for 127.0.0.1 at 2011-02-07 13:39:33)
[POST]
Session ID:
BAh7BzoMY3NyZl9pZCIlZWI5YTUwYzBjY2E1NDdhNGI3OTc2M2YyNWFkZDkw%0AMGIiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhh%0Ac2h7AAY6CkB1c2VkewA%3D–0abacbbecbc5fbb17e945b639dcbbe7f7520532a
Parameters: {“commit”=>“Create”,
“authenticity_token”=>“8265666eb9501a997981c9c35c6ebc296b0d21b7”,
“action”=>“create”, “publishing_date”=>“February 12, 2011”,
“controller”=>“books”, “book”=>{“title”=>“abcd8”, “author”=>“defg8”}}
Book Columns (0.001384) SHOW FIELDS FROM books
SQL (0.000088) BEGIN
Book Create (0.000392) INSERT INTO books (created_at, author,
title, updated_at, publishing_date) VALUES(‘2011-02-07 13:39:33’,
‘defg8’, ‘abcd8’, ‘2011-02-07 13:39:33’, NULL)
SQL (0.001896) COMMIT
Redirected to http://localhost:3000/books/1054583410
Completed in 0.01805 (55 reqs/sec) | DB: 0.00376 (20%) | 302 Found
[http://localhost/books]

I had a question, is it because that the calendar_date_select plugin
takes the date in the format “Month dd, year” and not as of the standard
attributes like created_at which take “year-month-date”, there is a null
being inserted for publishing_date while creating a new books record?

Thanks

Mohnish

On Feb 7, 8:17am, Mohnish G j [email protected] wrote:

[POST]
‘defg8’, ‘abcd8’, ‘2011-02-07 13:39:33’, NULL)
SQL (0.001896) COMMIT
Redirected tohttp://localhost:3000/books/1054583410
Completed in 0.01805 (55 reqs/sec) | DB: 0.00376 (20%) | 302 Found
[http://localhost/books]

I had a question, is it because that the calendar_date_select plugin
takes the date in the format “Month dd, year” and not as of the standard
attributes like created_at which take “year-month-date”, there is a null
being inserted for publishing_date while creating a new books record?

It’s because the publishing_date parameter is being sent at the top
level (ie params[:publishing_date], much as if you’d written
text_field_tag ‘publishing_date’.
Either change the name to ‘book[publishing_date]’ or I seem to recall
there is another form of the helper (analogous to text_field) that
does that for you

Fred

Frederick C. wrote in post #980025:

On Feb 7, 8:17am, Mohnish G j [email protected] wrote:

[POST]
‘defg8’, ‘abcd8’, ‘2011-02-07 13:39:33’, NULL)
SQL (0.001896) COMMIT
Redirected tohttp://localhost:3000/books/1054583410
Completed in 0.01805 (55 reqs/sec) | DB: 0.00376 (20%) | 302 Found
[http://localhost/books]

I had a question, is it because that the calendar_date_select plugin
takes the date in the format “Month dd, year” and not as of the standard
attributes like created_at which take “year-month-date”, there is a null
being inserted for publishing_date while creating a new books record?

It’s because the publishing_date parameter is being sent at the top
level (ie params[:publishing_date], much as if you’d written
text_field_tag ‘publishing_date’.
Either change the name to ‘book[publishing_date]’ or I seem to recall
there is another form of the helper (analogous to text_field) that
does that for you

Fred

Hi Fred,

Good idea:), I didn think of it… but…

I am able to now get publishing_date in my parameters for book by using
‘book[publishing_date]’. But while inserting it inserts a null value
still.

I figured another way to tackle my issue which worked, In the controller
of books , I gave @book.publishing_date = params[:publishing_date], this
would take the publishing_date at the top level and assign it wrt a
particular book in the controller create action/method.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs