Я пытаюсь сохранить запись в базе данных, в сводной таблице, но у меня появляется ошибка (экран), если я пишу @video.category_ids = 1
, но в базе данных работает идентификатор категории = 1
Ниже я добавлю свой код, если вам нужнодополнительную информацию пишите.
videos_controller.rb
class VideosController < ApplicationController
layout 'master'
before_action :chceck_login
def index
@videos = Video.sortASC
end
def new
@categories = Category.all
end
def create
@video = Video.new(video_params)
@video.category_ids = [video_params]
if @video.save
flash[:notice] = 'The movie has been added successfuly'
redirect_to(:action => 'index')
else
@categories = Category.all
render('new')
end
end
def show
end
def edit
end
def delete
end
private
def video_params
params.require(:video).permit(:title, :url, :description, category_attributes: [:id]).merge(user_id: session[:user_id])
end
end
_form_html.erb
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><i class="fas fa-tag"></i></span>
</div>
<%= f.text_field :title, placeholder: 'Title', :class => 'form-control', :id => 'title' %>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><i class="fas fa-link"></i></span>
</div>
<%= f.text_field :url, placeholder: 'URL', :class => 'form-control', :id => 'url' %>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><i class="fas fa-edit"></i></span>
</div>
<%= f.text_area :description, placeholder: 'Description', rows: 5, :class => 'form-control', :id => 'description' %>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><i class="fas fa-sitemap"></i></span>
</div>
<%= f.select(:category_id, @categories.map{|t| [t.name, t.id]}, {}, :multiple => true, :class => 'custom-select') %>
</div>
video.rb
class Video < ApplicationRecord
belongs_to :user
has_and_belongs_to_many :categories
scope :sortASC, lambda{order("videos.created_at ASC")}
end
new.html.erb
<% @page_title = 'Add Film' %>
<div class='container'>
<div class='row'>
<div class='col s-6'>
<div class="jumbotron jumbotron-fluid bg-info">
<div class="container">
<h1 class="display-4 text-center">ADD NEW FILM <i class="fas fa-plus"></i></h1>
<p class="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.</p>
</div>
</div>
</div>
<div class='col s-6'>
<%= form_for(:video, :url => {:action => 'create'}) do |f| %>
<%= render(:partial => 'form', :locals => {:f => f}) %>
<%= submit_tag('Add Film', :class => 'btn btn-primary float-right') %>
<% end %>
</div>
</div>
</div>
Ошибка https://imgur.com/7P4WpMU
Если хотите, я разместил свой проект на Github.