我无法在sqlite Rails中保存记录

时间:2018-05-30 12:57:18

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

我尝试在数据库中保存记录,在数据透视表中,但如果我写@video.category_ids = 1它有效但数据库中的类别id = 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上。

1 个答案:

答案 0 :(得分:2)

我建议进行以下更改:

  1. 在_form.html.erb中复数选择的名称,即更改

    <%= f.select(:category_id, @categories.map{|t| [t.name, t.id]}, {}, :multiple => true, :class => 'custom-select') %>
    

    <%= f.select(:category_ids, @categories.map{|t| [t.name, t.id]}, {}, :multiple => true, :class => 'custom-select') %>
    
  2. 在videos_controller.rb中更改

    def video_params
      params.require(:video).permit(:title, :url, :description, category_attributes: [:id]).merge(user_id: session[:user_id])
    end
    

    def video_params
      params[:video][:category_ids].reject!(&:blank?) unless params[:video][:category_ids].nil?
      params.require(:video).permit(:title, :url, :description, category_ids: []).merge(user_id: session[:user_id])
    end
    
  3. 同样在videos_controller.rb中删除

    @video.category_ids = [video_params]
    

    将事情留给构造函数

  4. 关于2.我对确切的解决方案不是100%肯定,也许你也应该拒绝参数中的空ID(根据提供的截图,使用.reject!(&:blank?)) - 进一步分析问题在将video_params函数传递给构造函数之前检查它的输出 - 它基本上应该看起来像是预期对象的哈希表示。

    修改更新了2.关于category_ids

    的更改提案