Link_to with two parameters not working

2019-06-14 10:38发布

On a page of an organization I have a partial that shows the users with the moderator role for that organization. Behind each user/moderator there's a link to remove that role. However, this link isn't working. When clicking it, nothing happens. In the url bar of the browser it only shows new text: https://url/organizations/ek99?data[confirm]=Are+you+certain%3F&data[method]=post&data[organization_id]=100&data[stakeholder_id]=113&data[url]=%2Fremovemoderator%2Fek99. Does anyone perhaps see what is wrong with this code?

In the organizations controller:

def show
  @organization = Organization.friendly.find(params[:id])
  @moderators = User.with_role(:moderator, @organization)
end

The view page calls on the partial using <%= render 'users/moderator', collection: @moderators %>. The partial contains for each moderator the link:

<%= link_to image_tag("delete.gif", title: "remove as moderator", class: 'profile-icon-small'), 
            data: { user_id: moderator.id, organization_id: @organization.id, method: :post, url: removemoderator_path, confirm: "Are you certain?" } %>

Routes:

post 'removemoderator/:id'=>  'users#remove_moderator', as: 'removemoderator'

Users controller method:

def remove_moderator
  @organization = Organization.friendly.find(params[:id])
  remove_modrights(@organization)
end

User model method:

def remove_modrights(organization)
  self.remove_role :moderator, organization
end

1条回答
【Aperson】
2楼-- · 2019-06-14 11:11
def remove_moderator 
    @user = User.find(params[:id])
    @organization = Organization.friendly.find(params[:organization_id])
    @user.remove_modrights(@organization)
    redirect_to @organization
end

The url in link should contain id.

<%= link_to image_tag("delete.gif", title: "remove as moderator", class: 'profile-icon-small'), removemoderator_path(id: moderator.id, organization_id: @organization.id), method: :post, data: { confirm: "Are you certain?" } %>
查看更多
登录 后发表回答