ruby on rails 4 - First argument in form cannot contain nil or be empty on line 3 -
i have read plenty on subject dont understand why wouldnt working way im doing it. im instantiating new post in posts_controller. using rails 4. matter?
here 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(post_params) if @post.save redirect_to posts_path, :notice => "your post saved" else render ="new" end end private def post_params params.require(:post).permit(:title, :content) end def edit @post = post.find(params[:id]) end def update @post = post.find(params[:id]) if @post.update_attributes(post_params) redirect_to posts_path, :notice => "your post has been updated" else render "edit" end end def destroy end end
here have in edit.html.erb
<h1>edit post</h1> <%= form_for @post |f| %> <p> <%= f.label :title %> <br/> <%= f.text_field :title %> </p> <p> <%= f.label :content %> <br/> <%= f.text_area :content %> </p> <p> <%= f.submit "update post" %> </p> <% end %>
why @post nil? tried doing "form_for post.new |f|" , works. have heard not approach
thanks in advance guys.
you have declared edit
, update
, destroy
actions private
. because of actions not getting called while rendering specific view, @post
variable not set. hence, error. remove them private section , add them public.
postscontroller
should below:
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(post_params) if @post.save redirect_to posts_path, :notice => "your post saved" else render ="new" end end def edit @post = post.find(params[:id]) end def update @post = post.find(params[:id]) if @post.update_attributes(post_params) redirect_to posts_path, :notice => "your post has been updated" else render "edit" end end def destroy ## need below code deleting post @post = post.find(params[:id]) @post.destroy redirect_to roles_url end private def post_params params.require(:post).permit(:title, :content) end end
Comments
Post a Comment