Quantico
Quantico

Reputation: 2446

Rails call a method from within a different view

I am trying to call a method of one controller from within a different view. I saw the following post Rails call destroy method from within another controller, but I am doing something wrong with my implementation.

The method I am trying to call is in an accessor controller.

    def remove_permission_from_index
      Accessor.find(params[:id]).destroy
      respond_to do |format|
        format.html { redirect_to tasks_url }
        format.json { head :no_content }
      end
    end

This controller also has the following strong param method

def accessor_params
  params.require(:accessor).permit(:accessor_id, :access_rights, :task_id)
end

My view is calling the following code

  <%= link_to 'Delete', {:controller => "accessors", :accessor => elem} ,method: :remove_permission_from_index, data: { confirm: "Are you sure?" }%>      </p>

where elem an element within a set that is defined in the task controller

@canEdit = Accessor.select(:task_id).where("accessor_id = ? AND access_rights = ?", current_user, true)
@canEdit.each do |p|
    p.task = Task.find(p.task_id)

When I am running my code I am getting the following error

param not found: accessor
    def accessor_params
      params.require(:accessor).permit(:accessor_id, :access_rights, :task_id)
    end
end

and I can see that the accessor param is not being passed :

Parameters:

{"_method"=>"remove_permission_from_index"}

a) why is the accessor_params is being called if I am trying to access a different method b) what am I doing wrong? After all I do set :accessor => elem

Upvotes: 0

Views: 88

Answers (1)

zwippie
zwippie

Reputation: 15525

Could be a copy paste error but:

<%= link_to 'Delete', {:controller => "accessors", :accessor => elem} ,method: :remove_permission_from_index, data: { confirm: "Are you sure?" }%>      </p>

should be more like:

<%= link_to 'Delete', {:controller => "accessors", :action => :remove_permission_from_index, :accessor => elem}, method: :delete, data: { confirm: "Are you sure?" }%>      </p>

Upvotes: 1

Related Questions