Reputation: 932
Ok so I'm trying to use simple form to create a course. The form does not push the data to the database. I have it working using the Select_tag from Rails but cannot get data validation working this way. I'm now trying to update the select_tag to work with simple_form. Data validation is working but the form tag f.association is not recognising the data.
The category model relation is correct to Course as I've gotten it working with the code below.
<%= select_tag(:category_id, options_for_select(@categories), :prompt => "Select Category") %>
This is the simple form code that does let me select through the available categories but it doesn't push the id to the database.I'll add my controller / model below for sanity checks. Thanks.
Course Model
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
Category Model
class Category < ActiveRecord::Base
has_many :courses
end
Course Controller
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
Any help as always is appreciated. Thanks.
Just to add whats going on in the background when creating a course when i remove presence = true for category_id
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"}
Category ID is there in the parameter. But if I look at the SQL query it is not included.
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) commit transaction Course Store (27.7ms) {"id":3} Redirected to http://localhost:3000/
Course Params looks ok as it has :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
Upvotes: 0
Views: 72
Reputation: 6834
I think the problem is here:
@course = current_user.courses.build(course_params)
@course.category_id = params[:category_id]
For what i see the category_id
is inside course_params
, so when you build the object is there but later you assign it to params[:category_id]
which is nil
.
Upvotes: 1