复选框功能按钮

时间:2018-04-11 05:24:53

标签: ruby-on-rails ruby checkbox boolean-operations

我在复选框中有布尔函数。当选中并提交复选框时,它将成立。现在我需要一个名为unblock的按钮,当我第一次按下它时必须为true并且提交并且按钮名称必须更改为阻止。并在第二次点击时,它必须更改为false。

我的控制器代码

def index
    @users = User.all
end

def edit
end

def update
  respond_to do |format|
    if @user.update(user_params)
      format.html { redirect_to @user, notice: 'User was successfully updated.' }
    else
      format.html { render :edit }
    end
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
def set_user
    @user = User.find(params[:id])
end

  # Never trust parameters from the scary internet, only allow the white list through.
def user_params
    params.require(:user).permit(:active)
end

我的观看代码

<%= form_with(model: user, local: true) do |form| %>
      <% if user.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>

          <ul>
          <% user.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

      <div class="field">
        <%= form.label :active %>
        <%= form.check_box :active %>
      </div>

      <div class="actions">
        <%= form.submit %>
      </div>
 <% end %>

任何人都可以帮助我。

我尝试了什么

查看代码

<% if user.active? %>    
  <%= button_to user_path(value: false), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
  Unblock
  <% end %>
<%else%>
  <%= button_to user_path(value: true), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
  block
  <% end %>
<%end%>

控制器代码

def block
  user = User.find(params[:id])
  user.update(active: params[:value])
end

2 个答案:

答案 0 :(得分:0)

<强>更新

你可以简单地这样做

<% if user.active? %>    
  <%= button_to user_path(id: user.id, value: false), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
  Unblock
  <% end %>
<% else %>
  <%= button_to user_path(id: user.id, value: true), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
  block
  <% end %>
<% end %>

<强>旧

因此,当您单击以检查true时,您需要一个复选框,否则为false,对吧?您可以使用hiddent_field执行此操作,默认值为false,如

<%= form.hidden_field :active, 'false' %>

所以代码看起来像这样

<div class="field">
    <%= form.label :active %>
    <%= form.hidden_field :active, 'false' %>
    <%= form.check_box :active, true %>
</div>

看看我选中复选框并提交然后params hash看起来像这样

Parameters: {"utf8"=>"✓", "authenticity_token"=>"UKbxyz...==", "active"=>"true", "commit"=>"Submit"}

取消选中复选框

Parameters: {"utf8"=>"✓", "authenticity_token"=>"UKbxyz...==", "active"=>"false", "commit"=>"Submit"}

答案 1 :(得分:0)

butto_to标签本身会创建表单,并且模型的错误不应该在form_with内,所以我在这里修改了您的代码,请看看

  <% if user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<% if user.active? %> 
    <%= button_to user_path(value: false, id: user.id), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
      Unblock
    <% end %>
<%else%>
    <%= button_to user_path(value: true, id: user.id), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
      Block
    <% end %>
<%end%>

此时,您需要提供两个参数valueuser(将更改用户的状态)button_to

但问题是点击按钮阻止/取消阻止它会点击更新操作,在此操作中您将获得params[:value] = falseparams[:id] = "2"

并且这不起作用,因为在更新操作中您正在处理强参数中的参数,我的意思是您期望params[:user][:value]将返回nil但是您将其作为{{1}所以它应该是这样的: -

params[:value]

更新

由于您更新了使用阻止方法以更改用户的def update if @user.update(active: params[:value]) respond_to do |format| format.html { redirect_to @user, notice: 'User was successfully updated.' } format.html { render :edit } end end end 状态,因此在这种情况下,请不要忘记将此activepathmethod一起使用 示例: - 假设您的路径为button_to 则: -

user_block_path

//控制器

<%= button_to user_block_path(value: false, id: user.id), data: {confirm: 'Are you sure?'}, method: :patch, class: 'btn btn-default btn-danger' do %>
  Unblock
<% end %>