我正在使用正确安装的rails simple_form gem上的ruby。我在"创建"时遇到问题。将表单中的数据保存到数据库中。
仅供参考:cott是一种值得社区支持的情况。
用户有很多cotts和cotts属于用户。
我删除了所有验证,表单将在没有数据的情况下保存。我可以在cott show页面上输出用户信息,但是cott数据没有保存。
当我添加验证时,收到错误: 情况不可能是空白的 解决方案不能为空 抵制不可能是空白的 受害者不能空白 违规者不能为空
class Cott < ActiveRecord::Base
belongs_to :user
has_many :categorizations
has_many :types, :through => :categorizations
validates :situation, presence: true, length: {maximum: 255}
validates :solution, presence: true, length: {maximum: 255}
validates :boycott, presence: true, length: {maximum: 255}
validates :victim, presence: true, length: {maximum: 80}
validates :violator, presence: true, length: {maximum: 80}
default_scope -> { order(created_at: :desc) }
end
Cott Controller
class CottsController < ApplicationController
before_action :logged_in_user, only: [:show, :edit, :update, :destroy]
def new
@cott = Cott.new
end
def create
@cott = current_user.cotts.build if logged_in?
respond_to do |format|
if @cott.save
format.html { redirect_to @cott, notice: 'Cott was successfully created.' }
format.json { render :show, status: :created, location: @cott }
else
format.html { render :new }
format.json { render json: @cott.errors, status: :unprocessable_entity }
end
end
end
private
def cott_params
params.require(:cotts).permit(:user_id, :situation, :solution, :boycott, :victim, :violator, :violator_address, :violator_emails)
end
end
用户控制器
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: :destroy
def new
@user = User.new
end
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
@user.send_activation_email
UserMailer.account_activation(@user).deliver_now
format.html { redirect_to root_url, notice: 'Please check your email to activate your account.' }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
表格
<%= simple_form_for(@cott, html: { class: 'form-horizontal'}) do |f| %>
<fieldset class="univ_padding action">
<% if @cott.errors.any? %>
<div id="error_explanation" >
<h2><%= pluralize(@cott.errors.count, "error") %> prohibited this cott from being saved:</h2>
<ul>
<% @cott.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="col-md-7">
<!-- proof -->
<!-- situation -->
<div class="form-group">
<%= f.label :situation, label: false, class: " control-label" %><br>
<div class="">
<%= f.input :situation, label: false, label: false, class: "form-control situaton_form_area" %>
<span class="help-block">Explain the situations</span>
</div>
</div>
<!-- solution -->
<div class="form-group">
<div class="">
<%= f.label :solution, label: false, class: " control-label" %><br>
<%= f.input :solution, label: false, class: "form-control situaton_form_area" %>
<span class="help-block">How can the violator fix this situation.</span>
</div>
<!-- boycott -->
<div class="">
<%= f.label :boycott, label: false, class: " control-label" %><br>
<%= f.input :boycott, label: false, class: "form-control situaton_form_area" %>
<span class="help-block">How do you suppose we boycott this violator.</span>
</div>
</div>
<!-- victim -->
<div class="form-group">
<div class="">
<%= f.label :victim, label: false, class: " control-label" %><br>
<%= f.input :victim, label: false, class: "form-control" %>
<span class="help-block">Who are the victims?</span>
</div>
<!-- violator name -->
<div class="">
<%= f.label :violator, "Violator Name", label: false, class: " control-label" %>
<%= f.input :violator, label: false, class: "form-control" %>
<span class="help-block">Violator name or business name</span>
</div>
</div>
<!-- violator address -->
<div class="form-group">
<div class="">
<%= f.label :violator_address, "Violator Addresss", label: false, class: " control-label" %>
<%= f.input :violator_address, label: false, class: "form-control"%>
<span class="help-block">Do you have an address for this violator?</span>
</div>
<!-- violator email -->
<div class="">
<%= f.label :violator_email,"Violator Email", label: false, class: "control-label" %>
<%= f.input :violator_email, label: false, class: "form-control" %>
<span class="help-block">Email address of the violator.</span>
</div>
</div>
</div>
<div class="col-md-5">
<!-- Multiple Checkboxes -->
<div class="">
<%= f.label :types, "Chosee a Category", label: false, class: " control-label" %>
<%= f.association :types, as: :check_boxes, label: false, class: "form-control" %>
</div>
</div>
<!-- end Health & Safety -->
<div class="form-group">
<div class="col-md-12 ">
<%= f.submit "submit cott", label: false, class: "btn btn-block btn-primary btn-large" %>
</div>
</div>
</div
</fieldset>
<% end %>
答案 0 :(得分:2)
问题在于您的Cotts
控制器的创建方法,您没有通过构建对象的参数。这就是为什么你的对象试图保存为空白,并且它返回验证错误。通过下面强大的参数:
def create
@cott = current_user.cotts.build(cott_params) if logged_in?
respond_to do |format|
if @cott.save
format.html { redirect_to @cott, notice: 'Cott was successfully created.' }
format.json { render :show, status: :created, location: @cott }
else
format.html { render :new }
format.json { render json: @cott.errors, status: :unprocessable_entity }
end
end
end
希望它会有所帮助。让我知道它是否有效。感谢