Rails 3:fields_for 在编辑视图上显示空白字段 [英] Rails 3: fields_for showing blank filed on Edit view

查看:26
本文介绍了Rails 3:fields_for 在编辑视图上显示空白字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 3 应用程序中,我在 form_for 中使用 fields_for 来创建和编辑问题和答案.但是我在 fields_for 上的编辑视图中遇到了问题的答案.当我在数据库上为 1 个问题注册了 2 个答案时,编辑视图会显示 2 个答案和一个空白字段.

这是模型代码和视图代码:

问题模型:

# 编码:utf-8课堂问题<ActiveRecord::Baseattr_accessible :description, :question_id , :research_id , :answer_id ,:answer_attributeshas_many :answer, :class_name =>"答案", :dependent =>:破坏accepts_nested_attributes_for :answer , :allow_destroy =>真的归属地:研究结尾

答案模型:

class 答案 <ActiveRecord::Base归属地:问题has_many :evaluate_answersattr_accessible :question_id, :description , :answer_attributes结尾

最后是用于新建和编辑视图的部分:

<%= form_for [:admin, @question] ,:html =>{ :class =>"形式-水平", :multipart =>真,:onSubmit =>" 返回 teste()" } 做 |f|%>.......<div id="div_respostas" class="respostas" style="margin-top: 25px; margin-left: 65px;"><div id="campo_answers"><%= hidden_​​field_tag :count_resp, 0%><p>Respostas <a href="#"><%=image_tag("admin/icons/btn_adicionar_p.jpg" ,{:onclick =>'add_answer()'}) %></</p><%= f.fields_for :answer do |a|%><div id="answer_<%= @number = @number + 1 %>"><%= a.text_field "description" %>删除:<%= a.check_box :_destroy %><a class="button_<%=@number%>"href="#"><%=image_tag("admin/icons/btn_excluir_p.jpg", {:onclick =>"remove_answer(#{@number})"}) %></a><;br/><br/>

<%end%>

一切正常,只是在编辑视图中,总是多出一个空白字段.有没有办法去除这个空白字段?

问题控制器

def 编辑@question = Question.find(params[:id])@answers = Answer.where(:question_id => @question.question_id)@数字 = 0@question.answer.build@questions = Question.all结尾

应答控制器

class AnswersController <应用控制器定义索引@answers = Answer.all结尾结尾

解决方案

这是因为您正在使用此行构建新的答案对象:

@question.answer.build

在控制器的 edit 操作中.

因此,当您访问编辑视图时,您会看到数据库中的 2 个现有答案对象以及您在上面创建的全新对象.

On my Rails 3 app, I'm using fields_for inside a form_for to create and edit questions and answers. But I'm having a problem with Edit view on fields_for for answers. When I have 2 answers registered on the DB for 1 Question, the edit view shows the 2 answers plus a blank field.

Here is the model code, and the views code:

Question Model:

# encoding: utf-8
class Question < ActiveRecord::Base
  attr_accessible :description, :question_id , :research_id , :answer_id  ,:answer_attributes
  has_many :answer, :class_name => "Answer", :dependent => :destroy 
  accepts_nested_attributes_for :answer , :allow_destroy => true
  belongs_to :research
end

Answer Model:

class Answer < ActiveRecord::Base
  belongs_to :question
  has_many :evaluate_answers
  attr_accessible :question_id, :description , :answer_attributes
end

And finally the partial used for New and Edit view:

<%= form_for [:admin, @question] ,:html => { :class => "form-horizontal", :multipart =>      true, :onSubmit => " return teste()" } do |f| %>
  ...
  ....  

            <div id="div_respostas" class="respostas" style="margin-top: 25px; margin-left: 65px;">

                <div id="campo_answers">
                    <%= hidden_field_tag :count_resp, 0%>
                    <p>Respostas  <a  href="#"><%=image_tag("admin/icons/btn_adicionar_p.jpg" ,{:onclick =>'add_answer()'}) %></a></p>

                    <%= f.fields_for :answer do |a| %>


                        <div id="answer_<%= @number = @number + 1 %>">
                            <%= a.text_field "description" %>
                            Delete: <%= a.check_box :_destroy %>
                            <a class="button_<%=@number%>" href="#"><%=image_tag("admin/icons/btn_excluir_p.jpg" , {:onclick =>"remove_answer(#{@number})"}) %></a><br /><br />
                        </div>
                    <%end%>

Everything works fine, just on edit view, there is always one more blank field rendered. Is there a way to remove this blank field?

EDIT:

Question Controller

def edit
    @question = Question.find(params[:id])
    @answers = Answer.where(:question_id => @question.question_id)
    @number = 0
    @question.answer.build
    @questions = Question.all 
 end

Answer Controller

class AnswersController < ApplicationController
  def index
    @answers = Answer.all  

  end

end

解决方案

It's because you are building a new answer object with this line:

@question.answer.build

in the edit action of your controller.

So when you visit the edit view, you see the 2 existing answer objects from the database plus the brand new one you've created above.

这篇关于Rails 3:fields_for 在编辑视图上显示空白字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆