需求:一个新建表单,包含一个可输入文本框、多个 check_box,选择多个 check_box 值,无法保存数据到数据库 错误提示:
Status: 500 Internal Server Error
expected Array (got String) for param `tag_line'
百度 谷歌了很多方法 还是无法解决 一下是我的代码
view:
<% form_for [:admin, @article], :html => {:multipart => true} do |f| %>
<div>
<%= f.label :tag_line %><%= f.text_field :tag_line, :style => "width:450px" %>
</div>
<dl class="tag-list" rel="tag-list">
<dt>可选标签:</dt>
<dd>
<%
%w(段子 小说 神 童年 帝 时事热点 伦理 校园 上班 恋爱 漫画 笑话 游戏玩家).each do |tag| %>
<%= f.check_box :tag_line, { :multiple => true }, "#{tag}", "" %> <%= tag %>
<% end %>
<dd>
</dl>
<% end %>
生成页面源码:
<form action="/admin/articles" class="new_article" enctype="multipart/form-data" id="new_article" method="post">
<div><label for="article_tag_line">标签</label><input id="article_tag_line" name="article[tag_line]" size="30" style="width:450px" type="text" /></div>
<input type="hidden" value="" name="article[tag_line][]">
<input id="article_tag_line" type="checkbox" value="段子" name="article[tag_line][]" multiple="multiple">
段子
.
.
.
</form>
相应的 controller 中代码:
class Admin::ArticlesController < Admin::BaseController
def new
@article = Article.new
end
def create
@article.attributes = params[:article]
if @article.save
flash[:success] = 'article created'
redirect_to [:edit, :admin, @article]
else
render :action => :new
end
end
end
这是提交表单时终端给我的输出Completed in 216ms (View: 124, DB: 5) | 200 OK [http://localhost/admin/articles/new]