Undefined method errors "errors" for nil:Class - ruby-on-rails

My error says that it has: "undefined method `errors' for nil:NilClass" and "NoMethodError in Questions#index"
Where the error says the error is.
<% if object.errors.any? %>
<ul id="form-errors">
<% object.errors.full_messages.each do |message| %>
<li><%= message %></li>
My questions controller where i think the error is.
class QuestionsController < ApplicationController
before_filter :auth, only: [:create, :your_questions, :edit, :update]
# def index
# #question = Question.new
# #questions = Question.unsolved(params)
# end
def self.unsolved(params)
order('created_at DESC').where(solved: false).paginate(page: params[:page],per_page: 3)
end
def create
#question = current_user.questions.build(params[:question])
if #question.save
flash[:success] = 'Your question has been posted!'
redirect_to #question
else
#questions = Question.unsolved(params)
render 'index'
end
end
def new
#question = Question.new
end
def show
puts params
#question = Question.find(params[:id])
#answer = Answer.new
end
def your_questions
#questions = current_user.your_questions(params[:id])
end
def edit
#question = current_user.questions.find(params[:id])
end
def update
#question = current_user.questions.find(params[:id])
if #question.update_attributes(params[:question])
flash[:success] = 'Your question has been updated!'
redirect_to #question
else
render 'edit'
end
end
def search
#questions = Question.search(params)
end
end
My full _question_form.html.erb
<%= form_for(#question) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<p>
<%= f.label :body, "Question" %><br />
<%= f.text_field :body %>
<%= f.submit "Ask a Question" %>
</p>
App/Views/new.html.erb
<% provide(:title, 'Make It Snappy Q&A - Register') %>
<h1>Register</h1>
<%= form_for(#user) do |f| %>
<%= render 'common/form_errors', object: #user %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation, 'Confirm' %><br />
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= f.submit "Register" %>
</p>
<% end %>

Your file app/views/questions/edit.html.erb and app/views/questions/new.html.erb are most probably calling some partial... i would guess the partial is:
app/views/questions/_form.html.erb
Is that right?
In any case, that partial probably has these two lines at the top...
<%= form_for(#question) do |f| %>
<%= render 'shared/error_messages' %>
So you are calling the _error_messages partial without specifying what the object in the partial should reference. So change it to...
<%= form_for(#question) do |f| %>
<%= render 'shared/error_messages', object: #question %>
Alternatively, you can do...
<%= form_for(#question) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
Which does exactly the same thing but is slightly nicer because the form needs to mention the instance variable only once.

Related

Summit button not working in rails form_with edit action

I have created a form to edit a nested resource within a rails application. The form renders properly and the submit button works with the form for the create action but causes noting to happen in when applied to the edit action. No error is triggered and the page doesn't change so it is hard to figure out where the problem exists. Similar problems seam to be the result of a syntax error but I cant seem to find one in this case however, I may be missing something. Below is the form in question.
<h1>Edit an Ingredient<h1>
<%= form_with model: [ #recipe, #ingredient ], local: true do |form| %>
<p>
<%= form.label :quantity %><br>
<%= form.text_field :quantity %>
</p>
<p>
<%= form.label :measurement %><br>
<%= form.text_area :measurement %>
</p>
<p>
<%= form.label :ingredientname %><br>
<%= form.text_area :ingredientname %>
</p>
<p>
<%= form.label :label %><br>
<%= form.text_area :label %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', recipes_path %>
And the functioning "New" form...
<h1>Add an Ingredient<h1>
<%= form_with model: [ #recipe, #recipe.ingredients.build ], local: true do |form| %>
<p>
<%= form.label :quantity %><br>
<%= form.text_field :quantity %>
</p>
<p>
<%= form.label :measurement %><br>
<%= form.text_area :measurement %>
</p>
<p>
<%= form.label :ingredientname %><br>
<%= form.text_area :ingredientname %>
</p>
<p>
<%= form.label :label %><br>
<%= form.text_area :label %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', recipes_path %>
And finally the relevant controller...
class IngredientsController < ApplicationController
def new
#recipe = Recipe.find(params[:recipe_id])
end
def edit
#recipe = Recipe.find(params[:recipe_id])
#ingredient = #recipe.ingredients.find(params[:id])
end
def create
#recipe = Recipe.find(params[:recipe_id])
#ingredient = #recipe.ingredients.create(ingredient_params)
redirect_to recipe_path(#recipe)
end
def update
#recipe = Recipe.find(params[:recipe_id])
#ingredient = #recipe.ingredients.find(params[:id])
end
def destroy
#recipe = Recipe.find(params[:recipe_id])
#ingredient = #recipe.ingredients.find(params[:id])
#ingredient.destroy
redirect_to recipe_path(#recipe)
end
private
def ingredient_params
params.require(:ingredient).permit(:quantity, :measurement, :ingredientname, :label)
end
end
Additionally the form is populated correctly when it is rendered leading to me believe it is not a problem with the form_with statement. Any help is appreciated!
I was able to work out the solution. The submit button was not working due to an incomplete definition of the update action in the controller. Instead of ...
def update
#recipe = Recipe.find(params[:recipe_id])
#ingredient = #recipe.ingredients.find(params[:id])
end
the update action should be defined as...
def update
#recipe = Recipe.find(params[:recipe_id])
#ingredient = #recipe.ingredients.find(params[:id])
if #ingredient.update(ingredient_params)
redirect_to #recipe
else
render 'edit'
end
end
change <%= form_with model: [ #recipe, #recipe.ingredients.build ], local: true do |form| %>
to <%= form_for [ #recipe, #recipe.ingredients.build ] do |form| %>

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)

Ruby On Rails Update failed still

I have try many things but still failed, i want to implement a edit and update action here.
Below is my code
home controller:
class HomeController < ApplicationController
def index
#inputs = Person.all
end
def new
#input = Person.new
end
def create
#input = Person.new(input_params)
respond_to do |x|
if #input.save
x.html {redirect_to :action => 'index'}
else
x.html {render :action => 'new'}
end
end
end
def show
#input = Person.find(params[:id])
end
def edit
#input = Person.find(params[:id])
end
def update
#input = Person.find(params[:id])
respond_to do |x|
if #input.update(input_params)
x.html {redirect_to :action => 'index'}
else
x.html {render :edit}
end
end
end
private
def input_params
params.require(:inputs).permit(:name, :weight, :height, :color, :age)
end
end
edit.html.erb
<h1>Editing Data</h1>
<%= render 'form' %>
<%= link_to 'Show', home_path %> |
<%= link_to 'Back', home_index_path %>
form.html.erb:
<%= form_for :#input do |person| %>
<div class="field">
<%= person.label :name %><br>
<%= person.text_field :name %>
</div>
<div class="field">
<%= person.label :weight %><br>
<%= person.number_field :weight %>
</div>
<div class="field">
<%= person.label :height %><br>
<%= person.number_field :height %>
</div>
<div class="field">
<%= person.label :color %><br>
<%= person.text_field :color %>
</div>
<div class="field">
<%= person.label :age %><br>
<%= person.number_field :age %>
</div>
<div class="actions">
<%= person.submit %>
</div>
<% end %>
Routes are correct, i can lead me to the edit page, however, it shows,
first problem, edit with no data pops up
New updated
You should use form_for instead of form_tag,
<%= form_for #input do |x| %>
...
and then (depending on what #input actually is!)
<%= x.text_field :age %>
It looks like you are not binding your form to your method.
http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object
gives an overview, but I'd suggest something like this for your edit:
<%= form_for #input do |f| %>
<%= render 'form', f: f %>
<%= f.submit "Update" %>
<% end %>
and in your partial - which should be _form.html.erb
<div class="field">
<p><label for = "input_name">Name</label>:
<%= f.text_field :name %>
</div>
etc.. Now assuming you have set your routes correctly, it will go back to your update method. If you have not set up your routes in the way rails expects, then you should use:
<%= form_for #article, url: **your_update_path** do |f| %>
Just add the routes path that points to your update method, when you run rake routes.

Couldn't find Article without an ID [duplicate]

I am trying to submit some data to db and its ok but when i am trying to retrieve those data show that Couldn't find Article without an ID.ils 4.0.1.
I am using ruby 2.0.0 and ra
def show
#article=Article.find(params[:id])
end
the ** contain error.
class ArticlesController < ApplicationController
def new
end
def create
#article = Article.new(article_params)
redirect_to #article
#article.save
end
def show
#article=Article.find(params[:id])
end
private
def article_params
params.require(:article).permit(:title, :full_name, :email, :phone_number, :message)
end
end
articles/show.html.erb
<p>
<strong>Title:</strong>
<%= #article.title %>
</p>
<p>
<strong>Full Name:</strong>
<%= #article.full_name %>
</p>
<p>
<strong>Email:</strong>
<%= #article.email %>
</p>
<p>
<strong>Phone Number:</strong>
<%= #article.phone_number %>
</p>
<p>
<strong>Message:</strong>
<%= #article.message %>
</p>
articles/new.html.erb
<h1>New Articles</h1>
<%= form_for :article, url: articles_path do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
<p>
<%= f.label :full_name %>
<%= f.text_field :full_name %>
</p>
<%= f.label :email %>
<%= f.text_field :email %>
<p>
<%= f.label :phone_number %>
<%= f.text_field :phone_number %>
</p>
<%= f.label :message %>
<%= f.text_field :message %>
<p>
<%= f.submit :send_message %>
</p>
<% end %>
You are redirecting before actually saving your article.
This:
def create
#article = Article.new(article_params)
redirect_to #article
#article.save
end
should be:
def create
#article = Article.new(article_params)
#article.save
redirect_to #article
end
And if you want to add some error handling as well:
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render :new
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

Resources