Ruby on rails 通过按钮为列指定值

Ruby on rails 通过按钮为列指定值,ruby-on-rails,forms,Ruby On Rails,Forms,我需要一些帮助。在这件事上,我的头脑已经过度紧张了。在花了几天的时间学习之后,我刚刚开始编写我的第一个应用程序 我正试图在“创建”一书中为名为:size:integer的列赋值。我相信我需要在控制器中加入一些东西来考虑这一点,但我已经画了一个空白。这是我的部分和我的控制器 <%= form_for(@book) do |f| %> <% if @book.errors.any? %> <div id="error_explanat

我需要一些帮助。在这件事上,我的头脑已经过度紧张了。在花了几天的时间学习之后,我刚刚开始编写我的第一个应用程序

我正试图在“创建”一书中为名为:size:integer的列赋值。我相信我需要在控制器中加入一些东西来考虑这一点,但我已经画了一个空白。这是我的部分和我的控制器

    <%= form_for(@book) do |f| %>
      <% if @book.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>

          <ul>
          <% @book.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

      <div class="field">
        <%= f.label :title %><br>
        <%= f.text_field :title %>
      </div>
      <div class="field">
      <%= f.button :size => ['Short', 0] :label =%>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>

禁止保存本书:

['Short',0]:标签=%>
控制器

class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update, :destroy]

  # GET /books
  # GET /books.json
  def index
    @books = Book.all
  end

  # GET /books/1
  # GET /books/1.json
  def show
  end

  # GET /books/new
  def new
    @book = Book.new
  end

  # GET /books/1/edit
  def edit
  end



  # POST /books
  # POST /books.json
  def create
    @book = Book.new(book_params)

    respond_to do |format|
      if @book.save
        format.html { redirect_to @book, notice: 'Book was successfully created.' }
        format.json { render action: 'show', status: :created, location: @book }
      else
        format.html { render action: 'new' }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @book.update(book_params)
        format.html { redirect_to @book, notice: 'Book was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /books/1
  # DELETE /books/1.json
  def destroy
    @book.destroy
    respond_to do |format|
      format.html { redirect_to books_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def book_params
      params.require(:book).permit(:title, :type)
    end
end
class BooksController
您的rails版本是什么?有什么错误?还没有错误。我试图理解如何从表单按钮定义列的值。Rails 4当您说“为列赋值”时,您是指“重命名列”还是简单地向其中输入某种值?只需向其中输入一个值,这样我就可以使用设置的值创建一个规则MM我不确定我是否理解您的意思,但是您为列赋值时执行@book.save。此命令触发插入查询