简体   繁体   English

在Rails中更改数据库值的按钮

[英]Button that changes database value in Rails

Part of Group show.html.erb looks like: 组show.html.erb的一部分如下所示:

  <% if !@pending.empty? %>
<div class="panel panel-default">
  <div class="panel-heading"> <h4>Pending Approvals for Group</h4></div>
    <table class="table", style="border-collapse: separate; border-spacing: 5px 1px;">
      <thead>
        <tr>
          <th>User Email</th>
          <th>Accept/Reject</th>
        </tr>
      </thead>
    <% @pending.each do |individual| %>
      <tbody>
        <tr>
          <td><%= individual.user.email %></td>
          <td>Accept/Reject (BUTTONS HERE)</td>
        </tr>
      </tbody>
    <% end %>
    </table>
  </div>
</div>
  <% end %>

Instead of the Accept/Reject , I need to have two buttons that changes the value in the database column membership. 我需要有两个按钮来更改数据库列成员资格中的值,而不是Accept / Reject。 Accept would put a value of "member" and Reject would put "denied". 接受将放置“成员”的值,拒绝将放置“拒绝”的值。 However, this change is in the GroupUser not Group, which is a joint table of Groups and Users. 但是,此更改位于GroupUser而不是Group中,GroupUser是Groups和Users的联合表。

In other words, the accept button would essentially do <% individual.update(membership: "member")%> and reject would do <% individual.update(membership: "denied")%> . 换句话说,接受按钮本质上将执行<% individual.update(membership: "member")%>而拒绝将执行<% individual.update(membership: "denied")%>

The button would then change to something like, Approved or Denied. 然后,该按钮将更改为“已批准”或“已拒绝”。 I tried various options but none of them seem to work. 我尝试了各种选择,但似乎都不起作用。 I am not sure if/how it can be achieved with javascript or jQuery. 我不确定是否/如何用javascript或jQuery实现。

This is part of my attempt to create a "Join Group" request feature that allows other users to request to join a private group and gain membership. 这是我尝试创建“加入群组”请求功能的一部分,该功能允许其他用户请求加入私人群组并获得成员资格。

All I needed was a link (button) that would pass the parameters to a controller, which would make the change according to these parameters like shown below: 我需要的只是一个链接(按钮),它将参数传递给控制器​​,控制器将根据这些参数进行更改,如下所示:

Group show.html.erb 组show.html.erb

        <td><%= link_to 'Accept', {:controller => "group",
                                   :action => "decision",
                                   :id => individual.group_id,
                                   :user_id => individual.user.id,
                                   :decision => "true"}, method: 'post', class: "btn btn-sm btn-success" %>

Group controller 组控制器

  def decision
    @groupUser = GroupUser.where(group_id: params[:id], user_id: params[:user_id])
    if params[:decision] == "true"
      @groupUser.update(membership: "member")
      Homework.where(group_id: params[:id]).find_each do |hw|
        UserHomework.create(user_id: params[:user_id], homework_id: hw.id, admin: true, status: 'No attempt')
      end
    else
      @groupUser.update(membership: "denied")
    end
    redirect_to groupshow_path(:id => params[:id])
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM