如何将 javascript 值传输到 Ruby on Rails 中使用的符号
我目前正在使用 Rails 2.0.2 和 Ruby 1.8.7。我的 Prototoype.js 版本是 1.7(如果需要)。我基本上试图将 calendar_date_select 插件集成到我的应用程序中。
我指的链接/教程是: http://ianli.com/site/HowTo/UseCalendarDateSelectRailsPlugin
我试图保存通过日期选择插件收到的日期并将其存储在 Rails 符号:publishing_date 中。
我正在使用 calendar_date_select 插件选择日期。所选日期被上传到文本字段中,但我不确定如何将返回值传输到图书表的出版日期属性(:出版日期符号)上。
手动输入日期后,我可以看到它反映在数据库中。当我尝试通过插件保存它时,我遇到了困难。
<%= f.text_field :publishing_date %>
单击创建按钮后,我通过 calendar_date_select 插件选择的日期得到一个空值。我能够正确插入书名和作者的值。
我的新书代码如下所示:
<%= javascript_include_tag :defaults %>
<script src='/javascripts/calendar_date_select/calendar_date_select.js' type='text/javascript'></script>
<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 %> <!--This way of save/assigning doesn't work -->
</p>
<br />
<br />
<br />
<br />
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', books_path %>
另外,一旦我完美保存它,您能否告诉我如何在我的index.html.erb中显示此日期我
当前的index.html.erb看起来像这样:
<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 %></td>
</tr>
<% end %>
</table>
<%= will_paginate @books %>
<br />
<%= link_to 'New book', new_book_path %>
下面给出了上述教程链接的有趣摘录:
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')
我面临的障碍是如何使用 Rails 在我的 new.html.erb 文件中调用
标记,然后将其分配给我的 :publishing_date 符号,该符号最终将用于更新我的 books 表中的Publishing_date 属性。$F('publishing_date')
><%=>
感谢您的支持。
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'></script>
<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 %> <!--This way of save/assigning doesn't work -->
</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 %></td>
</tr>
<% end %>
</table>
<%= 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是如何让你的插件做你想做的事情(使用 javascript,就像教程说的那样)
摘要:
非常复杂,实际上不是 Rails 代码。需要 javascript 知识,并使用这些知识编写您自己的自定义表单,而不是使用预先生成的 Rails 表单。这可能不是您正在寻找的,但它看起来像是插件实际期望的。调试可能很复杂(需要查看 html 源代码、日志中命中的路由等)
流程:
您的插件可以让您执行以下操作:(
根据您链接到的教程)。
那是 Rails 代码。所有 Rails erb 代码所做的就是为您提供一种快速、简单的方法来生成真正的 html...例如,链接
生成的 html 代码如下所示:
这是您的浏览器实际呈现的内容。到目前为止,一切都很好?
Rails 所做的就是获取 erb 文件,并将其逐行转换为 html,以便网页可以显示。这对你来说意味着,当你的插件实际运行时(在浏览器中),已经没有任何 Rails 代码了。所以你不能将任何 JavaScript 变量返回给你的 Rails 代码,除非你通过路由传递它。
Rails 通常如何处理表单,是否生成正确的 html 标签,以及 html 本身处理控制表单。即使您在 .erb 文件中设置了 Rails 变量,这些变量实际上并不能帮助数据进入您的 Rails 应用程序。相反,当您使用表单时,您应该(在日志中)看到类似以下内容:
在您的控制器中(在上述情况下的sign_in,看起来可能是为您创建的?),您可以通过说 params[: 来访问这些发布的参数: user][:email] 或其他任何内容,无论您如何传递它们。
也就是说,是的,在您的表单中您可以输入诸如 :title 和 :publishing date 之类的内容,但这些只是设置您传递给 Rails 的参数,而不是 Rails 控制器中的实际符号。您可以轻松地使用命令行工具(例如curl)来传递相同的内容,而无需rails生成的网页。
这基本上就是 javascript 解决方案的工作原理。
该教程说您可以执行以下操作:
%>
然后编写一个 javascript 函数(不是 Rails),如下所示:
上面的脚本采用您在日历上选择的任何日期,并将您发送到具有该日期的谷歌页面,如行所示:
这实际上非常接近我们想要的去做。不同之处在于我们希望它将位置设置为您的创建图书路线,并将图书的参数 POST(不像位置那样 GET)到该路线。一旦我们这样做了,rails 就会处理它,就像它是它自己创建的表单一样。
您熟悉宁静的路线吗? (更重要的是,您正在使用它们吗?) location.href (如示例中所示)执行 GET,而我们需要 POST 来使用正确的信息来创建路由。
我不太熟悉标准 javascript 的工作原理,但我的大多数 POST 都使用一个名为“JQuery”的库(您可以将其包含在 erb 文件的顶部,例如:
Jquery 让我将代码更改为如下所示) :
请记住,这绝不是执行 javascript 帖子的唯一方法,这只是我的做法。此外,这假设您希望像教程一样采用 yy/mm/dd 格式的发布日期(如果您不这样做,创建 pdate 不同)。
因此,最后一个应该让您将publishing_date 提供给负责创建书籍的 Rails 控制器操作!如果您希望它看起来完全像rails,您可以通过 params[:publishing_date] 访问它。你可能必须像这样嵌套它:
这样它看起来就像从表单到rails的东西一样
如果你想在那里获取表单参数的其余部分,你可能不得不不使用rails auto。 - 创建工具,也可以使用 javascript 构建表单的其余部分,那里有一些非常好的教程,但本质上,当您发布该帖子时,您希望将其更改为类似:
或其他内容,并且具有。 var book_name 可以使用类似的方法以相同的方法设置:
其中 form_name 是表单的名称(通过查看文件源很容易找到),field_name 是字段的名称(相同的处理)。
对于我的一些 Rails 代码,它生成的内容如下:
实际上没有表单的名称(可能有一些 Rails 方法来设置它),但字段的名称将是“video[name]”(不确定 javascript 如何喜欢括号...但只有一种方法可以找出问题,如果 javascript 确实有问题,您可以随时手动编写表单 html,并将所有标签名称设置为 javascript 可以处理的内容。不需要提交按钮,因为 javascript 会为您完成提交。
Here's how to make your plugin do what you want (with javascript, like the tutorial says)
Summary:
Very complicated, not actually rails code. Requires knowledge of javascript, and use of this knowledge to write your own custom form, rather than using pre-generated Rails forms. This might not be what you're looking for, but it looks like what the plugin actually expects. Probably complicated to debug (would need to look at html source code, what routes are getting hit in the logs, etc)
Process:
Your plugin lets you do something like:
(according to the tutorial you linked to).
That is rails code. All any rails erb code does is give you a quick, simple way to generate real html... A link to, for example,
produces html code that looks like this:
Which is what your browser actually renders. So far so good?
All rails does is take your erb file, and line by line convert it to html, which a web page can display. What this means for you, is that by the time your plugin is actually running (which is in the browser), there is NO rails code left. So you can't GIVE any javascript variables back up to your rails code UNLESS you pass it in through a route.
How rails normally handles forms, is it makes the proper html tags, and html itself handles controlling the form. Even though you set rails variables in the .erb file, those variables don't actually help data go to your rails app. Instead, when you use your form, you should see (in your logs), something like:
In your controller (sign_in in the above case, looks like maybe create for yours?), you could access these posted parameters by saying params[:user][:email], or whatever NO MATTER HOW YOU PASSED THEM IN.
That is, yes, in your form you say things like :title, and :publishing date, but those just set the params you pass in to rails, not the actual symbol in your rails controller. You could just as easily use a command line tool (like curl) to pass the same things in, without a rails-generated webpage.
WHICH, is basically how the javascript solution works.
The tutorial says you can do things like:
%>
And then write a javascript function (NOT rails) like:
The above script takes whatever date you chose on your calendar, and sends you to a google page with that date, as shown in the line:
This is ACTUALLY really close to what we want to do. The difference is we want it to set the location to your create book route, and POST (not GET like location does) the book's parameters to that route. Once we do that, rails will handle it just as if it was a form it had created itself.
Are you familiar with restful routes at all? (and more importantly, are you using them?) location.href (as in the example) does a GET, while we need a POST to hit the create route with the right information.
I'm not too familiar with how standard javascript does things, but I use a library called "JQuery" for most of my POSTs (which you can include at the top of your erb file like:
Jquery lets me change the code to look like:
Keep in mind this is by no means the only way to do javascript posts, it's just how I do them. Also, this assumes you want your publishing date in yy/mm/dd format like the tutorial (if you don't, create pdate differently).
So, that last should let you give publishing_date to your Rails controller action responsible for creating books. Tada! You can access it through params[:publishing_date] if you want. If you want it to look EXACTLY like rails, you'd probably have to nest it something like:
So that it looks just like something that came from a form to rails.
If you want to get the rest of your form params in there, you'll probably have to not use rails auto-creation tools, and instead build the rest of your form with javascript as well. There are some pretty good tutorials out there, but essentially, when you do that post, you want to change it to looke something like:
or whatever, and have the var book_name be set in the same method by using something like:
Where form_name is the name of the form (easily found out by viewing the source of the file) and field_name is the name of the field (same deal).
For some of my rails code it's generating something that looks like:
Which actually doesn't have a name for the form (there's probably some rails way to set this), but the name of the field would be "video[name]" (not sure how javascript would like the brackets...but only one way to find out. If javascript DOES have a problem, you can always write the form html by hand, and make all the tag names something javascript can deal with. You wouldn't need a submit button, because the javascript does the submitting for you.
至于“如何显示”,你是对的
,如果它被设置的话,它会很好地工作。您说您可以使用文本字段将其设置得很好,对吧(即您的迁移是正确的)?这样做,然后查看您的索引页应该可以确认这一点。
另外,根据我的经验,javascript vars 不能传递给rails(尽管rails vars 可以传递给javascript)。因此,您永远无法将 $F('publishing_date') 放入您的 erb 文件中。当任何 javascript 变量被设置时,它不再是一个 Ruby 程序,它只是一个网页......你的插件必须有一个默认的方式来处理事情,我有点惊讶它不能正常工作框... Hrrm...等等...
我实际上浏览了您链接到的教程,我想我看到了您的问题:
您的插件实际上并不是一个表单助手(与“form_for 一起使用)。
如果它是一个表单助手(并且会根据需要设置表单变量),您可以使用以下命令来调用它:
<% f.calendar_select_tag %>
这将使其成为传递到表单中的参数。它只是为您生成一个日历,然后您可以编写自己的 javascript 来处理它(如教程中的“函数changeLocation(日期)”),
如果您希望此界面实际工作,则 它与表单无关。使用表单,您必须使用 html/javascript 制作表单,而不是使用 ruby 辅助标记(这更复杂)。
您需要有一个包含“$F('publishing_date') 的帖子。 " 在参数中,以及您想要设置的任何其他 javascript 变量,然后通过 javascript 使用这些 post 变量手动点击您的创建路由......这并不完全有趣。
编辑:
它可能会与类似的东西一起工作:
或其他(一种不同的标记方式,而不是您使用的“form_for”)
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
无论哪种方式,至少,你调用它的方式:
完全错误,因为您在其中包含“f.text_field”,这可能是一个拼写错误?它怎么可能是一个选择标签和一个文本字段?谁知道呢,也许只是摆脱它会有所帮助(尽管它仍然不与表单关联,除非您执行 <% form_tag%>
As far as "How to display", you are correct that
will work just fine, IF it gets set. You say you can set it just fine with a text field, right (i.e. your migrations are correct)? Doing that, then looking at your index page should confirm this.
Also, from my experience, javascript vars can't be passed to rails (though rails vars can be passed to javascript). So, you would never be able to get $F('publishing_date') into your erb file. By the time any javascript vars are set, it's not a Ruby program anymore, it's just a web page... There MUST be a default way for your plugin to handle things, and I'm a little surprised it's not working out of the box... Hrrm...wait...
I just actually glanced over the tutorial you linked to, and I THINK I see your problem:
Your plugin is not ACTUALLY a form helper (that works with "form_for).
If it WAS a form helper (and would set your form variable like you want), you'd call it with:
<% f.calendar_select_tag %>
Which would make it be a parameter passed into your form. In this case, it seems like it's just generating a calendar for you, which you can then program your own javascript to handle (like "function changeLocation(date)" in the tutorial). It has nothing at ALL to do with forms.
If you want this interface to actually work with a form, you'll have to make the form in html/javascript and NOT with the ruby helper tags. (which is more complicated).
You'd need to have a post, which contains "$F('publishing_date')" in the parameters, as well as any other javascript variables you'd want to set, and hit your create route manually, through javascript, with those post variables.... Not exactly fun.
Edit:
It's possible it MIGHT work with something like:
or whatever (a different way to do tags, as opposed to the "form_for" you were using)
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
Either way, at the very least, the way you were calling it:
is completely wrong, since you include "f.text_field" in there, which might be a typo? How can it be a select tag AND a text field? Who knows, maybe just getting rid of that will help (though it's still not associated with a form unless you do <% form_tag%>