RoR:Mongoid和表单创建哈希 [英] RoR : Mongoid and form create hash
问题描述
Rails专家的简单问题.为什么我必须在我的VideosController的以下创建方法中使用以下语句插入新的Mongoid文档:params [:video] [:description]?为什么我不能使用POST表单中的params [:description]?如果使用它,该值将变为nil.
Simple question for Rails gurus. Why I do have to use the following statement to insert a new Mongoid document : params[:video][:description] in the following create method of my VideosController? Why I can't use the params[:description] from the POST form? If I use it, the value becomes nil.
def create
@video = Video.new(
:title => params[:video][:title],
:description => params[:video][:description]
)
if @video.save
render 'success'
else
render 'error'
end
end
这是Video.rb类:
Here is the Video.rb class :
class Video
include Mongoid::Document
field :title, type: String
field :description, type: String
validates_presence_of :title
validates_presence_of :description
acts_as_url :title
end
最后进入表单视图:
<%= form_for @video do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<p/>
<%= f.label :description %>
<%= f.text_field :description %>
<%= submit_tag("Enqueue video") %>
<% end %>
我不太明白为什么表单输入是video [description]而不只是期望的描述:
I don't quite get why the form input are video[description] and not just description as expected :
<label for="video_title">Title</label>
<input id="video_title" name="video[title]" type="text" />
<p/>
<label for="video_description">Description</label>
<input id="video_description" name="video[description]" type="text" />
推荐答案
使用form_for
时:
创建一个允许用户创建或更新属性的表单 特定模型对象的对象.
Creates a form that allows the user to create or update the attributes of a specific model object.
在您的情况下,为Video
模型.要了解Rails约定:
In your case, Video
model. To understand Rails convention:
<%= form_for @video do |f| %>
...
<%= f.text_field :description %>
...
这将产生一个html标记,其名称属性为 video [description] .这意味着,提交表单后,用户输入的值将在控制器中显示为 params [:video] [:description] .
Which results in an html tag whose name attribute is video[description]. This means that when the form is submitted, the value entered by the user will be available in the controller as params[:video][:description].
根据文档,params
变量是ActiveSupport::HashWithIndifferentAccess
的实例,就像Hash
一样,差别很小:
The params
variable is an instace of ActiveSupport::HashWithIndifferentAccess
, like a Hash
with a small difference, according to documentation:
此类具有可疑的语义,我们只有这样才能使人们 可以编写 params [:key] 而不是 params ['key'] ,它们得到的结果相同 两个键的值.
This class has dubious semantics and we only have it so that people can write params[:key] instead of params[‘key’] and they get the same value for both keys.
您的params
类似于:
{"utf8"=>"✓",
"_method"=>"post",
"authenticity_token"=>"xxx",
"video"=>
{"description"=>"Video desc"}
}
其中"video"
或:video
是哈希的键之一.因此,params[:video]
等效于params.fetch("video")
,其值为{"description"=>"Video desc"}
.如您所见,该值是另一个哈希.最后,要获取描述的值,您必须先params[:video][:description]
(Rails方式)或params.fetch("video").fetch("description")
,其值是"Video desc"
.
Where "video"
or :video
is one of the keys of the Hash. So, params[:video]
is equivalent to params.fetch("video")
which value is {"description"=>"Video desc"}
. As you can see the value is another Hash. Finally to get the value of the description you have to params[:video][:description]
(The Rails way) or params.fetch("video").fetch("description")
, which value is "Video desc"
.
fetch
是Hash
的Ruby方法:从哈希中返回给定键的值."
fetch
is a Ruby method of Hash
: "Returns a value from the hash for the given key."
考虑到这一点:
Video.new(params[:video]) = Video.new(:description => "Video desc") = Video.new(:description => params[:video][:description])
使用约定更容易,但是请确保您可以使用params[:description]
(以防万一):
It's easier to use conventions, but for sure you can have params[:description]
(just in case):
<%= form_for @video do |f| %>
...
<%= text_field_tag :description %>
...
请注意,我使用的是text_field_tag
而不是f.text_field
.在这种情况下,params
哈希中的html标签名称将为description
,您将收到{ "description" => 'Video desc" }
...
Note that I'm using text_field_tag
instead of f.text_field
. In this case the html tag name will be description
in the params
Hash you will receive { "description" => 'Video desc" }
...
请访问 Rails API文档,以了解不同的帮助程序等.此外,还请查看服务器的日志.
Take a look to Rails API documentation to understand different helpers, etc. And also review your server's log.
这篇关于RoR:Mongoid和表单创建哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!