Ruby新手在这里,所以请温柔。
我在表单中添加了一个下拉菜单,但是当我提交它时,它不会保存到数据库中。所有其他领域都有效,我正在以与其他领域类似的方式进行下拉。
我还使用以下命令将列添加到数据库中:
rails g migration AddStatusToIdeas status:enum
rake db:migrate
应用/模型/ idea.rb
class Idea < ActiveRecord::Base
has_many :comments
mount_uploader :picture, PictureUploader
# {attr_accessor :Status}
enum status: [ :Draft, :Published]
end
应用/视图/思想/ _form.html.erb
<%= form_for(@idea) do |f| %>
<% if @idea.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@idea.errors.count, "error") %> prohibited this idea from being saved:</h2>
<ul>
<% @idea.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :status %><br>
<%= f.select(:status, options_for_select([['Draft', 'Draft'],['Published', 'Published']])) %>
</div>
<div class="field">
<%= f.label :picture %><br>
<%= f.file_field :picture %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
答案 0 :(得分:3)
您有一个未经许可的参数status
:
开始发布&#34; / ideas&#34;对于127.0.0.1在2016-03-29 12:20:39 +0300 IdeasController处理#create as HTML参数: {&#34; UTF8&#34; = GT;&#34;✓&#34 ;, &#34; authenticity_token&#34; = GT;&#34; pVM48 / l0mTKe48pfg6TBytPO4eMNRmNBm1aDaHUvfQo =&#34 ;, &#34; idea&#34; =&gt; {&#34; name&#34; =&gt;&#34;测试SO&#34;,&#34;说明&#34; =&gt;&#34;测试对于SO&#34;, &#34; status&#34; =&gt;&#34;已发布&#34;},&#34;提交&#34; =&gt;&#34;创建提示&#34;}用户加载 (0.0ms)SELECT&#34;用户&#34;。* FROM&#34;用户&#34;用户&#34;。&#34; id&#34; = 2 ORDER BY&#34;用户&#34;。&#34; id&#34; ASC LIMIT 10 未允许的参数: 状态(0.0ms)开始事务SQL (0.5ms)INSERT INTO&#34; ideas&#34; (&#34; created_at&#34;,&#34; description&#34;, &#34; name&#34;,&#34; updated_at&#34;)VALUES(?,?,?,?)[0m [[&#34; created_at&#34;, &#34; 2016-03-29 09:20:39.034503&#34;],[&#34;描述&#34;,&#34;测试SO&#34;], [&#34;名称&#34;,&#34;测试SO&#34;],[&#34; updated_at&#34;,&#34; 2016-03-29 09:20:39.034503&#34; ]] (8.7ms)提交事务重定向到 http://localhost:3000/ideas/9完成302发现在24ms (ActiveRecord:9.2ms)
修复你的控制器许可参数方法。
def idea_params
params.require(:idea).permit(:name, :description, :status, :picture)
end