【问题标题】:Rails 4 - can't connect to the table on the database on rails 4Rails 4 - 无法连接到 rails 4 上的数据库表
【发布时间】:2014-05-26 23:33:12
【问题描述】:

我正在学习如何使用嵌套属性的教程,我正在做一个调查,但出现问题,因为我无法在问题表中插入任何数据

这就是我所做的:

问题模型

class Question < ActiveRecord::Base
    belongs_to :survey
    has_many :answers, dependent: :destroy
end

调查模型

class Survey < ActiveRecord::Base
    has_many :questions, dependent: :destroy
    accepts_nested_attributes_for :questions , :reject_if => lambda { |a| a[:content].blank? }
end

意见/调查/_form.erb

<%= form_for(@survey) do |f| %>


 <div class="field">
  <%= f.label :name %><br>
  <%= f.text_field :name %>
 </div>



  <%= f.fields_for :questions do |ff| %>
  <div class="field">
  <%= ff.label :content , "question"%><br>
  <%= ff.text_field :content %>
  </div>
  <% end %> 

<div class="actions">
  <%= f.submit %>
 </div>

<% end %>

调查员

# GET /surveys/new
  def new
@survey = Survey.new
3.times { @survey.questions.new }
end

现在,当我单击提交时,仅保存调查名称,但没有问题! 像这样:

2.1.1 :023 > Question.all
Question Load (0.3ms)  SELECT "questions".* FROM "questions"
=> #<ActiveRecord::Relation []>

创建动作

# POST /surveys
# POST /surveys.json
def create
@survey = Survey.new(survey_params)

respond_to do |format|
  if @survey.save
    format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
    format.json { render action: 'show', status: :created, location: @survey }
  else
    format.html { render action: 'new' }
    format.json { render json: @survey.errors, status: :unprocessable_entity }
  end
end
end

 private
# Use callbacks to share common setup or constraints between actions.
def set_survey
  @survey = Survey.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def survey_params
  params.require(:survey).permit(:name)
end
end

我添加了另一个模型 [answer]

class Answer < ActiveRecord::Base
  belongs_to :question
end

将问题模型更改为此

class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, allow_destroy: true
end

我将调查更改为

# GET /surveys/new
def new
@survey = Survey.new
1.times do
question = @ survey.questions.build
2.times { question.answers.build }
end
end 

现在和以前一样的问题的答案 我应该如何处理强参数

应该是这样的

def survey_params
  params.require(:survey).permit(:name, answers_attributes: [:content]) 
end

【问题讨论】:

  • 您可以发布“创建”操作吗?如果你使用强参数,还有“defsurvey_params”的定义
  • 你试过没有 3.times 吗?
  • @StavrosSouvatzis 我添加了创建操作
  • @MohamedElMahallawy 同样的问题,在此感谢 :)
  • @StavrosSouvatzis 你是对的,“defsurvey_params”丢失了......非常感谢:)

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


【解决方案1】:

听起来您的调查的允许参数中没有列出questions_attributes

确保您的 survey_params 包含以下问题属性:

private

def survey_params
  params.require(:survey).permit(:name, questions_attributes: [:content]) 
end

如果您有其他调查属性或问题属性,您也需要在此处允许它们。

Strong parameters with nested attributes documentation

【讨论】:

  • 我添加了另一个名为 answer 的模型,我遇到了同样的问题,我应该如何处理它?我把代码放在问题中
  • 它工作了 :) 我把它改成了这个 defsurvey_params params.require(:survey).permit(:name, questions_attributes:[:content, :id, :survey_id, answers_attributes:[:content, :id, :questions_id] ]) 结束
猜你喜欢
  • 2015-01-05
  • 1970-01-01
  • 2014-09-14
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 2021-11-23
相关资源
最近更新 更多