【问题标题】:Checkbox function to button复选框功能到按钮
【发布时间】:2018-04-11 14:39:16
【问题描述】:

我在复选框中有布尔函数。当复选框被选中并提交时,它将是真实的。现在我需要一个名为 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

【问题讨论】:

  • 到目前为止你尝试了什么?
  • @Harini 你只想要名为 block 和 unblock 的按钮而不是提交按钮和复选框吗?
  • @Gabbar 我想要一个必须更改布尔函数然后提交的按钮

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


【解决方案1】:

更新

你可以这样做

<% 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 %>

所以,当您单击以检查 truefalse 时,您需要一个复选框,对吗?您可以使用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>

当我选中复选框并提交时看看,然后参数哈希看起来像这样

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

当取消选中复选框时,然后

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

【讨论】:

    【解决方案2】:

    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 但您将它作为 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
    

    更新

    由于您已经更新了使用块方法来更改用户的active 状态,所以在这种情况下不要忘记将此pathmethodbutton_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 %>
    

    //控制器

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2020-04-03
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多