ArgumentError in Articles#show - ruby-on-rails

I am working with Jumpstartlab blogger tutorial. When I run server and I want open article I am getting following error:
ArgumentError in Articles#show
Showing /home/darek/rails_projects/blogger/app/views/comments/_form.html.erb where line
#3 raised:
First argument in form cannot contain nil or be empty
Extracted source (around line #3):
1 <h3>Post a Comment</h3>
2
3 <% form_for [ #article, #comment ] do |f| %>
4 <p>
5 <%= f.label :author_name %><br/>
6 <%= f.text_field :author_name %>
_form.html.erb
<h3>Post a Comment</h3>
<% form_for [ #article, #comment ] do |f| %>
<p>
<%= f.label :author_name %><br/>
<%= f.text_field :author_name %>
</p>
<p>
<% f.label :body %><br/>
<% f.text_area :body %>
</p>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
comments_controller.rb
class CommentsController < ApplicationController
def create
article_id = params[:comment].delete(:article_id)
#comment = Comment.new(params[:comment])
#comment.article_id = article_id
#comment.save
redirect_to article_path(#comment.article)
end
end
I've tried to compare code with github repository of tutorial but it didn;t help. The tutorial was prepared for Rails 3.x and I am working on 4.0
Any idea?

That error message says that objects you've used in the form_for is either nil or empty i.e. you haven't defined them. Since you haven't posted your show action try adding the following (Assuming you have the relationship between article and comment already setup):
# ArticlesController
def show
#article = Article.find(params[:id]) # However you are retrieving your #article
#comment = #article.comments.build
end

try this one :
def create
article_id = params[:comment].delete(:article_id)
#article = Article.find(article_id)
#comment = #article.comments.build(params[:comment])
#comment.save
redirect_to article_path(#comment.article)
end

Related

ForbiddenAttributesError in Rails 5

I am getting Forbidden Attributes Error even though I have used strong parameters in Rails. I have two models: Posts and Categories.
Here is my Post Controller:
class PostsController < ApplicationController
def index
#posts=Post.all
end
def new
#post=Post.new
#category=Category.all
end
def create
#post = Post.new(params[:post])
if #post.save
redirect_to posts_path,:notice=>"Post saved"
else
render "new"
end
end
def allowed_params
params.require(:post).permit(:title, :body, :category_id)
end
end
And here is my view for posts/new:
<%= form_for #post do |f| %>
<p>
<%= f.label :title %></br>
<%= f.text_field :title%><br/>
</p>
<p>
<%= f.label :body %></br>
<%= f.text_area :body%><br/>
</p>
<p>
<%= f.select :category_id, Category.all.collect{|x| [x.name,x.id]},{:include_blank =>"Select one"}%><br/>
</p>
<p>
<%= f.submit "Add Post" %>
</p>
<% end %>
But I am still getting Error.
You need to use allowed_params instead of params[:post]:
#post = Post.new(allowed_params)

how to edit comment within article's page - keep getting error about nil

I have typical article and comment blog format. It's a typical has_many/belongs_to, ie, the Guide's blog type.
However, I'm trying to edit the comment within the article, and I'm clueless about building the right form for this.
It's also in a partial which makes it more complicated for me.
Any help and/or educating me would be appreciated.
Comment's model
class Comment < ApplicationRecord
belongs_to :article
end
Article's model
class Article < ApplicationRecord
has_many :comments
validates :title, presence: true, length: { minimum: 5}
end
Article's Show page
<p>
<strong>Title:</strong>
<%= #article.title %>
</p>
<p>
<strong>Text:</strong>
<%= #article.text %>
</p>
<h2>Comments</h2>
<%= render #article.comments %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
<%= link_to 'Edit', edit_article_path(#article) %> |
<%= link_to 'Back', articles_path %>
Comment's _comment.html.erb page
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to 'Destroy Comment', [comment.article, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
<p>
<%= link_to 'Edit', edit_article_comment_path(#article, comment) %>
</p>
Comment's _form.html
<%= form_for([#article, #comment]) do |f| %>
<p>
<%= f.label :commenter %><br>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Error
It comes from Comment's _form.html.erb page in reference to this line: <%= form_for([#article, #comment]) do |f| %> ... and the error is: First argument in form cannot contain nil or be empty ...
Articles Controller
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def new
#article = Article.new
end
def edit
#article = Article.find(params[:id])
end
def create
#render plain: params[:article].inspect
##article = Article.new(params[:article])
##article = Article.new(params.require(:article).permit(:title, :text))
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def show
#article = Article.find(params[:id])
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
Comments Controller
class CommentsController < ApplicationController
def create
#article = Article.find(params[:article_id])
#comment = #article.comments.create(comment_params)
redirect_to article_path(#article)
end
def destroy
#article = Article.find(params[:article_id])
#comment = #article.comments.find(params[:id])
#comment.destroy
redirect_to article_path(#article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
First argument in form cannot contain nil or be empty
The error is due to #article and #comment being nil in the form. Since you are rendering the form with a partial, you need send variables to the partial as well
Change <%= render 'comments/form' %> to <%= render 'comments/form', article: #article, comment: #comment %>
and in the form have this <%= form_for([article, comment]) do |f| %>
Also in the show method of articles controller, you should define #comment
def show
#article = Article.find(params[:id])
#comment = Comment.new
end

Variables not displaying in rails blog post

I'm building a simple blog tool using rails and having trouble with variables displaying from a form. What is most confusing is that the post times are showing correctly, but the title and text aren't coming through.
My new form page looks like:
<h1>New post</h1>
<%= form_for :blog, url: blogs_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
The controller is currently:
class BlogsController < ApplicationController
def index
end
def new
end
def create
#blog = Blog.new(post_params[:id])
#blog.save
redirect_to #blog
end
def show
#blog = Blog.find(params[:id])
end
private
def post_params
params.require(:blog).permit(:title, :body)
end
end
And the show page is:
<div id="blog">
<h1>
<%= #blog.title %>
</h1>
<p class="date">
Submitted <%= time_ago_in_words(#blog.created_at) %> ago
</p>
<p>
<%= #blog.body %>
</p>
</div>
Any ideas?
Change:
def create
#blog = Blog.new(post_params[:id])
#blog.save
redirect_to #blog
end
to:
def create
#blog = Blog.new(post_params)
#blog.save
redirect_to #blog
end
The reason why it wasn't working is that you were only trying to savepost_params[:id]. You need to pass the whole param as an argument when creating a new blog post Blog.new(post_params)

How come when I create a new version of my model in Ruby on Rails, it does not send the id to the edit and show actions?

When I run my Ruby on Rails blog app, create a new article and attempt to save it, I get the following error "NoMethodError in Articles#Show". The reason seems to be that when my create an article and attempt to save it, it is not passing the id to the show action. I am getting a similar error when I try to edit a post as well. How can I fix this error? Below is the code for my Articles Controller and my view for Articles#New.
class ArticlesController < ApplicationController
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def show
#article = Article.find(params[:id])
end
def edit
#article = Article.find(params[:id])
end
end
//code for View
<%= form_for :article, url: articles_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I really appreciate the help!
<%= form_for :article, url: articles_path do |f| %>
should be
# view file
<%= form_for #article do |f| %>
# articles_controller.rb
def new
#article = Article.new
end
The first version is not incorrect per se, it's just used in other circumstances. The second one pulls the data from the #article variable into the form and then configures the form according to whether it's for creation or updating.
More in the documentation for form_for

undefined method `permit' for nil:NilClass

while i am creating a comment associated with the posts, i am getting this error::
My comments controller ::
class CommentsController < ApplicationController
def new
#comments = Comment.new
end
def create
#post = Post.find (params[:post_id])
#comments = #post.comments.create(params[:comments].permit(:commenter, :body))
redirect_to post_path(#post)
end
end
// The form for the comments ///
<strong>Title:</strong>
<%= #post.Title %>
</p>
<p>
<strong>Text:</strong>
<%= #post.Text %>
</p>
<%= form_for([#post, #post.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
i am getting error on this line::
#comments = #post.comments.create(params[:comments].permit(:commenter, :body))
Please point me out where i am wrong..
One edit :: My actual error statement ::
NoMethodError in CommentsController#create
The correct syntax to use strong parameter is
params.require(:comments).permit(:commenter, :body)
But I think params will contain comment not comments
So you should use
params.require(:comment).permit(:commenter, :body)
well, as the error message states, params[:comments] is nil.
You should be using params.require(:comments).permit(:commenter, :body) so that if comments isn't present, it won't go any further.
Also, the actual param being submitted is comment, not comments. You can verify this by looking at the submitted params in your logs.

Resources