在视图中
<input id="checkbox-1" type="checkbox" value="drama" name="user[book][]">
<input id="checkbox-2" type="checkbox" value="poem" name="user[book][]">
<input id="checkbox-3" type="checkbox" value="fantasy" name="user[book][]">
如果选中1和3复选框,则可能会出现类似&#34;戏剧,幻想&#34;保存在野外书中
控制器中的
private
def user_params
params[:user][:book] = params[:user][:book].join(',')
params.require(:user).permit(title, email, :book, message)
end
def create
@user = User.new(user_params)
if @user.save
UserMailer.new_feedback(@user).deliver
render :json => { :result => 'Text message' }
end
end
当我尝试将表单输入保存到数据库时,我收到以下错误:
Template is missing
没有params[:user][:book] = params[:user][:book].join(',')
工作正常,但我需要将数组转换为字符串以存储在DB列中
答案 0 :(得分:0)
如果我理解你有代码
def create
@user = User.new user_params
if @user.save
UserMailer.new_feedback(@user).deliver
render :json => {:result => 'Text message'}
end
# what happens if doesnt pass
end
你需要
def create
@user = User.new user_params
if @user.save
UserMailer.new_feedback(@user).deliver
render :json => {:result => 'Text message'}
else
render :json => @user.errors.first, :status => 400
# when user does not create controller is looking for some template and you don't have template
end
end