togi
togi

Reputation: 924

How to add different edit/update method into rails controller?

The controller:

def edit  
end

def update
    respond_to do |format|
      if @item.update(item_params)
        format.html { redirect_to @item, notice: 'Item was successfully updated.' }
        format.json { render :show, status: :ok, location: @item }
      else
        format.html { render :edit }
        format.json { render json: @item.errors, status: :unprocessable_entity }
      end
    end
  end

I want to add another methods like this. For updating only 1 column of item and i want to dont show other columns to user. I have all neccesary views.

def another_edit
end

def another_update
 respond_to do |format|
      if @item.update(params[:item].permit(:amount))
        format.html { redirect_to @item, notice: 'Item was successfully updated.' }
        format.json { render :show, status: :ok, location: @item }
      else
        format.html { render :edit }
        format.json { render json: @item.errors, status: :unprocessable_entity }
      end
    end

I thought my routes are wrong. Then tried

  resources :items do
    collection do
        get 'add', on: :edit
    end
  end

and

get 'items/another_edit'

Is it possible or not? Please help me.

Upvotes: 0

Views: 409

Answers (1)

M. Stavnycha
M. Stavnycha

Reputation: 433

You need to add those endpoints in routes as "member" (not collection):

resources :items do
   get :another_edit, on: :member
   put :another_update, on: :member
end

Then routes will be built with "id" parameter inside.

Upvotes: 1

Related Questions