从 select_tag 保存数据

发布于 2024-11-16 19:06:42 字数 2131 浏览 3 评论 0原文

尝试将数据从表单保存到数据库。 使用 select_tag

<%= select_tag :size, options_from_collection_for_select(@plan, 'name', 'size') %>

一切都很好,它同时获取 size 和 email ,但是当我尝试存储表单(size)中的数据时,它传递了 NULL。

这是我的控制台:

Started POST "/users" for 127.0.0.1 at 2011-06-24 07:25:29 -0500
Processing by UserController#create as HTML
Parameters: {"utf8"=>"✓",   "authenticity_token"=>"MfT3gs5TtR+bvpaLro0E8Qm1zojaY2ms9WK0WprKPAw=", "size"=>"small",
"user"=>{"email"=>"[email protected]"}, "commit"=>"Create User"}
AREL (0.4ms)  INSERT INTO "users" ("email", "size", "created_at", "updated_at") VALUES
 ('[email protected]', NULL, '2011-06-24 12:25:29.646814', '2011-06-24 12:25:29.646814')
Redirected to http://localhost:3000/users/14
Completed 302 Found in 56ms

所以,它从表单中获取正确的数据,如您所见“size”=>“small”,但是当需要存储它时,它将它作为 NULL 传递,

 VALUES ('[email protected]', NULL, '2011-06-24

我想,这是 select_tag,因为它没有附加 u,就像 text_field 一样

<%= form_for @user do |u| %>
                    <%= render 'shared/error_messages' %>
                        <p><%= u.label :size, 'How many employees do you have?' %>: </p>
                        <p><%= select_tag :size, options_from_collection_for_select(@plan, 'name', 'size') %></p>

                        <p><%= u.label :email, 'What\'s your email address?' %>:</p>
                        <p><%= u.text_field :email %></p>
                        <%= u.submit%>
                    <% end %>

但是当我尝试 u.select_tag = 错误时,未定义方法。

我的模型

  class User < ActiveRecord::Base
attr_accessible :size, :email
end

有什么想法吗?

Trying to save data from a form to the database.
Using the select_tag

<%= select_tag :size, options_from_collection_for_select(@plan, 'name', 'size') %>

Everything is fine, it grabs both size and email ,but when I try to store the data from the form (size), it passes NULL.

Here's my console:

Started POST "/users" for 127.0.0.1 at 2011-06-24 07:25:29 -0500
Processing by UserController#create as HTML
Parameters: {"utf8"=>"✓",   "authenticity_token"=>"MfT3gs5TtR+bvpaLro0E8Qm1zojaY2ms9WK0WprKPAw=", "size"=>"small",
"user"=>{"email"=>"[email protected]"}, "commit"=>"Create User"}
AREL (0.4ms)  INSERT INTO "users" ("email", "size", "created_at", "updated_at") VALUES
 ('[email protected]', NULL, '2011-06-24 12:25:29.646814', '2011-06-24 12:25:29.646814')
Redirected to http://localhost:3000/users/14
Completed 302 Found in 56ms

So, It gets the correct data from the form, as you see "size"=>"small", but when its time to store it, it passes it as NULL,

 VALUES ('[email protected]', NULL, '2011-06-24

I thought, it was the select_tag, as it doesnt have u attached, as text_field does

<%= form_for @user do |u| %>
                    <%= render 'shared/error_messages' %>
                        <p><%= u.label :size, 'How many employees do you have?' %>: </p>
                        <p><%= select_tag :size, options_from_collection_for_select(@plan, 'name', 'size') %></p>

                        <p><%= u.label :email, 'What\'s your email address?' %>:</p>
                        <p><%= u.text_field :email %></p>
                        <%= u.submit%>
                    <% end %>

But when I tried u.select_tag = Error, undefined method.

My model

  class User < ActiveRecord::Base
attr_accessible :size, :email
end

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2024-11-23 19:06:42

您需要将“size”参数嵌套在“users”哈希中。当您查看日志时,您想要验证是否看到类似这样的内容:

"user"=>{"email"=>"[email protected]", "size"=>"small"}

要在表单内部实现这一点,您可以保留现有的 select_tag 并将其范围设置为这样:

<%= select_tag 'user[size]', options_from_collection_for_select(@plan, 'name', 'size') %>

或者对于这种情况,您似乎可以使用 collection_select 范围在表单对象上:

<%= u.collection_select :size, @plan, :name, :size %>

You need to have the "size" param nested inside of the "users" hash. When you look in the log you want to verify seeing something like this:

"user"=>{"email"=>"[email protected]", "size"=>"small"}

To achieve that inside of of your form for, you can keep your existing select_tag and scope it as such:

<%= select_tag 'user[size]', options_from_collection_for_select(@plan, 'name', 'size') %>

Or you for this case it looks like you could use collection_select scoped on the form object:

<%= u.collection_select :size, @plan, :name, :size %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文