如何使用隐藏形式更改布尔值?

时间:2020-07-17 14:33:44

标签: html ruby-on-rails ruby web boolean

我正在尝试设置一个将帖子标记为不当内容的按钮。

所有帖子均设置为:flag值的初始值为'false'。我已经为按钮设置了一个隐藏表单,当在帖子的显示页面中单击该按钮时,会将:flag布尔值设置为true。当我提交按钮时,我得到一个空白页面,上面写着不允许。任何帮助将不胜感激!

posts_controller.rb

class PostsController < ApplicationController
  before_action :authenticate_account!, except: [:show, :index]
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  
def show
   @post = Post.find(params[:id])
end

def update
    if @post.account != current_account
      return render plain: 'Not Allowed', status: :forbidden
    end

    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

def post_params
   params.fetch(:post, {}).permit(:title,:contact , :content, :category_id, :genre_id, :city_id, :instrument_id, :flag)
end

show.html.erb中的隐藏表格

<%= form_for @post, {:url => post_path(@post), :method=>:patch} do |f| %>
  <%= f.hidden_field :flag, value: true %>
  <%= f.submit 'flag', class: 'btn btn-warning' %>
<% end %>

迁移AddFlagToPost

class AddFlagToPosts < ActiveRecord::Migration[5.2]
  def change
    add_column :posts, :flag, :boolean, default: false
  end
end

0 个答案:

没有答案