Copying Twitters Style - Integrating Index and Create Form on Same Page
I'd like to basically do what Twitter's stream does - have the create action and index action available on the same page, but I'm now getting an error, NilClass from the form.
Here's my view, then my code
class MistakesController < ApplicationController
respond_to :html, :xml, :json
before_filter :authenticate_user!
def index
@mistakes = Mistake.all
@user = current_user
respond_to do |t|
t.html
end
end
def create
@mistake = Mistake.new(params[:mistake])
@mistake.user = current_user
respond_to do |f|
if @mistake.save
f.html {开发者_如何学Python redirect_to("/", :notice => 'cool') }
else
f.html { render :action => 'new' }
end
end
end
def new
@new_mistake = Mistake.new
end
def show
@mistake = Mistake.find(params[:id])
end
end
and index.html.haml
%p test
%p= @user.email
= link_to "Create", new_mistake_path
- semantic_form_for @mistake do |form|
= form.inputs :name => "Basic" do
= form.input :message
= form.input :notes
= form.inputs :name => "Topics" do
= form.input
= form.buttons do
= form.commit_button
I'm starting to go through the growing pains of creating a non-trivial rails app so any help would be great.
Thanks
EDIT:
Error Info
Message
undefined method `model_name' for NilClass:Class
Occurs when I view index.html.haml.
Try adding this to the index action of your controller
@mistake = Mistake.new
This is required in addition to the @mistakes variable you already have there...
You may not need a new action at all, since that form is now part of your index page. I'd recommend just getting rid of that action and having create redirect to index instead.
This is in addition to stephenmurdoch's suggestion above.
精彩评论