undefined method `permit' for nil:NilClass - ruby-on-rails

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.

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)

Point a form to a specific controller action ruby on rails

Does anyone know how to point the following comments form to the hello action in my comments controller below, any advice would be appreciated:
<h2>Add a comment:</h2>
<%= form_for([#venue, #venue.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 %>
//////////////////my comments controller:
def hello
#venue = Venue.find(params[:venue_id])
#comment = #venue.comments.create(params[:comment].permit(:commenter, :body))
redirect_to venue_path(#venue)
end
You have to set route properly, for example:
resources :venues do
resources :comments do
collection do
post :hello
end
end
end
and set form url to this action's url:
<%= form_for(#venue.comments.build, url: hello_venue_comments_path(#venue)) do |f| %>

Why do I get undefined method `action' for PostsController(Table doesn't exist)?

I am pretty new to coding, so I decided to start the Ruby on Rails guide for version 4.0.0 and have had problem after problem. I am currently running version 4.0.0 and I have followed the guide step by step.
Once I got to 5.2 First form I began to get errors and was using other people's posts to solve my questions, but this error doesn't seem to happen for anyone else, so here it is:
NoMethodError in PostsController#index
undefined method `action' for PostsController(Table doesn't exist):Class
Here is my code:
class PostsController < ActiveRecord::Base
attr_accessible :title, :text
def new
end
def create
#post = Post.new(params[:post].permit(:title, :text))
#post.save
redirect_to #post
end
def show
#post = Post.find(params[:id])
end
def index
#post = Post.all
end
def action
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Here is my view:
<p>
<strong> Title: </strong>
<%= #post.title %>
</p>
<p>
<strong> Text: </strong>
<%= #post.text %>
</p>
My form is:
<h1> New Post </h1>
<%= form_for :posts, url: posts_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 %>
The error states that I don't have an action method defined, but I'm not sure how to define one in the first place, however this is not all. When I go to my localhost:3000/posts/new page I get the same error but for PostsController#new
Can someone please help me!!
It's not clear why your controller is inheriting from a model class:
class PostsController < ApplicationController
# ...
end

params.require().permit does not work as expected

I have this controller
class PeopleController < ApplicationController
def new
#person = Person.new
#person.phones.new
end
# this is the action that gets called by the form
def create
render text: person_params.inspect
# #person = Person.new(person_params)
# #person.save
# redirect_to people_path
end
def index
#person = Person.all
end
private
def person_params
params.require(:person).permit(:name, phones_attributes: [ :id, :phone_number ])
end
end
and this view
<%= form_for :person, url: people_path do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :phones do |f_phone| %>
<div class="field">
<p>
<%= f_phone.label :phone_number %><br />
<%= f_phone.text_field :phone_number %>
</p>
</div>
<% end %>
<p>
<%= f.submit %>
</p>
<% end %>
When I fill out both form fields and hit "Save Person" I only get {"name"=>"foo"} - the phone number seems to vanish.
However, when I change phones_attributes to phones I get {"name"=>"foo", "phones"=>{"phone_number"=>"123"}} (this would however cause problems with the create function.
What's wrong here?
Please note that this question is strongly related to that one: accepts_nested_attributes_for: What am I doing wrong as well as to this posting: https://groups.google.com/forum/#!topic/rubyonrails-talk/4RF_CFChua0
You don't have #phones defined in the controller:
def new
#person = Person.new
#phones = #person.phones.new
end
Finally found the problem. In the view there should be
<%= form_for #person, url: people_path do |f| %>
Instead of
<%= form_for :person, url: people_path do |f| %>
#phron said that already here:
accepts_nested_attributes_for: What am I doing wrong

ArgumentError in Articles#show

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

Resources