Rails simple_form不将数据字段推送到数据库

时间:2017-05-22 14:44:41

标签: ruby ruby-on-rails-4 simple-form

好的,我试着用简单的形式来创建课程。表单不会将数据推送到数据库。我使用Rails的Select_tag工作,但不能以这种方式进行数据验证。我现在正在尝试更新select_tag以使用simple_form。数据验证工作正常,但表单标签f.association无法识别数据。

类别模型关系对于课程是正确的,因为我已经使用下面的代码。

<%= select_tag(:category_id, options_for_select(@categories), :prompt => "Select Category") %>

这是一个简单的表单代码,可让我选择可用的类别,但它不会将id推送到数据库。我将在下面添加我的控制器/模型以进行完整性检查。谢谢。

课程模型

         validates :course_reference, :title, :course_img_file_name, :description, :short_description, :venue, :duration_days, :duration_weeks, :start_date, :start_time, :end_date, :max_enrolment, :price, :category_id, :presence => true

searchkick

belongs_to :user
belongs_to :category
belongs_to :location

类别模型

class Category < ActiveRecord::Base has_many :courses end

课程控制器

def new
    @course = current_user.courses.build
    @categories = Category.all.map{ |c| [c.name, c.id]}
    @locations = Location.all.map{ |c| [c.name, c.id]}
end

def create
    @course = current_user.courses.build(course_params)
    @course.category_id = params[:category_id]
    @categories = Category.all.map{ |c| [c.name, c.id]}
    @locations = Location.all.map{ |c| [c.name, c.id]}

    if @course.save
        redirect_to root_path
    else
        render 'new'
    end
end

任何帮助一如既往地表示赞赏。感谢。

当我为category_id删除presence = true时创建课程时,在后台添加最新内容

Started POST "/courses" for ::1 at 2017-05-22 15:54:13 +0100 Processing by CoursesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"du0IPedCSPueWp1RIOqWNenXiIpQLMLSohvt1ls7BUteoDFxzGFpVQ959iryIq+wBEUeaNOkAmA3jCjAG+d0Vw==", "course"=>{"course_reference"=>"cf0002", "course_img"=>#<ActionDispatch::Http::UploadedFile:0x007fce9a6ee950 @tempfile=#<Tempfile:/var/folders/gx/86yj74bx3md88cfn2fwc975h0000gn/T/RackMultipart20170522-1878-15pqchz.png>, @original_filename="Health Course Red.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"course[course_img]\"; filename=\"Health Course Red.png\"\r\nContent-Type: image/png\r\n">, "title"=>"Infection Control 1022", "category_id"=>"1", "description"=>"test", "short_description"=>"Short description should really be kept to the snappy bare bones", "venue"=>"36 annamoe park, cabra, dublin", "duration_days"=>"1", "duration_weeks"=>"1", "start_date(1i)"=>"2017", "start_date(2i)"=>"5", "start_date(3i)"=>"22", "start_time(1i)"=>"2017", "start_time(2i)"=>"5", "start_time(3i)"=>"22", "start_time(4i)"=>"14", "start_time(5i)"=>"48", "end_date(1i)"=>"2017", "end_date(2i)"=>"5", "end_date(3i)"=>"22", "max_enrolment"=>"1", "price"=>"1"}, "_wysihtml5_mode"=>"1", "location_id"=>"", "commit"=>"Create Course"}

参数中有类别ID。但是,如果我查看SQL查询,它不包括在内。

SQL (0.5ms)  INSERT INTO "courses" ("title", "description", "price", "course_img_file_name", "course_img_content_type", "course_img_file_size", "course_img_updated_at", "venue", "max_enrolment", "course_reference", "short_description", "duration_days", "duration_weeks", "start_date", "start_time", "end_date", "user_id", "latitude", "longitude", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["title", "Infection Control 1022"], ["description", "test"], ["price", 1], ["course_img_file_name", "Health_Course_Red.png"], ["course_img_content_type", "image/png"], ["course_img_file_size", 17181], ["course_img_updated_at", "2017-05-22 14:54:13.012445"], ["venue", "36 annamoe park, cabra, dublin"], ["max_enrolment", 1], ["course_reference", "cf0002"], ["short_description", "Short description should really be kept to the snappy bare bones"], ["duration_days", 1.0], ["duration_weeks", 1.0], ["start_date", "2017-05-22"], ["start_time", "2017-05-22 14:48:00.000000"], ["end_date", "2017-05-22"], ["user_id", 1], ["latitude", 53.3598805], ["longitude", -6.289487299999999], ["created_at", "2017-05-22 14:54:13.705340"], ["updated_at", "2017-05-22 14:54:13.705340"]]

(2.7ms)提交事务   课程商店(27.7ms){&#34; id&#34;:3} 重定向到http://localhost:3000/

课程参数看起来没问题,因为它有:category_id

def course_params params.require(:course).permit(:title, :description, :price, :category_id, :course_img, :venue, :location_id, :max_enrolment, :course_reference, :short_description, :duration_days, :duration_weeks, :start_date, :start_time, :end_date, :user_id, :complete) end

1 个答案:

答案 0 :(得分:1)

我认为问题在于:

@course = current_user.courses.build(course_params)
@course.category_id = params[:category_id]

我看到category_id位于course_params内,所以当您构建对象时,会将其分配给params[:category_id] nil