Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 以嵌套形式设置两个父对象的ID/显示两次_Ruby On Rails - Fatal编程技术网

Ruby on rails 以嵌套形式设置两个父对象的ID/显示两次

Ruby on rails 以嵌套形式设置两个父对象的ID/显示两次,ruby-on-rails,Ruby On Rails,我有5个模型 用户、作业、应用程序、问题和答案 Jobs has many questions Jobs and Users are associated through Applications Both Questions and Applications has_many answers. 我正在尝试让一个应用程序创建操作- 将用户与特定作业关联 允许用户回答特定作业的问题 现在,我正在让它工作,但它显示问题和答案两次 即视图显示为-> This is question one Tex

我有5个模型

用户、作业、应用程序、问题和答案

Jobs has many questions 
Jobs and Users are associated through Applications
Both Questions and Applications has_many answers.
我正在尝试让一个应用程序创建操作-

将用户与特定作业关联 允许用户回答特定作业的问题 现在,我正在让它工作,但它显示问题和答案两次

即视图显示为->

This is question one
Text field for question one
This is question two
Text field for question two 
This is question one
Text field for question one
This is question two
Text field for question two 
这就是我的ApplicationNew视图的外观->

<% provide(:title, " Apply to this job") %>

<%= form_for [@job, @application]  do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

        <% @job.questions.each do |question| %>

             <%= f.fields_for :answers do |question_field| %>
                    <%= question_field.label :content, question.content %>
                    <%= question_field.text_area :content %>
                    <%= question_field.hidden_field :question_id,  :value => question.id  %>

             <% end %>
        <% end %>


   <%= f.submit "Submit the application", class: "button" %>
<% end %>
这是我的应用程序控制器->

class ApplicationsController < ApplicationController

  before_filter :set_user_and_job


  def new 
   job = params[:job_id]
   @application = Application.build(job)

  end

  def create
    @application = Application.new(application_params)
    @application.save

    redirect_to root_url, :notice => "You have now applied!"

  end

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

    @answers = []

    @job.questions.each do |question|
      @application.answers.each do |answer|
        @answers << answer if answer.question_id == question.id
      end
    end

  end

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

    @application.update_attributes(application_params)
    redirect_to root_url, :notice => "You have updated your application!"

  end



  def destroy
    Application.find(params[:id]).destroy
    flash[:success] = "Application Deleted."
    redirect_to root_url 
  end 

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

    @answers = []

    @job.questions.each do |question|
      @application.answers.each do |answer|
        @answers << answer if answer.question_id == question.id
      end
    end

  end   


private

  def set_user_and_job
      @user = current_user
      @job = Job.find(params[:job_id])
  end

def application_params
   params.require(:application).permit(:job_id, :user_id, 
     answers_attributes:[:id, :question_id, :content]).merge(user_id: current_user.id,
     job_id: params[:job_id])
end


end
这是我的应用程序模型

# == Schema Information
#
# Table name: applications
#
#  id         :integer          not null, primary key
#  user_id    :integer
#  job_id     :integer
#  created_at :datetime
#  updated_at :datetime
#

class Application < ActiveRecord::Base
    belongs_to :job
    belongs_to :user
    validates :job_id, presence: true 
    validates :user_id, presence: true 
    has_many :answers
    accepts_nested_attributes_for :answers, :allow_destroy => true


    def self.build(job_id) 
        application = self.new

        job = Job.find(job_id)
        job.questions.count.times do
         application.answers.build
        end

    application
    end

end
这是我的编辑视图功能->

<% provide(:title, " Edit this application") %>
<%= form_for [@job, @application] do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

         <%= f.fields_for :answers do |question_field| %>
              <%= question_field.label :content, question_field.object.question.content %>
              <%= question_field.text_area :content %>
         <% end %>

   <%= f.submit "Submit the application", class: "button" %>
<% end %>
我认为发生这种情况的原因是我在运行一个双循环,但我不确定如何为每个答案获取问题id和问题内容

你觉得怎么样

-

下面是运行此表单时参数的外观->

<%= form_for [@job, @application]  do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

      <% @job.questions.each do |question| %>

         <%= f.fields_for :answers, question do |question_field| %>
                <%= question_field.label :content, question.content %>
                <%= question_field.text_area :content %>
                <%= question_field.hidden_field :question_id,  :value => question.id  %>

         <% end %>
    <% end %>

   <%= f.submit "Submit the application", class: "button" %>
<% end %>




Started POST "/jobs/3/applications" for 127.0.0.1 at 2013-12-30 14:26:35 +0400
Processing by ApplicationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"+gbcaJJjQZ2GfkWiKcOmSf58hf/GEnWonmGrVe1p3ZI=", "application"=>{"question"=>{"content"=>"Sample answer 2", "question_id"=>"6"}}, "commit"=>"Submit the application", "job_id"=>"3"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = '69f212609955f368c0f17873b5dce9f506bd3eb7' LIMIT 1
  Job Load (0.1ms)  SELECT "jobs".* FROM "jobs" WHERE "jobs"."id" = ? LIMIT 1  [["id", "3"]]
Unpermitted parameters: question
   (0.1ms)  begin transaction
试试这个:

   <% @job.questions.each do |question| %>

         <%= f.fields_for :answers, question do |question_field| %>
                <%= question_field.label :content, question.content %>
                <%= question_field.text_area :content %>
                <%= question_field.hidden_field :question_id,  :value => question.id  %>

         <% end %>
    <% end %>
这将为您提供以下视图:

<%= form_for @application do |f| %>
     <%= f.fields_for :questions do |q| %>
           <%= q.label :content %>
           <%= q.fields_for :answer do |a| %>
                    <%= a.text_area :content %>
           <% end %>
     <% end %>
<% end %>

是否使用“新建”操作同时显示编辑表单和新建表单?我这么问是因为我在你的ApplicationController中没有看到任何“编辑”操作。嗨,Gjardon,我从这里删除了它,以便更容易阅读,不过一秒钟后,我会直接进入并更新它:嗨,Rich,这里发生了一些奇怪的事情。当我尝试这样做时,它最终会导致NoRecordFound错误。我认为原因是Rails认为答案的id就是问题的id。如果我删除了id,尽管这样做了,它会工作,但会破坏编辑表单。一个解决方案可能是有两个不同的参数,一个用于编辑,一个用于新建,但我不确定这是否是最佳解决方案。你有什么建议?嗯,老实说,我得再考虑一下!你是说你认为Rails把问题_idparam错当成了答案的id?还有一件事可能会导致这个问题。您正在新建对象,但“编辑”呢?也许这与伊塞·斯捷潘帕鲁纳什维利有关,谢谢你的情人们。看着他们,你似乎是对的——它传递的是问题对象,而不是答案。我认为如果我们能实施一个有很多:通过,它会解决你的问题;但要想把它做好需要一些思考
<%= form_for @application do |f| %>
     <%= f.fields_for :questions do |q| %>
           <%= q.label :content %>
           <%= q.fields_for :answer do |a| %>
                    <%= a.text_area :content %>
           <% end %>
     <% end %>
<% end %>
#app/controllers/applications_controller.rb
def new
   job = params[:job_id]
   @application = Application.build(job)
end

private
def application_params
   params.require(:application).permit(:job_id, :user_id, 
     questions_attributes: [answer_attributes:[:content]]).merge(user_id: current_user.id,
     job_id: params[:job_id])
end