We are talking about a minimalistic nested Comment's Controller
.
the validation is validates :body, :presence => true
Now, editing a comment works, and if i create a new comment and break the validation it is shown normally.
My Problem is when i EDIT a comment and delete his body, NORMALLY i should get an error about the Body, instead i get a NoMethodError in Comments#update
Problem Extension => Normally when i successfully edit a Comment (http://localhost:3000/s/38/comments/11/edit)
it redirects me to @screen http://localhost:3000/s/38
. But if i empty the body and try to submit, it redirects me to http://localhost:3000/s/38/comments/11
instead of showing me the errors.
What am i missing ?
The Controller:
def update
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment.screen, notice: 'Comment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
My Edit Form:
<%= form_for([@screen, @comment]) do |f| %>
<div class="panel panel-default">
<div class="panel-heading">
<h1>Edit your Comment</h1>
<% if @comment.errors.any? %>
<div id="error_explanation">
<h3><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h3>
<ul>
<% @comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
<div class="panel-body">
<div class="form-group">
<%= f.label :body, "Comment" %>
<%= f.text_area :body, autofocus: true, class: "form-control" %>
</div>
</div>
<div class="panel-footer">
<%= f.submit "Edit Comment", class: "btn btn-primary" %>
<%= link_to "Back", @screen, class: "btn btn-primary" %>
</div>
</div>
<% end %>
My Routes:
resources :screens, :path => 's' do
resources :comments
member do
get :like
get :unlike
end
end
screen_comments GET /s/:screen_id/comments(.:format) comments#index
POST /s/:screen_id/comments(.:format) comments#create
new_screen_comment GET /s/:screen_id/comments/new(.:format) comments#new
edit_screen_comment GET /s/:screen_id/comments/:id/edit(.:format) comments#edit
screen_comment GET /s/:screen_id/comments/:id(.:format) comments#show
PATCH /s/:screen_id/comments/:id(.:format) comments#update
PUT /s/:screen_id/comments/:id(.:format) comments#update
DELETE /s/:screen_id/comments/:id(.:format) comments#destroy