Ruby on rails 嵌套形式(Railscast#196)
我知道有些铁路司机可能已经老了,但不应该被修改。我正在努力克服嵌套形式,但我得到的大多数答案是使用插件。因此,我尝试从头开始创建Railcast,但出现了一个异常。我想知道Railcasts 196的更新版本今天有哪些代码是正确的 这是我的代码,可能是我犯的一个愚蠢的错误 调查模型Ruby on rails 嵌套形式(Railscast#196),ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,我知道有些铁路司机可能已经老了,但不应该被修改。我正在努力克服嵌套形式,但我得到的大多数答案是使用插件。因此,我尝试从头开始创建Railcast,但出现了一个异常。我想知道Railcasts 196的更新版本今天有哪些代码是正确的 这是我的代码,可能是我犯的一个愚蠢的错误 调查模型 class Survey < ActiveRecord::Base has_many :questions, :dependent => :destroy accepts_nested_attri
class Survey < ActiveRecord::Base
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions , :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
attr_accessible :name, :questions_attributes
end
这里是新的错误
SyntaxError in Surveys#new
Showing /home/jean/rail/surveysays/app/views/surveys/_form.html.erb where line #22 raised:
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:22: syntax error, unexpected keyword_class, expecting keyword_do or '{' or '('
@output_buffer.safe_concat(' <div class="actions">
^
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:24: unterminated regexp meets end of file
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:24: syntax error, unexpected $end, expecting keyword_end
调查中的SyntaxError#新增
显示/home/jean/rail/surveysays/app/views/surveys/_form.html.erb,其中第22行出现:
/home/jean/rail/surveysay/app/views/surveys/_form.html.erb:22:语法错误,意外的关键字\u类,应为关键字\u do或“{”或“(”
@输出缓冲区安全连接('
^
/home/jean/rail/surveysay/app/views/surveys/_form.html.erb:24:unterminated regexp符合文件末尾
/home/jean/rail/surveysay/app/views/surveys/_form.html.erb:24:语法错误,意外$end,应为关键字_end
向问题模型添加有许多:答案
,并将答案模型更改为属于:问题
它解决了关联错误,但是我有一个缺少结束标记的错误我刚刚添加了表单,它的say in div class=“”你错过了一段结束语:谢谢你,至少这是有效的,现在我需要玩它,但是基本的嵌套形式确实有效,它让我的道德感提高了,非常感谢!!!!!!
class Answer < ActiveRecord::Base
belongs_to :questions
attr_accessible :content, :question_id
end
<p id="notice"><%= notice %></p>
<div>Name:</div>
<div><%=@survey.name%></div>
<% for question in @survey.questions %>
<div><%=h question.content%></div>
<% end %>
<%= link_to 'Edit', edit_survey_path(@survey) %> |
<%= link_to 'Back', surveys_path %>
<%= form_for(@survey) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<%= f.fields_for :questions do |bf|%>
<% render 'question_fields, :f => bf %>
<% end %>
***<div class="actions">***
<%= f.submit %>
</div>
<% end %>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows=> 3 %><br />
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Question"%><br />
<%= f.fields_for :answer do |form| %>
<%= render 'answer_fields', :f => form %>
<% end %>
<%= f.label :content, "Answer" %>
<%= f.text_field :content %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove" %>
class SurveysController < ApplicationController
# GET /surveys
# GET /surveys.json
def index
@surveys = Survey.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @surveys }
end
end
# GET /surveys/1
# GET /surveys/1.json
def show
@survey = Survey.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @survey }
end
end
# GET /surveys/new
# GET /surveys/new.json
def new
@survey = Survey.new
3.times {@survey.questions.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @survey }
end
end
# GET /surveys/1/edit
def edit
@survey = Survey.find(params[:id])
end
# POST /surveys
# POST /surveys.json
def create
@survey = Survey.new(params[:survey])
respond_to do |format|
if @survey.save
format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
format.json { render json: @survey, status: :created, location: @survey }
else
format.html { render action: "new" }
format.json { render json: @survey.errors, status: :unprocessable_entity }
end
end
end
# PUT /surveys/1
# PUT /surveys/1.json
def update
@survey = Survey.find(params[:id])
respond_to do |format|
if @survey.update_attributes(params[:survey])
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 = Survey.find(params[:id])
@survey.destroy
respond_to do |format|
format.html { redirect_to surveys_url }
format.json { head :no_content }
end
end
end
ArgumentError in SurveysController#create
No association found for name `answers'. Has it been defined yet?
SyntaxError in Surveys#new
Showing /home/jean/rail/surveysays/app/views/surveys/_form.html.erb where line #22 raised:
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:22: syntax error, unexpected keyword_class, expecting keyword_do or '{' or '('
@output_buffer.safe_concat(' <div class="actions">
^
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:24: unterminated regexp meets end of file
/home/jean/rail/surveysays/app/views/surveys/_form.html.erb:24: syntax error, unexpected $end, expecting keyword_end