2016-04-29 43 views
0

我建立具有幾個測驗具有完全相同的結構的Rails應用程序:Ruby on Rails的自動變量名稱(複數VS奇)

  • 一個名稱,如@quiz_bf@quiz_bs
  • new.html.erbedit.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 
+0

你可以發佈你的用戶模型嗎? – Pavan

+1

這裏有更大的問題。你不應該有幾十個具有相同名稱和不同數字的字段,所有'@quiz_bf.bf01'變量都不應該這樣設置。您應該將這些存儲爲相關記錄,或者簡單地序列化一個數組,以便您可以在這些字段中循環*。 – meagar

+0

你的用戶模型有'has_many:quiz_bfs'嗎?如果不是,那麼'@ user.quiz_bfs'將是未定義的。 – SteveTurczyn

回答

1

未定義的方法錯誤

與未定義的方法這個問題可能有與呼叫是一個多調用(quiz_bfs),它希望你有一個的has_many關聯,而你的模型只定義了一個做HAS_ONE協會:

has_one :quiz_bs 
has_one :quiz_bf 

無論是)使用quiz_bfs是筆誤,應該是quiz_bf,b)的HAS_ONE不正確,或c)你不需要在人的呼叫current_user.quiz_bfs l爲@quiz_bf分配一個值。在不知道更多細節的情況下很難分辨,但看起來您可以刪除current_user.quiz_bfs。航線上

雖然它不是這個問題,

獎金具體而言,你所提到的被視爲一個複數詞尾中的'資源的痛苦。如果您還沒有看到,可以正確命名quiz_bs路由。要做到這一點,你可以在你config/routes.rb文件中使用這種形式:

resources :quiz_bs, as: :quiz_bs 

應該給你,你想的路線助手的名字。您可以在Rails Routing from the Outside InOverriding Named Helpers部分閱讀更多內容。

您可能希望把你的控制器QuizBsController,還有,你可以使用controller:覆蓋上的資源路由定義來做到這一點。試試這個,看看它是否給你正確的控制器:

resources :quiz_bs, controller: 'quiz_bs' 

你總是可以結合這兩種路線的方法和使用這樣的:

resources :quiz_bs, controller: 'quiz_bs', as: :quiz_bs 

退房的Rails Routing from the Outside In更多信息,Specifying a Controller to Use部分,使用細節。