【发布时间】:2016-08-23 20:48:40
【问题描述】:
我正在制作一个表单,用户可以在其中选择国家/地区,然后会显示选项并显示.html.erb。
我已经像这样在我的表单中添加了 country_select
<div class="col-md-4">
<div class="form-group">
<%= f.country_select :country %>
</div>
我的模型 article.rb 如下所示:
class Article < ActiveRecord::Base
belongs_to :author
has_many :article_article_categories
has_many :categories, through: :article_article_categories
validates :title, presence: true, length: { minimum: 3, maximum: 50 }
validates :description, presence: true, length: { minimum: 10, maximum: 500 }
validates :author_id, presence: true
attr_accessor :country
end
选择有效,用户可以选择国家..完美!
但它不会出现在我的视图/show.html.erb.. 我试过这样:
<%= @article.country %>
因此我生成了一个迁移:
class AddCountryToArticles < ActiveRecord::Migration[5.0]
def change
add_column :article, :country, :string
end
end
并运行迁移。
在我的控制器中,我将此添加到我的参数中:
def article_params
params.require(:article).permit(:country, :title, :description, article_article_categories_ids: [])
end
但我什么也没得到.. 在 country_select 文档中,使用中: “简单地使用提供模型和属性作为参数: country_select("用户", "国家")"
但我真的不知道该把那行代码放在哪里.. 我试图把它放在我的创建、显示和参数中。并更新为(“文章”,“国家”)
有人可以帮我更近一步吗? 我也设计安装..也许这会造成一些麻烦? 我正在使用 rails 5.0.0
【问题讨论】:
-
你为什么将
country定义为attr_accessor?你所说的表明country没有正确保存,检查rails日志看它的参数是否通过控制器会有所帮助跨度> -
它真的存在于这个国家吗?检查您的 Rails 控制台 (
rails console):Article.pluck(:country)
标签: ruby-on-rails ruby rubygems country