我有一个使用seed.rb 从.txt 文件加载的俱乐部和活动列表(即乐队或棒球)。我想在我的视图中创建一个复选框列表,用户可以在其中检查他/她参与的任何活动。我在为活动列表创建迭代时遇到问题,并且收到“nil:NilClass 的未定义方法‘每个’”错误。我的模型称为活动。
我的控制器有 @activities = Activity.all 和
我的观点有 <% @activities.each 做 |a| %>
但是那条线不起作用。Activity 模型有一个名为 :name 的字符串属性,如果有帮助的话。有人有想法我可以试试吗?谢谢!
路线。rb
CollegeConnection::Application.routes.draw do
devise_for :users
resources :quizzes
resources :users
resources :majors
resources :activities
root :to => "users#welcome"
end
quiz controller:
class QuizzesController < ApplicationController
before_filter :authenticate_user!
# GET /quizzes
# GET /quizzes.json
def index
@quizzes = Quiz.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @quizzes }
end
end
# GET /quizzes/1
# GET /quizzes/1.json
def show
@quiz = Quiz.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @quiz }
end
end
# GET /quizzes/new
# GET /quizzes/new.json
def new
@quiz = Quiz.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @quiz }
end
end
# GET /quizzes/1/edit
def edit
@quiz = current_user.quizzes.build(params[:id])
end
# POST /quizzes
# POST /quizzes.json
def create
@quiz = Quiz.new(params[:quiz])
@quiz.userName=current_user.name
@activities = Activity.all
respond_to do |format|
if @quiz.save
format.html { redirect_to @quiz, notice: 'Thank You for Taking the Quiz!.' }
format.json { render json: @quiz, status: :created, location: @quiz }
else
format.html { render action: "new" }
format.json { render json: @quiz.errors, status: :unprocessable_entity }
end
end
end
# PUT /quizzes/1
# PUT /quizzes/1.json
def update
@quiz = Quiz.find(params[:id])
respond_to do |format|
if @quiz.update_attributes(params[:quiz])
format.html { redirect_to @quiz, notice: 'Quiz was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @quiz.errors, status: :unprocessable_entity }
end
end
end
# DELETE /quizzes/1
# DELETE /quizzes/1.json
def destroy
@quiz = Quiz.find(params[:id])
@quiz.destroy
respond_to do |format|
format.html { redirect_to quizzes_url }
format.json { head :ok }
end
end
结尾
_form.html.erb 测验视图
<%= form_for(@quiz, html: {class: "nice"}) do |f| %>
<% if @quiz.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@quiz.errors.count, "error") %>
prohibited this quiz from being saved:
</h2>
<ul>
<% @quiz.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :q1, "Current/Possible Major" %><br />
<%= f.collection_select(:q1, Major.all, :name, :name) %>
</div>
<div class="field">
<%= f.label :q2, "Activities" %><br />
<%= f.collection_select(:q2, Activity.all, :name, :name) %>
</div>
<% @activities.each do |a| %>
<%= a.check_box :active %>
<%= a.submit "Update" %>
<% end %>
<div class="field">
<%= f.label :q3 %><br />
<%= f.text_area :q3, class: 'input-text' %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
好的,所以我找到了一个部分解决方案:
<div class="field">
<%= f.label :q2, "Activities" %><br />
<% for activity in Activity.find(:all) %>
<div>
<%= check_box_tag(:q2, activity.name) %> <%=activity.name %>
</div>
<% end %>
</div>
它列出了所有旁边带有名称的复选框,但是它没有将值设置为 q2 的答案(问题二的 quiz 属性)我认为我对复选框标签做错了。我试过 f.check_box 但它也不起作用。有什么帮助吗?
错误:
NoMethodError in Quizzes#new
Showing /Users/Daniel/Documents/cis196/CollegeConnection/app/views/quizzes/_form.html.erb
where line #24 is raised
undefined method `merge' for "Debate\n":String
Extracted source (around line #24):
21: <%= f.label :q2, "Activities" %><br />
22: <% for activity in Activity.find(:all) %>
23: <div>
24: <%= f.check_box(:q2, activity.name) %> <%=activity.name %>
25: </div>
26: <% end %>
27: </div>
Trace of template inclusion: app/views/quizzes/new.html.erb
Rails.root: /Users/Daniel/Documents/cis196/CollegeConnection
Application Trace | Framework Trace | Full Trace
app/views/quizzes/_form.html.erb:24:in `block (2 levels) in _app_views_quizzes__form_html_erb__435461842904466959_70320063188680'
app/views/quizzes/_form.html.erb:22:in each'
app/views/quizzes/_form.html.erb:22:in
block in _app_views_quizzes__form_html_erb__435461842904466959_70320063188680' app/views/quizzes/_form.html.erb:1:in _app_views_quizzes__form_html_erb__435461842904466959_70320063188680'
app/views/quizzes/new.html.erb:6:in
_app_views_quizzes_new_html_erb__735256988922435877_70320063679640' app/controllers/quizzes_controller.rb:31:in `new'要求
参数:
无 显示会话转储
显示环境转储
回复
标题:
没有任何
测验模型
Class Quiz < ActiveRecord::Base
belongs_to :user
validates :q1, :presence => true
validates :q2, :presence => true
validates :q3, :presence => true
serialize :q2
结尾