Search code examples
ruby-on-railsrubyacts-as-taggable-on

Ruby on Rails: video object tags not updating


Video objects have tags (via acts-as-taggable-on gem). The update function works for all of the video attributes except for tags. I'm not sure why. Any insight would be greatly appreciated.

views/videos/_new.html.erb

      <div class="">
        <%= f.label(:tag_list, "Tags (separated by commas)") %></br >
        <%= f.text_field :tag_list, multiple: true, class: "form-control" %>
      </div>

videos_controller.rb

  def edit
    @video = Video.find(params[:id])
    @post = @video.post
  end

  def update
    @video = Video.find(params[:id])
    @post = @video.post
    @video.update_attributes(video_params)
    if current_user == @video.user
      if @video.save
        flash[:notice] = "Video successfully updated!"
        redirect_to video_path(@video)
      else
        flash[:notice] = "Video was not updated."
        @errors = @video.errors.full_messages.join(", ")
        flash[:alert] = @errors
        render action: "edit"
      end
    else
      flash[:notice] = "Only OP can edit video"
      refresh :edit
    end
  end

  private

  def video_params
    params.require(:video).permit(
      :title,
      :url,
      :tag_list,
    )
  end

Solution

  • Looks like your video_params is filtering it.

    Try

     params.require(:video).permit(
          :title,
          :url,
          tag_list: []
        )