首页 > 解决方案 > 保存到数据库后,年份属性为零 - 不从表单转移

问题描述

我的年份属性没有从表单下拉菜单中转移。我不确定我哪里出错了。

我的表格:

  <%= simple_form_for @movie do |f| %>

    <div class="form-group">
      <%= f.input :title, placeholder: "Movie Title", input_html: { class: 'form-control' } %>
    </div>

    <div class="form-row">
      <div class="form-group col-md-6">
        <%= f.input :year, as: :date,
            start_year: Date.today.year,
            end_year: Date.today.year - 100, 
            discard_day: true, order: [:year], 
            input_html: { class: 'form-control' } %>
      </div>

控制器代码:

class MoviesController < ApplicationController
  before_action :find_movie, only: [:show, :edit, :update, :destroy]

  def index
  end

  def new
    @movie = Movie.new
  end

  def create
    @movie = Movie.new(movie_params)
    if @movie.save
      redirect_to movie_path(@movie)
    else
      flash[:danger] = "Please try again!"
      redirect_to new_movie_path
    end
  end

  private

  def find_movie
    @movie = Movie.find(params[:id])
  end

  def movie_params
    params.require(:movie).permit(:title, :year, :genre, :poster, :director, :plot, :rating, :list_ids => [])
  end
end

它在视图中看起来和工作正常,但是当我检查数据库时,年份为零。有任何想法吗?

标签: ruby-on-rails

解决方案


推荐阅读