我正在尝试在我的主要欢迎页面(我使用 HighVoltage gem 来创建它)、我的组表单中呈现部分内容,用户可以在其中创建一个新组......
到目前为止我所尝试的给了我以下错误......
表单中的第一个参数不能包含 nil 或为空
= form_for @group do |f|
.fieldset
- if @group.errors.any?
.error_messages
我的路线是这样配置的
Giraffle::Application.routes.draw do
get 'sign_up', to: 'groups#new', as: 'sign_up'
get 'sign_in', to: 'sessions#new', as: 'sign_in'
delete 'sign_out', to: 'sessions#destroy', as: 'sign_out'
resources :sessions
resources :members
resources :groups
resources :events
resources :event_sets
root :to => 'high_voltage/pages#show', id: 'welcome'
end
因此,当我尝试加载主页时,直接进入欢迎页面,并因上述错误而崩溃
我想我知道问题是什么......但我不知道如何解决它。我正在尝试呈现表单,但由于“组”viarable 从未初始化它会引发此错误。
我的代码...
意见/页面/welcome.html.slim
row id="div"
.small-4 id="innerDiv"
.row
.small-4.columns align="center"
img src="groupIcon.png" id="mainImg"
.large-6.large-offset-2.columns
h1 Sign Up
= render :partial => '/groups/form'
意见/组/_form.html.slim
= form_for @group do |f|
.fieldset
- if @group.errors.any?
.error_messages
h2 Form is invalid
ul
- @group.errors.full_messages.each do |message|
li= message
.row
.small-12.columns
= f.text_field :name, placeholder: "Name"
.row
.small-12.columns
= f.text_field :group_id, placeholder: "Group"
.row
.small-12.columns
= f.password_field :password, placeholder: "Password"
.row
.small-12.columns
= f.password_field :password_confirmation, placeholder: "Confirm Password"
.row
.small-3.columns
.actions= f.submit 'Sign Up', class: 'button radius'
编辑
控制器/groups_controller.rb
class GroupsController < ApplicationController
load_and_authorize_resource
before_action :set_group, only: [:edit, :update, :destroy]
before_action :authorize, except: [:new, :create]
def new
@group = Group.new
end
def edit
end
def create
@group = Group.new(group_params)
if @group.save
redirect_to root_url, notice: 'Signed Up!'
else
render 'new'
end
end
def update
if @group.update(group_params)
redirect_to root_url, notice: 'Group Info was successfully updated.'
else
render action: 'edit'
end
end
private
def set_group
@group = Group.find(params[:id])
end
def group_params
params.require(:group).permit(:group_id, :name, :password, :password_confirmation)
end
end