Trouble saving tags in Ruby on Rails 4 with acts_as_taggable

China☆狼群 提交于 2019-12-03 22:06:58
colemerrick

If I understand the question correctly, all you should need to do is, essentially, tell the recommended protection model for params(strong_parameters) about the acts-as-taggable gem. Your already Halfway there with this:

private

    def micropost_params
      params.require(:micropost).permit(:content)
    end

    [...]
end

Just add:

private

    def micropost_params
     params.require(:micropost).permit(:content, :tag_list => [])
    end

    [...]
end

Also, you may want to add one thing to your routes.rb file.. Replace the line:

resources :microposts, only: [:create, :destroy]

with:

resources :microposts, only: [:create, :destroy, :tag]

Let me know if these suggestions do the trick. Good luck!

I think, input variable for the tags in the form should tags_list. i.e

<%= form_for(@micropost) do |f| %>
  ...
  <div class="field">
    ...
    <%= f.label :tags %>
    <%= f.text_field :tags_list %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

And also update the _micropost.erb.html to use tags_list.

Or you can just update your with model to use a tag attribute instead i.e 'acts_as_taggable_on :tag'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!