Ruby 在 Rails 部分本地

Ruby on Rails Partial Locals

在我的网站上,用户可以参加和重新参加测验,这意味着我将在局部视图中使用测验代码,编辑视图和新视图都将使用该视图。

要渲染我在新视图中的部分:

<%= render partial: "quiz", locals: { url: quiz_bs_path } %>

在我的编辑视图中:

<%= render partial: "quiz", locals: { url: edit_quiz_b_path } %>

link_to 个位置来自我的佣金路线结果:

        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 = 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