Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何验证是否至少选中了一个复选框,并将值发送到Rails中的另一个视图页面?_Html_Ruby On Rails - Fatal编程技术网

Html 如何验证是否至少选中了一个复选框,并将值发送到Rails中的另一个视图页面?

Html 如何验证是否至少选中了一个复选框,并将值发送到Rails中的另一个视图页面?,html,ruby-on-rails,Html,Ruby On Rails,新手到Rails来。因此,我正在为我的项目创建一个在线图书馆,在那里我可以上传用户选择的电子书(PDF格式)。我使用的是rails表单助手,我有一个复选框用户可以选择的类型列表。我希望rails验证是否至少选中了一个复选框,并将值发送到show.html.erb查看页面 为复选框设置:multiple=>true会返回一个错误,表示需要选择复选框,但是即使我单击所有复选框,它也不会消失;它只是停留在那里。这是我到目前为止所拥有的 提前谢谢你 # NEW.HTML.ERB, already i

新手到Rails来。因此,我正在为我的项目创建一个在线图书馆,在那里我可以上传用户选择的电子书(PDF格式)。我使用的是rails表单助手,我有一个
复选框
用户可以选择的类型列表。我希望rails验证是否至少选中了一个复选框,并将值发送到
show.html.erb
查看页面

为复选框设置
:multiple=>true
会返回一个错误,表示需要选择复选框,但是即使我单击所有复选框,它也不会消失;它只是停留在那里。这是我到目前为止所拥有的

提前谢谢你

  # NEW.HTML.ERB, already inside form_for loop #

  <div class="form-group">
    <h5>Genre(s) <span style="color:red">*</span> </h5> 
    <p class="error"><%= @book.errors.full_messages%></p>
    <% genre = ["Action","Adventure","Comedy","Drama","Ecchi","Fantasy","Horror","Magic","Mystery","Psychological","Romance","Sci-fi","Slice of Life","Supernatural"] %> 
    <% genre.each do |genre,i| %>
      <label>
        <%= f.check_box :genre, {:multiple => true}, id:"book_genre_#{i}"%>
        <%= genre %>
      </label><br>
    <% end %>
</div>


# CONTROLLER FILE #
class BooksController < ApplicationController
skip_before_action :verify_authenticity_token
def index  #step 4
    @books = Book.all

end

def show #step 3
    @book = Book.find(params[:id]) #get individual post page
end

def newBook 
    @book = Book.new
    #creates a new post of registered books 

end

def create #Step 2
    #render plain: params[:books].inspect #see what data is sent
    @book = Book.new(book_params)

    if(@book.save)
        redirect_to @book
    else
        render 'newBook' #if title is not there, just re renders newBook page
    end

end

def edit 
    @book = Book.find(params[:id]) #after this, make edit.html.erb
end

def update
    @book = Book.find(params[:id]) 

    if(@book.update(book_params))
        redirect_to @book
    else
        render 'edit'
    end

end

def destroy 

    @book = Book.find(params[:id])
    @book.destroy

    redirect_to books_path
end

def mangaList
    @books = Book.all
end


private def book_params #can only be accessed from this class #step1 
    params.require(:books).permit(:title, :author, :description, :file, volumes: [], genre: [])
end
end


# book.rb MODEL FILE #  
class Book < ApplicationRecord
has_one_attached :file #upload file on book, create record in 
 database 
has_many_attached :volumes
validates :title, presence: true
validates :author, presence: true
validates_presence_of :description
validates_presence_of :volumes 
validates :genre, acceptance: {message: 'must be selected'}
end

#UPDATE SCHEMA.DB
 # This file is auto-generated from the current state of the 
 database. Instead
 # of editing this file, please use the migrations feature of Active 
 Record to
  # incrementally modify your database, and then regenerate this 
 schema definition.
 #
 # Note that this schema.rb definition is the authoritative source 
 for your
 # database schema. If you need to create the application database on 
 another
 # system, you should be using db:schema:load, not running all the 
 migrations
 # from scratch. The latter is a flawed and unsustainable approach 
 (the more migrations
 # you'll amass, the slower it'll run and the greater likelihood for 
 issues).
 #
 # It's strongly recommended that you check this file into your 
 version control system.

 ActiveRecord::Schema.define(version: 2019_08_21_104451) do

 create_table "active_storage_attachments", force: :cascade do |t|
   t.string "name", null: false
   t.string "record_type", null: false
   t.integer "record_id", null: false
   t.integer "blob_id", null: false
   t.datetime "created_at", null: false
   t.index ["blob_id"], name: 
   "index_active_storage_attachments_on_blob_id"
  t.index ["record_type", "record_id", "name", "blob_id"], name: 
  "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", force: :cascade do |t|
  t.string "key", null: false
  t.string "filename", null: false
  t.string "content_type"
  t.text "metadata"
  t.bigint "byte_size", null: false
  t.string "checksum", null: false
  t.datetime "created_at", null: false
  t.index ["key"], name: "index_active_storage_blobs_on_key", unique: 
  true
   end

    # Could not dump table "books" because of following StandardError
    #   Unknown type 'body' for column 'description'

   end
#NEW.HTML.ERB,已在form_for loop中#
类型(s)*

对},id:“book_流派{i}”%>
#控制器文件# 类BooksController