ruby - Rails: Save country_select to database -
i’m working on form user can pick country , there choice shown , show.html.erb.
i have added country_select in form this
<div class="col-md-4"> <div class="form-group"> <%= f.country_select :country %> </div>
my model article.rb looks this:
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
the select works , user can select country.. perfect!
but won’t show in view/show.html.erb.. have tried this:
<%= @article.country %>
so therefor generated migration:
class addcountrytoarticles < activerecord::migration[5.0] def change add_column :article, :country, :string end end
and ran migration.
in controller added params:
def article_params params.require(:article).permit(:country, :title, :description, article_article_categories_ids: []) end
but nothing.. @ country_select documentation, under usage: "simple use supplying model , attribute parameters: country_select("user", "country")"
but don’t know, put line of code.. have tried put in create, show, , params.. , updated (“article”, "country")
can me step closer? devise installed.. maybe cause trouble? i'm working rails 5.0.0
first, rid of attr_accessor :country
attr_accessor used define attribute object of model not mapped column in database.
to answer question "but don’t know, put line of code.. have tried put in create, show, , params.. "
you have put line of code in form view. first attribute name of model (article in case) , second name of attribute (country in case). have done with:
<%= f.country_select :country %>
you have make sure form article
should be:
<%= form_for @article |f| %> <div> <%= f.country_select :country %> </div> <% end %>
Comments
Post a Comment