在simple_form_for中创建具有2个嵌套属性和复选框的表,可以按照以下步骤进行:
gem 'simple_form'
然后运行bundle install
命令来安装gem。
app/views/posts/new.html.erb
中创建表单,可以这样写:<%= simple_form_for @post do |f| %>
<%= f.input :title %>
<%= f.input :content %>
<%= f.simple_fields_for :comments do |c| %>
<%= c.input :author %>
<%= c.input :body %>
<% end %>
<%= f.input :published, as: :boolean %>
<%= f.button :submit %>
<% end %>
在上面的例子中,我们使用simple_fields_for
方法来创建嵌套属性的表单。在这个例子中,我们使用了一个名为comments
的嵌套属性,并为其创建了两个输入字段:author
和body
。
PostsController
中,你可能需要这样设置:class PostsController < ApplicationController
def new
@post = Post.new
@post.comments.build
end
def create
@post = Post.new(post_params)
if @post.save
# 处理保存成功后的逻辑
else
# 处理保存失败后的逻辑
end
end
private
def post_params
params.require(:post).permit(:title, :content, comments_attributes: [:author, :body])
end
end
在上面的例子中,我们使用了comments_attributes
来允许接受嵌套属性的参数。
这样,你就可以在simple_form_for中创建具有2个嵌套属性和复选框的表单了。根据你的需求,你可以根据需要添加更多的嵌套属性和其他类型的输入字段。
请注意,以上答案中没有提及任何特定的云计算品牌商,因为这与问题的内容无关。如果你需要了解与云计算相关的信息,可以参考腾讯云的官方文档和产品介绍页面。
领取专属 10元无门槛券
手把手带您无忧上云