【发布时间】:2016-08-24 18:47:21
【问题描述】:
我正在构建一个 Rails 应用程序,其中包含多个结构完全相同的测验:
- 诸如
@quiz_bf或@quiz_bs之类的名称 -
new.html.erb和edit.html.erb每个测验的观看次数 - 部分
_quiz.html.erb存储每个测验的实际测验问题 - 每个独立测验的模型和控制器
- 两个地方(主导航和用户个人资料页面)保存指向
new视图的链接(如果人们已经参加了测验,则指向edit视图,由 erb if/else 语句决定)每个测验
我成功地设置了第一个测验,但由于我是 Ruby 新手,我犯了一个(可怕的)错误,即为测验的单数版本设置了一个以 -s (quiz_bs) 结尾的变量名.这对 Ruby 的单数和复数命名约定造成了严重破坏。
我目前已经设置了我的第二个测验 (quiz_bf) 的代码,但是在我的控制器中的新测验的定义上出现了一个无方法错误 undefined method 'quiz_bfs' for #<User:0x007fb9a3247f98>。
这是我的控制器:
class QuizBfController < ApplicationController
before_action :require_sign_in
def show
@quiz_bf = QuizBf.find(params[:id])
end
def new
@quiz_bf = current_user.quiz_bfs || current_user.build_quiz_bfs
end
def create
@quiz_bf = QuizBf.new
@quiz_bf.bf01 = params[:quiz_bfs][:bf01]
@quiz_bf.bf02 = params[:quiz_bfs][:bf02]
@quiz_bf.bf03 = params[:quiz_bfs][:bf03]
@quiz_bf.bf04 = params[:quiz_bfs][:bf04]
@quiz_bf.bf05 = params[:quiz_bfs][:bf05]
@quiz_bf.bf06 = params[:quiz_bfs][:bf06]
@quiz_bf.bf07 = params[:quiz_bfs][:bf07]
@quiz_bf.bf08 = params[:quiz_bfs][:bf08]
@quiz_bf.bf09 = params[:quiz_bfs][:bf09]
@quiz_bf.bf10 = params[:quiz_bfs][:bf10]
@quiz_bf.user = current_user
if @quiz_bf.save
flash[:notice] = "Quiz results saved successfully."
redirect_to user_path(current_user)
else
flash[:alert] = "Sorry, your quiz results failed to save."
redirect_to welcome_index_path
end
end
def edit
@quiz_bf = QuizBf.find(params[:id])
end
def update
@quiz_bf = QuizBf.find(params[:id])
@quiz_bf.assign_attributes(quiz_bfs_params)
if @quiz_bf.save
flash[:notice] = "Post was updated successfully."
redirect_to user_path(current_user)
else
flash.now[:alert] = "There was an error saving the post. Please try again."
redirect_to welcome_index_path
end
end
private
def quiz_bfs_params
params.require(:quiz_bfs).permit(:bf01, :bf02, :bf03, :bf04, :bf05, :bf06, :bf07, :bf08, :bf09, :bf10)
end
end
这是我的模型(用户has_one :quiz_bf):
class QuizBf < ActiveRecord::Base
before_save :set_bfcode
def set_bfcode
self.bfcode = "#{self.bf01}#{self.bf02}#{self.bf03}-#{self.bf04}#{self.bf05}#{self.bf06}-#{self.bf07}#{self.bf08}#{self.bf09}#{self.bf10}"
end
belongs_to :user
validates :user, presence: true
end
这是我的用户模型:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, length: { minimum: 1, maximum: 100 }, presence: true
validates :password, presence: true, length: { minimum: 6 }, unless: :password_digest
validates :password, length: { minimum: 6 }, allow_blank: true
validates :email,
presence: true,
uniqueness: { case_sensitive: false },
length: { minimum: 3, maximum: 254 }
has_secure_password
has_one :quiz_bs
has_one :quiz_bf
结束
以下是我的部分测验的相关部分:
<%= form_for @quiz_bf do |f| %>
...
<%= f.submit "Submit Answers" %>
这是我从new 视图链接的方式:
<%= render partial: "quiz", locals: { url: quiz_bfs_path, method: :post } %>
还有我的edit 观点:
<%= render "quiz", url: quiz_bf_path(@quiz_bf), method: :put %>
(最后)这是我从application 视图链接到它的方式:
<% if current_user.quiz_bfs == nil? %>
<%= link_to "Body Flexibility Quiz", quiz_bf_path %>
<% else %>
<%= link_to "Body Flexibility Quiz ✓", edit_quiz_bf_path(current_user.quiz_bfs) %>
<% end %>
还有我的用户show页面:
<% if @user.quiz_bfs == nil %>
<p><%= link_to "Test Your Body Flexibility", new_quiz_bf_path %></p>
<% else %>
<h3><%= @user.quiz_bfs.bfcode %></h3>
<p><%= link_to "Retest Results", edit_quiz_bf_path(@user.quiz_bfs) %></p>
<% end %>
我知道这段代码在quiz_bs 上成功运行,但正如您在我的 rake 路由中看到的(如下所示),我愚蠢的变量名称的复数/单数问题使得很难看到实际命名为什么。谁能比我更有经验的红宝石眼睛告诉我我需要改变什么?
quiz_bs GET /quiz_bs(.:format) quiz_bs#index
POST /quiz_bs(.:format) quiz_bs#create
new_quiz_b GET /quiz_bs/new(.:format) quiz_bs#new
edit_quiz_b GET /quiz_bs/:id/edit(.:format) quiz_bs#edit
quiz_b GET /quiz_bs/:id(.:format) quiz_bs#show
PATCH /quiz_bs/:id(.:format) quiz_bs#update
PUT /quiz_bs/:id(.:format) quiz_bs#update
DELETE /quiz_bs/:id(.:format) quiz_bs#destroy
quiz_bf_index GET /quiz_bf(.:format) quiz_bf#index
POST /quiz_bf(.:format) quiz_bf#create
new_quiz_bf GET /quiz_bf/new(.:format) quiz_bf#new
edit_quiz_bf GET /quiz_bf/:id/edit(.:format) quiz_bf#edit
quiz_bf GET /quiz_bf/:id(.:format) quiz_bf#show
PATCH /quiz_bf/:id(.:format) quiz_bf#update
PUT /quiz_bf/:id(.:format) quiz_bf#update
DELETE /quiz_bf/:id(.:format) quiz_bf#destroy
【问题讨论】:
-
你能发布你的用户模型吗?
-
这里有更大的问题。你不应该像这样有几十个同名和不同数字的字段,你所有的
@quiz_bf.bf01变量都不应该这样设置。您应该将这些存储为相关记录,或者只是序列化一个数组,以便您可以在字段上循环。 -
你的用户模型有
has_many :quiz_bfs吗?如果不是,那么@user.quiz_bfs将是未定义的。 -
我刚刚在编辑中添加了用户模型。它
has_one :quiz_bf. -
你的模型只设置了 quiz_bs 和 quiz_bf 关联,为什么还要使用 quiz_bfs?
标签: ruby-on-rails ruby variables activerecord plural