[英]Ruby on Rails Partial Locals
在我的網站上,用戶可以參加和重新參加測驗,這意味着我將在部分視圖中使用測驗代碼,編輯視圖和新視圖都將使用該代碼。
為了渲染我在新視圖中的部分:
<%= render partial: "quiz", locals: { url: quiz_bs_path } %>
在我的編輯視圖中:
<%= render partial: "quiz", locals: { url: edit_quiz_b_path } %>
link_to
位置來自我的 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
我在我的編輯視圖中收到一個參數錯誤,說First argument in form cannot contain nil or be empty
。 調用錯誤的行在我的部分:
<%= form_for @quiz_bs, url: url, method: :post do |f| %>
我的控制器代碼是:
class QuizBsController < ApplicationController
before_action :require_sign_in
def show
@quiz_bs = QuizBs.find(params[:id])
end
def new
@quiz_bs = current_user.quiz_bs || current_user.build_quiz_bs
end
def create
@quiz_bs = QuizBs.new
@quiz_bs.bs01 = params[:quiz_bs][:bs01]
@quiz_bs.bs02 = params[:quiz_bs][:bs02]
@quiz_bs.bs03 = params[:quiz_bs][:bs03]
@quiz_bs.bs04 = params[:quiz_bs][:bs04]
@quiz_bs.bs05 = params[:quiz_bs][:bs05]
@quiz_bs.bs06 = params[:quiz_bs][:bs06]
@quiz_bs.user = current_user
if @quiz_bs.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 update
@quiz_bs = QuizBs.find(params[:quiz_bs])
@quiz_bs.assign_attributes(quiz_bs_params)
if @quiz_bs.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_bs_params
params.require(:quiz_bs).permit(:bs01, :bs02, :bs03, :bs04, :bs05, :bs06)
end
end
模型是:
class QuizBs < ActiveRecord::Base
before_save :set_bscode
def set_bscode
self.bscode = "#{self.bs01}#{self.bs02}#{self.bs03}-#{self.bs04}#{self.bs05}#{self.bs06}"
end
belongs_to :user
validates :user, presence: true
end
我哪里出錯了?
您在控制器中沒有edit
操作,因此 rails 假定它為空。 因此@quiz_bs
真的nil
添加類似的東西
def edit
@quiz_bs = QuizBs.find(params[:id])
end
同樣在您的update
操作中params[:quiz_bs]
也很可能nil
,應該更改為params[:id]
保持簡單,如果你想分配給 current_user, create
方法會為你做。
部分的:
#new.html.erb
<%= render partial: "quiz", locals: {quiz: @quiz_bs, url: :quiz_bs } %>
#edit.html.erb
<%= render partial: "quiz", locals: {quiz: @quiz_bs, url: :quiz_b } %>
形式:
#_form.html.erb
<%= form_for quiz, url: url do |f| %>
控制器:
def new
@quiz_bs = QuisBs.new
end
沒有必要在控制器中創建 RESTfull 方法,但是如果你像我看到的@quiz_bs
使用它@quiz_bs
那么你必須先初始化它然后你才能使用它,在你的新視圖中你初始化@quiz_bs
@quiz_bs = current_user.quiz_bs || current_user.build_quiz_bs
那么你正在使用它。
並且為edit
生成的routes
也需要一個id
即
edit_quiz_b GET /quiz_bs/:id/edit(.:format) quiz_bs#edit
所以你必須通過@quiz_bs
edit_quiz_b_path(@quiz_bs)
知道了?
控制器的一些更新:
before_action :assign_quiz_bs, only: [:show, :edit, :update, :destroy]
#...
def update
@quiz_bs.update(quiz_bs_params)
# .... rendering ...
end
private
def assign_quiz_bs
@quiz_bs = Quiz.find(params[:id])
end
def quiz_bs_params
params.require(:quiz_bs).permit(:bs01, :bs02, :bs03, :bs04, :bs05, :bs06)
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.