使用另一个字段轨道向嵌套字段添加响应

时间:2017-04-05 00:43:27

标签: ruby-on-rails ruby nested-attributes

我有一个主表,它包含一个应用程序表和一个带有输入问题的测试嵌套表。我正在尝试添加另一个表来回答嵌套表提出的问题。

我的问题是它不会在答案表中保存回复。

Explanation of what the heck!

这是我的代码:

answers.controller.rb

  class AnswersController < ApplicationController
    def create
        @application = Application.find(params[:application_id])
        @answer = @application.answers.create(params[:answer].permit(:reply))
        @answer.user_id = current_user.id if current_user
        @answer.save

        if @answer.save
            redirect_to application_path(@application)
        else
            render 'applications/new'
        end
    end
 end

applications.controller.rb

class ApplicationsController < ApplicationController
    before_action :find_application, only: [:show, :edit, :update, :destroy]
    before_action :authenticate_user!, except: [:index, :show   ]

    def index
        @applications = Application.all.order("created_at DESC").limit(3)
    end

    def new 
      @application = current_user.applications.build
      @application.tests.build
    end

    def create
        @application = current_user.applications.build(application_params)

        if @application.save
            redirect_to @application
        else
            render 'new'
        end
    end

    def show
    end

    def edit
    end

    def update
        if @application.update(application_params)
            redirect_to @application
        else
            render 'edit'
        end
    end


    def destroy
        @application.destroy
        redirect_to root_path
    end

    private

    def find_application
        @application = Application.find(params[:id])
    end

    def application_params
        params.require(:application).permit(:name, :description, tests_attributes: [:id, :question, :_destroy])
    end

    def after_sign_out_path_for(resource_or_scope)
    redirect_to root_path
    end
end

型号:

answer.rb

class Answer < ApplicationRecord
  belongs_to :test
  belongs_to :application
  belongs_to :user
end

application.rb中

class Application < ApplicationRecord
    belongs_to :user

    has_many :tests
    has_many :answers

    accepts_nested_attributes_for :tests, reject_if: :all_blank, allow_destroy: true

    validates :name, :description, presence: true
end

test.rb

class Test < ApplicationRecord
  belongs_to :application, required: false
  validates :question, presence: true
  has_many :answers
end

user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :applications
  has_many :answers
end

answers.form.html.erb

<%= form_for([@application, @application.answers.build]) do |f| %>
    <div class="field">
        <%= f.text_field :reply %>
        <%= f.submit class: "btn btn-default" %>
    </div>
<% end %>

applications.html.erb

<h1><%= @application.name %></h1>
<h4><i><%= @application.description %></i></h4>
<h3>Questions</h3>
<ul>
    <% @application.tests.each do |test| %>
        <li>
            <%= test.question + "?" %>
        </li>
    <% end %>
</ul>

<h2><%= @application.answers.count %></h2>
<%= render @application.answers %>

<%= render 'answers/form' %>

<h6><%= link_to "Back", root_path %></h6>

create_answers migration:

class CreateAnswers < ActiveRecord::Migration[5.0]
  def change
    create_table :answers do |t|
      t.string :reply
      t.references :test, foreign_key: true
      t.references :application, foreign_key: true
      t.references :user, foreign_key: true

      t.timestamps
    end
  end
end

我应该只关联GitHub回购。我很可怕。我需要朋友,我可以问这些东西,而不是感觉像dum dum。应该毕业了。

0 个答案:

没有答案