MusicsController#new中的SyntaxError

时间:2017-03-30 04:27:28

标签: ruby-on-rails ruby

我想将多个复选框值保存到Database.When运行此代码时出现错误: - premature end of char-class: /[\][\]\"]/ 当我单击复选框以选中任何复选框值时,它应该仅在该页面下方显示该复选框值的详细信息。 这是我的代码:

music.rb

class Music < ActiveRecord::Base

before_save do 
  self.genre.gsub!(/[\][\]\"]/,"") if attribute_present?("genre")


    end
end

musics_controller.rb

class MusicsController < ApplicationController
    before_action :set_music, only: [:show, :edit, :update, :destroy]

   def index
    @musics = Music.all
   end

   def show
   end

  def new
   @music = Music.new
   end

  def edit
   end

  def create
   @music = Music.new(music_params)

   respond_to do |format|
    if @music.save
    format.html { redirect_to @music, notice: 'Music was successfully created.' }
    format.json { render :show, status: :created, location: @music }
  else
     format.html { render :new }
     format.json { render json: @music.errors, status: :unprocessable_entity }
   end
 end
end
def update
   respond_to do |format|
    if @music.update(music_params)
     format.html { redirect_to @music, notice: 'Music was successfully updated.' }
     format.json { render :show, status: :ok, location: @music }
   else
     format.html { render :edit }
     format.json { render json: @music.errors, status: :unprocessable_entity }
    end
  end
end

def destroy
   @music.destroy
    respond_to do |format|
    format.html { redirect_to musics_url, notice: 'Music was successfully destroyed.' }
    format.json { head :no_content }
    end
  end

private
    def set_music
  @music = Music.find(params[:id])
end

def music_params
  params.require(:music).permit(:title, :artist, genre:[])
end
end

0 个答案:

没有答案