ruby on rails - Form_for "First argument in form cannot contain nil or be empty" error -
i cannot figure out why i'm getting error, , means.
first argument in form cannot contain nil or empty(line 3)
add new post
<%= form_for @post |f| %> //error here <p> <%= f.label :title, 'title' %><br/> <%= f.text_field :title %><br/> </p> <p> <%= f.label :content, 'content'%><br/> <%= f.text_area :content %><br/> </p> <p> <%= f.submit "add new post" %> </p> <% end %>
controller:
class postscontroller < applicationcontroller def index @posts = post.all end def show @post = post.find(params[:id]) end def new @post = post.new end def create @post = post.new(params[:post]) if @post.save redirect_to posts_path, :notice => "your post saved" else render "new" end end def edit end def update end def destroy end end
assuming rendering postscontroller
, using conventional view name, new
method should create new post
, assign it:
def new @post = post.new end
you can use class name (as @yuriy suggested), conventional way instantiate new object. allows re-use same form view rendering errors after save.
if want see how looks, create new rails project , use scaffold generator create sample code.
Comments
Post a Comment