New post - First argument in form cannot contain nil or be empty
up vote
0
down vote
favorite
This is error I'm getting when I click on "New Post:
First argument in form cannot contain nil or be empty
My PostController:
class PostsController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all
end
def show
end
def edit
end
def update
@post.update(post_params)
redirect_to posts_path
end
def new
if user_signed_in?
@post = Post.new
else
flash[:alert] = ":("
end
end
def create
@post = Post.new(post_params)
@post.user_id = current_user.id
@post.save
redirect_to posts_path
end
def destroy
if user_signed_in?
@post.destroy!
else
flash[:alert] = ":("
end
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:body)
end
def set_user
@post = Post.find(params[:id])
end
end
and posts#index:
<% @posts.each do |post| %>
<%= post.body %>
<%= link_to 'Show', post %>
<%= link_to 'Edit', edit_post_path(post) %>
<%= link_to 'Delete', post, method: :delete %>
<br>
_form.html.erb
<%= form_for(@post) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
ruby-on-rails ruby-on-rails-4
add a comment |
up vote
0
down vote
favorite
This is error I'm getting when I click on "New Post:
First argument in form cannot contain nil or be empty
My PostController:
class PostsController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all
end
def show
end
def edit
end
def update
@post.update(post_params)
redirect_to posts_path
end
def new
if user_signed_in?
@post = Post.new
else
flash[:alert] = ":("
end
end
def create
@post = Post.new(post_params)
@post.user_id = current_user.id
@post.save
redirect_to posts_path
end
def destroy
if user_signed_in?
@post.destroy!
else
flash[:alert] = ":("
end
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:body)
end
def set_user
@post = Post.find(params[:id])
end
end
and posts#index:
<% @posts.each do |post| %>
<%= post.body %>
<%= link_to 'Show', post %>
<%= link_to 'Edit', edit_post_path(post) %>
<%= link_to 'Delete', post, method: :delete %>
<br>
_form.html.erb
<%= form_for(@post) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
ruby-on-rails ruby-on-rails-4
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is error I'm getting when I click on "New Post:
First argument in form cannot contain nil or be empty
My PostController:
class PostsController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all
end
def show
end
def edit
end
def update
@post.update(post_params)
redirect_to posts_path
end
def new
if user_signed_in?
@post = Post.new
else
flash[:alert] = ":("
end
end
def create
@post = Post.new(post_params)
@post.user_id = current_user.id
@post.save
redirect_to posts_path
end
def destroy
if user_signed_in?
@post.destroy!
else
flash[:alert] = ":("
end
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:body)
end
def set_user
@post = Post.find(params[:id])
end
end
and posts#index:
<% @posts.each do |post| %>
<%= post.body %>
<%= link_to 'Show', post %>
<%= link_to 'Edit', edit_post_path(post) %>
<%= link_to 'Delete', post, method: :delete %>
<br>
_form.html.erb
<%= form_for(@post) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
ruby-on-rails ruby-on-rails-4
This is error I'm getting when I click on "New Post:
First argument in form cannot contain nil or be empty
My PostController:
class PostsController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all
end
def show
end
def edit
end
def update
@post.update(post_params)
redirect_to posts_path
end
def new
if user_signed_in?
@post = Post.new
else
flash[:alert] = ":("
end
end
def create
@post = Post.new(post_params)
@post.user_id = current_user.id
@post.save
redirect_to posts_path
end
def destroy
if user_signed_in?
@post.destroy!
else
flash[:alert] = ":("
end
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:body)
end
def set_user
@post = Post.find(params[:id])
end
end
and posts#index:
<% @posts.each do |post| %>
<%= post.body %>
<%= link_to 'Show', post %>
<%= link_to 'Edit', edit_post_path(post) %>
<%= link_to 'Delete', post, method: :delete %>
<br>
_form.html.erb
<%= form_for(@post) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
ruby-on-rails ruby-on-rails-4
ruby-on-rails ruby-on-rails-4
edited Nov 11 at 13:48
Cœur
17.1k9102140
17.1k9102140
asked May 30 '13 at 22:12
fuskie
356
356
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
you should check your "new.html.erb" or "_form.html.erb" file. to see if the "form_for" method is correctly called.
error doesnt show up when im logged in
– fuskie
May 30 '13 at 22:31
please post all the error message you got.
– Siwei Shen申思维
May 31 '13 at 1:18
add a comment |
up vote
0
down vote
If you are using devise, You can get this by adding a before action:
before_action :authenticate_user!, only: [:new]
and you new action as:
def new
@post = Post.new
end
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
you should check your "new.html.erb" or "_form.html.erb" file. to see if the "form_for" method is correctly called.
error doesnt show up when im logged in
– fuskie
May 30 '13 at 22:31
please post all the error message you got.
– Siwei Shen申思维
May 31 '13 at 1:18
add a comment |
up vote
0
down vote
you should check your "new.html.erb" or "_form.html.erb" file. to see if the "form_for" method is correctly called.
error doesnt show up when im logged in
– fuskie
May 30 '13 at 22:31
please post all the error message you got.
– Siwei Shen申思维
May 31 '13 at 1:18
add a comment |
up vote
0
down vote
up vote
0
down vote
you should check your "new.html.erb" or "_form.html.erb" file. to see if the "form_for" method is correctly called.
you should check your "new.html.erb" or "_form.html.erb" file. to see if the "form_for" method is correctly called.
answered May 30 '13 at 22:17
Siwei Shen申思维
13.7k45366
13.7k45366
error doesnt show up when im logged in
– fuskie
May 30 '13 at 22:31
please post all the error message you got.
– Siwei Shen申思维
May 31 '13 at 1:18
add a comment |
error doesnt show up when im logged in
– fuskie
May 30 '13 at 22:31
please post all the error message you got.
– Siwei Shen申思维
May 31 '13 at 1:18
error doesnt show up when im logged in
– fuskie
May 30 '13 at 22:31
error doesnt show up when im logged in
– fuskie
May 30 '13 at 22:31
please post all the error message you got.
– Siwei Shen申思维
May 31 '13 at 1:18
please post all the error message you got.
– Siwei Shen申思维
May 31 '13 at 1:18
add a comment |
up vote
0
down vote
If you are using devise, You can get this by adding a before action:
before_action :authenticate_user!, only: [:new]
and you new action as:
def new
@post = Post.new
end
add a comment |
up vote
0
down vote
If you are using devise, You can get this by adding a before action:
before_action :authenticate_user!, only: [:new]
and you new action as:
def new
@post = Post.new
end
add a comment |
up vote
0
down vote
up vote
0
down vote
If you are using devise, You can get this by adding a before action:
before_action :authenticate_user!, only: [:new]
and you new action as:
def new
@post = Post.new
end
If you are using devise, You can get this by adding a before action:
before_action :authenticate_user!, only: [:new]
and you new action as:
def new
@post = Post.new
end
answered Jun 2 '16 at 10:12
Atchyut Nagabhairava
4492818
4492818
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16846676%2fnew-post-first-argument-in-form-cannot-contain-nil-or-be-empty%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown