Couldn't find Article without an ID [duplicate] - ruby-on-rails

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

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)

redirect_to #article doesn't work. article_url not defined

As I said above. I use my code like this.
class ArticlesController < ApplicationController
def show
#article = Article.find(params[:id])
end
def new
end
def create
#article = Article.new(article_params)
#article.save
redirect_to #article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
But it always doesn't work. I have no idea and other questions are no use.
This is my template.
<%= 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 %>
My fault I mistyped resources in the the route.
resources :articles

Ruby Incorrect link generation redirect_to products_path(#product) generate /products.1 instead of /products/1

I'm trying to add a reviews on my single product page. But when I click Submit - It takes me to the /products.1 page, instead of /products/1
class CommentsController < ApplicationController
def create
#product = Product.find(params[:product_id])
#comment = #product.comments.new(comment_params)
#comment.user = current_user
#comment.save
redirect_to products_path(#product)
end
def destroy
end
private
def comment_params
params.require(:comment).permit(:user_id, :body, :rating)
end
end
and the comment.html.erb
<div class="row">
<div class="col-sm-6">
<% if signed_in? %>
<h4>Add a review:</h4>
<%= form_for([#product, #product.comments.build]) do |f| %>
<p>
<%= f.label :body, "Comment" %><br>
<%= f.text_area :body, class: "form-control" %>
</p>
<p>
<%= f.label :rating %><br>
<%= f.text_field :rating, class: "rating form-control" %>
</p>
<p>
<%= f.submit "Submit", class: "btn" %>
</p>
<% end %>
<% end %>
</div>
</div>
Try redirect_to #product instead of redirect_to products_path(#product).
Did you check your routes.rb under config? Try running rake routes in the terminal and you can debug from there.

Undefined method errors "errors" for nil:Class

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.

Resources