Tutorial for Editing Tags

ezbreaks edited this page Nov 19, 2016 · 1 revision

This assumes that you have already created tags and it's addressed only for editing the created tags.

First we need to add the routes for the tags controller:

Rails.application.routes.draw do
  resources :tags
end

We have to create a Tags Controller where we will add the UPDATE function from CRUD.

class TagsController < ApplicationController
  before_action :set_tag, only: [:show, :edit, :update, :destroy]

  def edit  
  end

  def update
    @tag = ActsAsTaggableOn::Tag.find(params[:id])
    
    respond_to do |format|
      if @tag.update(tag_params)
        format.html { redirect_to tags_path, notice: 'Tag was successfully updated.' }
        format.json { render :show, status: :ok, location: @tag }
      else
        format.html { render :edit }
        format.json { render json: @tag.errors, status: :unprocessable_entity }
      end
    end

  end
  
  private
  
  def set_tag
    @tag = ActsAsTaggableOn::Tag.find(params[:id])
  end

  def tag_params
    params.require(:acts_as_taggable_on_tag).permit(:id, :name)
  end

end

Now we have to add the corresponding views. We need to create the _form.html.erb partial with the following code and render the form in the edit.html.erb view.

<%= form_for(@tag, url: tag_path(@tag)) do |f| %>
 
  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control'  %>
  </div>
	
	<br>
  <div class="actions">
    <%= f.submit class: 'btn btn-primary' %>
  </div>
<% end %>

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.