ok ...
<%= form_for :article, url: articles_path do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<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 %>
<%= link_to 'Back', articles_path %>
Ok, code above is from http://edgeguides.rubyonrails.org/getting_started.html
Question pertains to this part:
<%= form_for :article, url: articles_path do |f| %>
<% if #article.errors.any? %>
If I change :article to #article in first line, it still works. And it should, right because they're interchangeable, yes?
But if I change the second one like this:
#article.errors.any? to :article.errors.any?
it generates errors, specifically this:
undefined method 'errors' for :article:Symbol
Why??
Please do be gentle as I'm still trying to figure what may seems obvious to you.
Thinking out loud here, the reason why symbol doesn't work for second line is that it needs an instance of it to collect the errors, right?
But then, aren't symbols interchangeable with instances? Meaning that Rails should read that line and translate it into an instance, ie, it finds the instance of the model class, yes?
EDIT
apparently, I didn't read far enough again, and I didn't take to heart what I learned previously of form_for helper ...
EdgeGuides says this after the code:
The first parameter of the form_tag can be an object, say, #article which would cause the helper to fill in the form with the fields of the object. Passing in a symbol (:article) with the same name as the instance variable (#article) also automagically leads to the same behavior. This is what is happening here. More details can be found in form_for documentation.
The key is form_for helper.
That's why it doesn't work for the second line ... because it cannot figure out how to fill in the place of the symbol.
Hopes this helps some other poor guys/gals.
ps. feel free to correct me if my edit is wrong.
:article is only the model (not really sure :)
#article is an instance with data in it
when Article.new empty, Article.find(..) with data
In my applications I use always
<%= form_for #article do |f| %>
then I am sure that the form and the f variable reflects the instance #article
and all fields will be filled as expected.
In more complicated routes I use the url parameter as well.
<%= form_for :article, :url => articles_path do |f| %>
But it's only more clear if it goes in direction nested routes.
Although form_for :article and form_for #article both work, but that only means that the form_for method can gather the needed information from either #article or :article. It doesn't mean symbols and instance is interchangeable.
Related
EDIT: SOLVED, view the solution in Brian Kunzig's answer+comments.
I've decided to try out Ruby on Rails and have been constantly running into this problem. I was searching around for problems like this but none of the solutions have done the trick for me. It seems that I constantly write a part of code that just isn't correct. So here are some code snippets:
(ignore what the code would be for, it's just to try stuff out)
My controller:
class AuthsController < ApplicationController
def index
#auths=Auth.all
end
def new
#auth=Auth.new
end
def show
#auth=Auth.find(params[:id])
end
end
My index.html.erb:
<div style="margin: 0 auto; width:50%; text-align: center">
<h1>Please authorise your use of this webpage and its database(s).</h1>
<%= form_for :auth, url: auths_path do |f| %>
<% if #auth.errors.any? %> ==> RAILS REPORTS ERROR IN CODE HERE
<div id="error_explanation">
<h2>
<%= pluralize(#auth.errors.count, "error") %> prohibited
this authentification from being saved:
</h2>
<ul>
<% #auth.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :username %><br>
<%= f.text_field :username %>
</p>
<p>
<%= f.label :password %><br>
<%= f.text_field :password %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</div>
So, bottom line. Rails is saying that I cannot use #auth in the form_for block, for example. Or anywhere else for that matter.. It always says that it belongs to NilClass or something like that. It obviously wants me to instantiate it somehow, but isn't it enough to make the method new and put in the line: #auth=Auth.new ?
I'm just confused with this situation because I can't figure out how it's supposed to go. Thanks a lot !
P.S. I'm using <%= form_for :auth, url: auths_path do |f| %> because it won't accept #auth, that's what the error in the next line is. I have seen solutions to instantiate it "on the go" outside of the controller but I want to do it the way it's supposed to be done.
You should be putting this form in the new.html.erb file and not the index. The index is for listing entries while the new and create actions handle POST requests. You're getting an error because you're trying to list all Auth's when none have been created. Also, you're sending a form via a GET request if you're using standard rails routing. Use resourceful routing and put this form in your new view for that controller and it should work.
Routes file should be:
resources :auths
This will provide all the necessary routing automagically. If you type rake routes after modifying this you will see the newly generated urls and the helpers to them. You will notice it affords the create and update actions a POST/PUT request while others are GET.
I have comments on my post and I can display my comments first and form for writing comments after, but cannot display my form first and comments after. I'm pretty certain that the reason begind that is the .build:
<%= form_for([#question, #question.replies.build]) do |f| %>
So inside this form you just enter the comment body and click submit.
The displayed data is: commenter (user who commented), body of the comment and created_at.
The error I get is:
undefined method `first_name' for nil:NilClass
and the extracted source is:
<%= render #question.replies%>
<% #question.replies.each do |reply| %>
<div class="reply">
<p><%= link_to reply.user.first_name, user_profile_path(reply.user) %> says:</p>
<p><%= reply.body %></p>
<p>Answered <%= time_ago_in_words(reply.created_at) %> ago</p>
<% if current_user==reply.user %>
you should have some sort of #current_user available to you right?
So try doing something like this
<%= form_for([#question, #question.replies.build.tap{|a| a.user = #current_user}]) do |f| %>
The issue is that the comment that you are building with the form_for has a nil user value.
The solution to this is that you have to check if the reply is nil before displaying anything.
<% #question.replies.each do |reply| %>
<% unless reply.user.nil? %>
So I have the following erb that works:
<ul class="list-group">
<%- #user_skills.each do |user_skill| %>
<li class="list-group-item"><%= user_skill.skill.name %></li>
<% end %>
</ul>
<%= semantic_form_for current_user.user_skills.new do |f| %>
<%= f.inputs %>
<%= f.actions %>
<% end %>
In my controller, I have a #user_skills = current_user.user_skills.
Once I swap the two blocks:
<%= semantic_form_for current_user.user_skills.new do |f| %>
<%= f.inputs %>
<%= f.actions %>
<% end %>
<ul class="list-group">
<%- #user_skills.each do |user_skill| %>
<li class="list-group-item"><%= user_skill.skill.name %></li>
<% end %>
</ul>
It raises an error like this: undefined methodname' for nil:NilClass`. So I'm guessing the form builder is somehow overwriting the user_skill inside the ul block. But why and how do I resolve the problem? It doesn't seem to be a formtastic issue, since I also used the native form builder.
UPDATE:
So I found out that the #user_skills array actually contains the just created user_skill, which hasn't been persisted and do not have a #skill_id or #skill.name! And if I do UserSkill.new at the start of the form, then the #user_skills array will be ok. Maybe there is a caching layer in activerecord that will automatically include just created objects into the query. Or is this a bug?
The call current_user.user_skills.new adds a blank user_skill to the has_many association of current_user. Since #user_skills is simply a reference to current_user.user_skills, the blank instance is included in the the unordered list when iterating after initializing the new user_skill record.
You'd be better off simply initializing a new user_skill in the form:
<%= semantic_form_for UserSkill.new do |f| %>
<%= f.inputs %>
<%= f.actions %>
<% end %>
Then, in the create action of your UserSkillsController, associate the user_skill attributes with the current_user. A bare-bones version could look something like:
def create
#user_skill = current_user.user_skills.create(params[:user_skill])
respond_with #user_skill
end
I have a multi-model form and I cannot seem to capture the errors related to the nested models. This is the hierarchy of the form Project->Team->Roles->Role_skill_relationship. I am able to capture errors related the project model instance #project but I have been unsuccessful at capturing validations related to the Role and Skills models. The errors are directly returned in the browser page rather than redirecting and flashing to the screen. One example of an error if I intentionally fill out a role portion of the form incorrectly is
NoMethodError in Roles#create
Showing
C:/Users/Dstile/Documents/GitHub/creunity_app/app/views/skills/_form.html.erb
where line #8 raised:
undefined method `map' for nil:NilClass Extracted source (around line #8)
7: <%= skill_form.label :skill %>
8: <%= skill_form.collection_select :skill_id, #skills, :id, :name, :prompt => "Select a > skill" %>
Here is a portion of the code
<%= render 'shared/project_error_messages' %>
<%= project_form.label :title %>
<%= project_form.text_field :title %>
<%= project_form.label :category, "Category" %>
<%= project_form.select(:category, Project::CATEGORY_TYPES) %>
<%= project_form.label :description %>
<%= project_form.text_area :description %>
<%= project_form.label :goal_1, "Goal 1:" %>
<%= project_form.text_field :goal_1 %>
<h2>Your Team</h2>
<%= project_form.fields_for :team do |f| %>
<%= render 'teams/form', :team_form => f %>
<% end %>`
Here is the error render code
'<% if #project.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(#project.errors.count, "error") %>.
</div>
<ul>
<% #project.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>'
The role form is nested within the team form in the same manner team is nested in project. From all of the other threads I have read my understanding is that the errors for project and all of its children models (team, roles, role_skill_relationships) should be captured by the #parent object.
Is there a config setting or piece of code I am missing that should force the browser to ignore the errors? My thinking is that the errors may be in #project but that this process is interrupted.
It appears #skills has not been set. #collection_select runs #map on the passed-in collection (#skills in this case), and will show that error if the collection is nil (which is the default value for undefined instance variables).
Remember that, in the case of validation errors, the #create method in your controller will simply be rendering a template - it does not run the corresponding action method for that template. So if you're defining #skills in your #new action, then you need to also define it in your #create action (or, better yet, in a before_filter).
Rails 3.0 deprecated f.error_messages and now requires a plugin to work correctly - I however want to learn how to display error messages the (new) native way. I am following the getting started guide, which uses the deprecated method when implementing the comments form. For example:
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<%= f.error_messages %>
<div class="field">
<% f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here is the correct way to do it (as generated by the scaffold):
<%= form_for(#post) do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
. . .
I understand that I use the #post variable in the latter example, but what variable do I reference in the former to get the error messages for comment creation?
The best and clean way to implement error_messages in your form is by implementing error_messages in a FormBuilder.
For example, here is the error_messages method I've implemented for my last project.
By implemeting your own FormBuilder you can follow the rules and styles of your webdesigner...
Here is an example that will output the errors list in ul/li's with some custom styles :
class StandardBuilder < ActionView::Helpers::FormBuilder
def error_messages
return unless object.respond_to?(:errors) && object.errors.any?
errors_list = ""
errors_list << #template.content_tag(:span, "There are errors!", :class => "title-error")
errors_list << object.errors.full_messages.map { |message| #template.content_tag(:li, message) }.join("\n")
#template.content_tag(:ul, errors_list.html_safe, :class => "error-recap round-border")
end
end
Then in my forms :
= f.error_messages
And that's all.
I'm pretty sure all you'd need to do is reference #post.comments
So you could do something like:
<% #post.comments.each do |comment| %>
<% if comment.errors.any? %>
<% comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
<% end %>
<% end %>
Or just pull all the errors out out:
comment_errors = #post.comments.map(&:errors)
and then loop through them in your display logic to output each of the comment errors.
This functionality exists as a standalone gem dynamic_form.
Add the the following to your Gemfile
gem 'dynamic_form'
From the github page:
DynamicForm holds a few helpers method to help you deal with your Rails3 models, they are:
input(record, method, options = {})
form(record, options = {})
error_message_on(object, method, options={})
error_messages_for(record, options={})
It also adds f.error_messages and f.error_message_on to your form builders.
Here is my solution to the whole error scene.
I created a partial which simply uses a model variable which one would pass when rendering it:
<%# app/views/errors/_error.html.erb %>
<%= content_for :message do %>
<% if model.errors.any? %>
<ul>
<% model.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<% end %>
You can easily add dynamic html class and/or id names based on the name of the model, as well as generic ones.
I have things setup where my error messages render in all the same place in a layout file:
<%# app/views/layouts/application.html.erb %>
<%= yield :message %>
If one didn't want that functionality, removing the content_for in the partial would do the trick.
Then in really any view you want you can simply write:
<%= render 'errors/error', model: #some_model %>
One could further expand this by creating a partial which takes a collection and leverages the error partial above:
<%# app/views/errors/_collection.html.erb %>
<% collection.each do |model| %>
<%= render 'errors/error', model: model %>
<% end %>
Render it with:
<%= render 'errors/collection', collection: #some_model.some_has_many_association %>
I like this way. It is simple, easy to manage/maintain, and incredibly tweakable.
I hope this helps!
EDIT: Everything in HAML
-# app/views/errors/_error.html.haml
= content_for :message do
- if model.errors.any?
%ul
- model.errors.full_messages.each do |msg|
%li= msg
-# app/views/layouts/application.html.haml
= yield :message
= render 'errors/error', model: #some_model
-# app/views/errors/_collection.html.haml
- collection.each do |model|
= render 'errors/errors', model: #some_model
= render 'errors/_collection', collection: #some_model.some_has_many_association
I guess that the [#post, #post.comments.build] array is just passed to polymorphic_path inside form_for. This generates a sub-resource path for comments (like /posts/1/comments in this case). So it looks like your first example uses comments as sub-resources to posts, right?.
So actually the controller that will be called here is the CommentsController. The reason why Lukas' solution doesn't work for you might be that you actually don't use #post.comments.build inside the controller when creating the comment (it doesn't matter that you use it in the view when calling form_for). The CommentsController#create method should look like this (more or less):
def create
#post = Post.find(params[:post_id]
#comment = #post.comments.build(params[:comment])
if(#comment.save)
# you would probably redirect to #post
else
# you would probably render post#show or wherever you have the form
end
end
Then you can use the code generated by scaffolding, only replace #post instance variable with #comment in all the lines except form_for call.
I think it may also be a good idea to add the #comment = #post.comment.build to the controller method that displays this form and use form_for([#post, #comment], ...) to keep the form contents displayed in the form if there're errors.
If this doesn't work and you're not able to figure it out, please add your CommentsController#create method to the question.
I just looked into the docrails github issues, and they've decided to remove f.error_messages instead of explaining how to do validation for comments.
a rather simple implementation can be achieved with
class ActionView::Helpers::FormBuilder
def error_message(method)
return unless object.respond_to?(:errors) && object.errors.any?
#template.content_tag(:div, object.errors.full_messages_for(:"#{method}").first, class: 'error')
end
end
which allows one to use
<%= form.label :first_name %>
<%= form.text_field :first_name %>
<%= form.error_message :first_name %>
and with the following sass
#import variables
.error
padding: $margin-s
margin-left: $size-xl
color: red
.field_with_errors
input
border: 1px red solid
input:focus
outline-color: red
it looks like
using simple form gives you quite similiar functionality with more advanced functionality.
For example check out their examples with bootstrap