我将如何添加按钮ID,以便告诉我在Rails中按下了哪个按钮

时间:2015-07-15 12:56:34

标签: ruby-on-rails

我有一堆代码

 <%= link_to admin_conference_statuses_path(conference_id: @conference.id), class: "btn btn-primary", method: :post, remote: true do %>
      <span id="span">Comm invoiced out Venue</span>
  <% end %>


  <%= link_to admin_conference_statuses_path(conference_id: @conference.id), class: "btn btn-primary", method: :post, remote: true do %>
      <span id="span">Cross charged to Client</span>
  <% end %>

我在控制器中有这个

def create
    conference_id = params[:conference_id] #Keep the same
    @conference_status = ConferenceStatus.find_by_conference_id(conference_id)#Keep the same
    @conference_status = ConferenceStatus.new unless @conference_status#Keep the same
    @conference_status.conference_id = params[:conference_id]
    @conference_status.invoiced_out_user_id = current_user.id
    @conference_status.invoiced_out_datetime = DateTime.now
    if @conference_status.save
    # Success
    else
    # Failure

    end
  end

现在,当按下一个按钮时,它会抓取id并将其放入数据库。

如何添加它以便在按下按钮2(与按钮1相对)时将当前用户ID放入名为“cross_charged_user_id”的列中

如果你有答案,你可以发布并解释它的作用,所以下次我知道吗?

由于 萨姆

1 个答案:

答案 0 :(得分:0)

您可以将一个额外参数传递给第二个链接。然后,根据此额外参数,您可以将当前用户指定为cross_charged_user

html代码如下:

<%= link_to admin_conference_statuses_path(@conference), class: "btn btn-primary", method: :post, remote: true do %>
  <span id="span">Comm invoiced out Venue</span>
<% end %>

<%= link_to admin_conference_statuses_path(@conference, cross_site_to_client: true), class: "btn btn-primary", method: :post, remote: true do %>
  <span id="span">Cross charged to Client</span>
<% end %>

控制器只需检查params[:cross_site_to_client]并指定当前用户

if params[:cross_site_to_client].present?
  @conference_status.cross_site_to_client_id = current_user.id
end

即使您也可以清理代码

@conference_status = ConferenceStatus.find_or_create_by_conference_id(params[:conference_id])

if params[:cross_site_to_client].present?
  @conference_status.cross_site_to_client = current_user
else
  @conference_status.invoiced_out_user = current_user
end

@conference_status.invoiced_out_datetime = DateTime.now
@conference_status.save