Rails Has_Many:通过混乱

时间:2014-02-23 04:48:56

标签: ruby-on-rails

我正在学习基础调查应用程序,因为我正在学习rails。我在调查之间设置了一个has_many关系作为问题(可以在多个调查中使用问题)。我一直在努力为调查增加一个问题。知道在创建新问题时创建新的调查问题需要做些什么(从而将问题添加到调查中)?我能够在控制台中完成它,但我正在努力将其转换为控制器/视图/参数 - 如果你知道任何关于那些我想要检查它们的好文档(但到目前为止还没有找到)多)。

当我尝试使用我发送给Question控制器的params中的:survey_id来分配我的@survey变量时似乎出错了。

我真的很感谢你的帮助!

Question.rb:

class Question < ActiveRecord::Base
  has_many :answers, dependent: :delete_all
  validates :title, presence: true
  has_many :surveytizations
  has_many :surveys, :through => :surveytizations
end

Survey.rb

class Survey < ActiveRecord::Base
  has_many :surveytizations
  has_many :questions, :through => :surveytizations
end

Surveytization.rb:

class Surveytization < ActiveRecord::Base
  has_many :surveys
  has_many :questions
  validates :survey_id, presence: true
  validates :question_id, presence:true
end

SurveyController.rb:

class SurveysController < ApplicationController
  before_action :set_survey, only: [:show, :edit, :update, :destroy]
  before_action :set_question

  # GET /surveys
  # GET /surveys.json
  def index
    @surveys = Survey.all
  end

  # GET /surveys/1
  # GET /surveys/1.json
  def show
  end

  # GET /surveys/new
  def new
    @survey = Survey.new
  end

  # GET /surveys/1/edit
  def edit
  end

  # 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

  # PATCH/PUT /surveys/1
  # PATCH/PUT /surveys/1.json
  def update
    respond_to do |format|
      if @survey.update(survey_params)
        format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /surveys/1
  # DELETE /surveys/1.json
  def destroy
    @survey.destroy
    respond_to do |format|
      format.html { redirect_to surveys_url }
      format.json { head :no_content }
    end
  end

  def add_question(question)
    surveytizations.create!(question_id: question.id)
  end

  def remove_question(question)
    surveytizations.find_by(question_id: question.id).destroy
  end

  def find_question(question)
    @question = surveytizations.find_by(question_id: question.id)
  end

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

    def set_question
      @question = Question.new
    end

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

调查show.html.erb:

<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @survey.title %>
</p>

<%= link_to 'Edit', edit_survey_path(@survey) %> |
<%= link_to 'Back', surveys_path %>

<%= link_to "Add Question", new_question_path(:survey_id => @survey.id)%>

QuestionController:

class QuestionsController < ApplicationController
  before_action :set_question, only: [:show, :edit, :update, :destroy]

  # GET /questions
  # GET /questions.json
  def index
    @questions = Question.all
  end

  # GET /questions/1
  # GET /questions/1.json
  def show
    @answers = @question.answers
  end

  # GET /questions/new
  def new
    @question = Question.new
    @survey = Survey.find(:survey_id)
  end

  # GET /questions/1/edit
  def edit
  end

  # POST /questions
  # POST /questions.json
  def create
    @question = Question.new(question_params)

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

  # PATCH/PUT /questions/1
  # PATCH/PUT /questions/1.json
  def update
    respond_to do |format|
      if @question.update(question_params)
        format.html { redirect_to @question, notice: 'Question was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /questions/1
  # DELETE /questions/1.json
  def destroy
    @question.destroy
    respond_to do |format|
      format.html { redirect_to questions_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def question_params
      params.require(:question).permit(:title, :single_response, :surveytization)
    end
end

1 个答案:

答案 0 :(得分:2)

一个问题是你的联接关系应该有belongs_to而不是has_many,以通过工作来获得has_many:

class Surveytization < ActiveRecord::Base
  belongs_to :survey
  belongs_to :question
  validates :survey_id, presence: true
  validates :question_id, presence:true
end

注意:survey和:问题在belongs_to

中是单数名称

要向调查添加问题,您可以

# create new question or find existing question and store it in @question
@question 

# get the survey into @survey
@survey

@survey.questions << @question

这也将神奇地创建调查。现在这个问题将属于该调查。 你甚至不必打电话给@ survey.save!后。