我建立具有幾個測驗具有完全相同的結構的Rails應用程序:Ruby on Rails的自動變量名稱(複數VS奇)
- 一個名稱,如
@quiz_bf
或@quiz_bs
new.html.erb
和edit.html.erb
意見對於每個測驗- 的部分
_quiz.html.erb
存儲用於每個測驗 - 一種用於每個單獨測驗模型和控制器的實際測驗問題
- 兩個地方(主導航和用戶個人資料頁面)持有指向
new
視圖的鏈接(或者如果人們已經參加了測驗,由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
,但你可以在我的耙路看到(如下圖所示),我的白癡變量的複數/奇異問題名字使得很難看到實際命名的是什麼。任何擁有比我的經驗豐富的紅寶石眼睛的人都能告訴我我需要改變什麼?
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
你可以發佈你的用戶模型嗎? – Pavan
這裏有更大的問題。你不應該有幾十個具有相同名稱和不同數字的字段,所有'@quiz_bf.bf01'變量都不應該這樣設置。您應該將這些存儲爲相關記錄,或者簡單地序列化一個數組,以便您可以在這些字段中循環*。 – meagar
你的用戶模型有'has_many:quiz_bfs'嗎?如果不是,那麼'@ user.quiz_bfs'將是未定義的。 – SteveTurczyn