I'm trying to render a partial in my main welcome page (i used HighVoltage gem to create it), my Groups form, where the user can create a new group...
what i've tried so far gives me the following error...
First argument in form cannot contain nil or be empty
= form_for @group do |f| 
    .fieldset
      - if @group.errors.any?
        .error_messages
My routes are configured like this
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
So when i try and load the main page goes straight to the welcome page and it crashes by the error above
i think i know what the problem is... but i don't knwo how to solve it. I'ts trying to render the form, but since the "group" viarable is never initialize it throws this error.
My code...
views/pages/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'
views/groups/_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'
Edit
controllers/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
    I set up a High Voltage demo application with a form for creating an "Inquiry."
Here is the commit with the changes to make the form work.
Here are the high level steps:
Create a form that will be used in the view:
<%= simple_form_for resource, html: { novalidate: true }, remote: true do |f| %>
  <%= f.input :name %>
  <%= f.input :email %>
  <%= f.input :comments, as: :text %>
  <%= f.button :submit, value: 'Submit' %>
<% end %>
Create a controller to process the request:
class InquiriesController < ApplicationController
  def create
    @inquiry = Inquiry.new(params[:inquiry])
    @inquiry.save
  end
end
The .save method on the Inquiry object will do the magic of emailing the request, or saving to a database, etc. Notice the call to valid? this is provided by ActiveModel::Model and will return true if all the validations pass.
class Inquiry
  include ActiveModel::Model
  attr_accessor :comments, :email, :name
  validates :comments, presence: true
  validates :email, presence: true, email: true
  validates :name, presence: true
  def save
    if valid?
      # send an email or persist to the database...
    end
  end
end
By using .js.erb files we can return a success message, or errors to the static page from the controller endpoint.
<% if @inquiry.valid? %>
  $('#inquiry_form').html('<h2>Thank you for contacting us!</h2>');
<% else %>
  $('#inquiry_form').html('<%= j render 'inquiry_form', resource: @inquiry %>');
<% end %>
What this does is return the "success" message if the request is valid, and returns the form with errors if the request is invalid.
来源:https://stackoverflow.com/questions/20293225/render-partial-from-static-page