I have a simple Rails app that has a model that is contained in several places. I allow the updating of the model from several different controllers in Rails 2.3.8. In my model I have code that allows for the name and the description to be validated. If they are blank Active_Scaffold should be populating the div with an error message. This works in the page that is linked directly to the User's controller but if I include the user anywhere else it will only show up on the User's controller page and not on the page that they are currently on. Shouldn't Acitive_Scaffold magically redirect to the correct place?
Code:
class User < ActiveRecord::Base
validates_presence_of :name, :description, :allow_blank => false
def create_from_params(params)
#name = params[:name]
#description = params[:description]
end
As you can see nothing spectacularly hard.
EDIT: Sorry I left out the rest of the method signature
I suspect the active_scaffold views only check for errors on the model it represents. Add this error check code to any view but the user view.
<%= error_messages_for :user %>
Related
I've been following the Getting Started rails tutorial and am now trying some custom functionality.
I have 2 models, Person and Hangout. A Person can have many Hangouts. When creating a Hangout, a Person has to be selected and associated with the new Hangout. I'm running into issues however when I call my create action. This fires before the validate_presence_of for person.
Am I going about this the wrong way? Seems like I shouldn't have to create a custom before_create validation to make sure that a Hangout was created with a Person.
#hangout_controller
def create
#person = Person.find(params[:hangout][:person_id])
#hangout = #person.hangouts.create(hangout_params)
#hangout.save
redirect_to hangouts_path(#hangout)
end
#hangout.rb
class Hangout < ActiveRecord::Base
belongs_to :person
validates_presence_of :person
end
#person.rb
class Person < ActiveRecord::Base
has_many :hangouts
validates :first_name, presence: true
validates :met_location, presence: true
validates :last_contacted, presence: true
def full_name
"#{first_name} #{last_name}"
end
end
Create action fires before the validate_presence_of for person
I think you are confused about rails MVC. Your form contains a url and when you submit your form your form params are send to your controller action according to the routes you have defined in routes.rb Your controller action, in this case create action, interacts with model this is very it checks for your validations and if all the validations are passed your object is saved in databse so even though in your app the control is first passed to your controller but your object is saved only once if all the validations are passed.
Now lets comeback to your code. There are couple of things you are doing wrong
a. You don't need to associate your person separately:
In your create action you have this line:
#person = Person.find(params[:hangout][:person_id])
You don't need to do this because your person_id is already coming from your form and it'll automatically associate your hangout with person.
b. You are calling create method instead of build:
When you call .association.create method it does two things for you it first initialize your object, in your case your hangout and if all the validations are passed it saves it. If all the validations are not passed it simply rollback your query.
If you'll use .association.build it'll only initialize your object with the params coming from your form
c. Validation errors won't show:
As explained above, since you are calling create method instead of build your validation error won't show up.
Fix
Your create method should look like this:
def create
#hangout = Hangout.new(hangout_params) # since your person_id is coming from form it'll automatically associate your new hangout with person
if #hangout.save
redirect_to hangouts_path(#hangout)
else
render "new" # this will show up validation errors in your form if your hangout is not saved in database
end
end
private
def hangout_params
params.require(:hangout).permit(:person_id, :other_attributes)
end
You are confused with the controller and model responsibilities.
Let me try to explain what I think is confusing you:
First try this in your rails console:
Hangout.create
It shouldn't let you because you are not passing a Person object to the create method. So, we confirm that the validation is working fine. That validation means that before creating a Hangout, make sure that there is a person attribute. All this is at the model level, nothing about controllers yet!
Let's go to the controllers part. When the create action of the controller 'is fired', that controller doesn't know what you are trying to do at all. It doesn't run any validations. It is just an action, that if you want, can call the Hangout model to create one of those.
I believe that when you say 'it fires' you are saying that the create action of the HangoutController is called first than the create method on the Hangout model. And that is completely fine. The validations run at the model level.
Nested Attributes
I think you'll be better using accepts_nested_attributes_for - we've achieved functionality you're seeking before by using validation on the nested model (although you'll be able to get away with using reject_if: :all_blank):
#app/models/person.rb
Class Person < ActiveRecord::Base
has_many :hangouts
accepts_nested_attributes_for :hangouts, reject_if: :all_blank
end
#app/models/hangout.rb
Class Hangout < ActiveRecord::Base
belongs_to :person
end
This will give you the ability to call the reject_if: :all_blank method -
Passing :all_blank instead of a Proc will create a proc that will
reject a record where all the attributes are blank excluding any value
for _destroy.
--
This means you'll be able to create the following:
#config/routes.rb
resources :people do
resources :hangouts # -> domain.com/people/:people_id/hangouts/new
end
#app/controllers/hangouts_controller.rb
Class HangoutsController < ApplicationController
def new
#person = Person.find params[:people_id]
#hangout = #person.hangouts.build
end
def create
#person = Person.find params[:people_id]
#person.update(hangout_attributes)
end
private
def hangout_attributes
params.require(:person).permit(hangouts_attributes: [:hangout, :attributes])
end
end
Although I've not tested the above, I believe this is the way you should handle it. This will basically save the Hangout associated object for a particular Person - allowing you to reject if the Hangout associated object is blank
The views would be as follows:
#app/views/hangouts/new.html.erb
<%= form_for [#person, #hangout] do |f| %>
<%= f.fields_for :hangouts do |h| %>
<%= h.text_field :name %>
<% end %>
<%= f.submit %>
<% end %>
I'm trying to create a Contact Us form on my website using Active Model.
The problem I am having is errors messages are never returned.
I'm using a remote form.
routes:
resource :front_contacts, only: :create, controller: :front_contact
controller:
class FrontContactController < ApplicationController
def create
contact = FrontContact.new(params[:front_contact])
#errors = contact.errors.size
end
end
front_contact:
class FrontContact
include ActiveModel::Model
attr_accessor :name, :email, :message
validates_presence_of :name, :message
validates_format_of :email, with: /[a-zA-Z0-9._%-]+#(?:[a-zA-Z0-9-]+\.)+(com|net|org|info|biz|me|edu|gov)/i
end
js.erb:
alert(<%= #errors %>);
The alert is always alerting zero.
Please advise.
If you're using Rails 4, there's the new inclusion of strong params that may be preventing your models from getting created.
Do you have something like the following anywhere in your controller?
params.require(:front_contact).permit!
I had the same problem as you when initially switching over to Rails 4, and was baffled because not permitting specific params doesn't throw an error message; the object just doesn't get created.
If you have a RailsCasts account, there's a really great video on how to deal with strong params here.
I've watched the RailsCast, another nested attributes video, lots of SO posts, and fought with this for a while, but I still can't figure it out. I hope it's something tiny.
I have two models, User (created by Devise), and Locker (aka, a product wishlist), and I'm trying to create a Locker for a User when they sign up. My login form has a field for the name of their new Locker (aptly called :name) that I'm trying to assign to the locker that gets created upon new user registration. All I'm ever greeted with is:
WARNING: Can't mass-assign protected attributes: locker
I've tried every combination of accepts_nested_attributes and attr_accesible in both of my models, yet still nothing works. I can see from the logs that it's being processed by the Devise#create method, and I know Devise isn't smart enough to create my models how I want :)
Here's the relevant bits of my two models:
# user.rb
class User < ActiveRecord::Base
attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :locker_attributes
# Associations
has_many :lockers
has_many :lockups, :through => :lockers
# Model nesting access
accepts_nested_attributes_for :lockers
end
and
# locker.rb
class Locker < ActiveRecord::Base
belongs_to :user
has_many :lockups
has_many :products, :through => :lockups
attr_accessible :name, :description
end
# lockers_controller.rb (create)
#locker = current_user.lockers.build(params[:locker])
#locker.save
I'm assuming I need to override Devise's create method to somehow get this to work, but I'm quite new to rails and am getting used to the black box "magic" nature of it all.
If anyone can help me out, I'd be incredibly thankful. Already spent too much time on this as it is :)
EDIT: I realized I omitted something in my problem. My Locker model has three attributes - name, description (not mandatory), and user_id to link it back to the User. My signup form only requires the name, so I'm not looping through all the attributes in my nested form. Could that have something to do with my issue too?
EDIT 2: I also figured out how to override Devise's RegistrationsController#create method, I just don't know what to put there. Devise's whole resource thing doesn't make sense to me, and browsing their source code for the RegistrationsController didn't help me much either.
And for bonus points: When a user submits the login form with invalid data, the Locker field always comes back blank, while the regular Devise fields, username & email, are filled in. Could this also be fixed easily? If so, how?
first, you have a typo :
attr_accessible :locker_attributes
should be plural :
attr_accessible :lockers_attributes
then, the standard way to use nested_attributes is :
<%= form_for #user do |f| %>
<%# fields_for will iterate over all user.lockers and
build fields for each one of them using the block below,
with html name attributes like user[lockers_attributes][0][name].
it will also generate a hidden field user[lockers_attributes][0][id]
if the locker is already persisted, which allows nested_attributes
to know if the locker already exists of if it must create a new one
%>
<% f.fields_for :lockers do |locker_fields| %>
<%= locker_fields.label :name %>
<%= locker_fields.text_field :name %>
<% end %>
<% end %>
and in you controller :
def new
#user = User.new
#user.lockers.build
end
def create
# no need to use build here : params[:user] contains a
# :lockers_attributes key, which has an array of lockers attributes as value ;
# it gets assigned to the user using user.lockers_attributes=,
# a method created by nested_attributes
#user = User.new( params[:user] )
end
as a side note, you can avoid building a new locker for new users in controller in different ways:
create a factory method on User, or override new, or use an after_initialize callback to ensure every new user instantiated gets a locker builded automatically
pass a specific object to fields_for :
<% f.fields_for :lockers, f.object.lockers.new do |new_locker_fields| %>
Someone helped me figure out the solution in a more "Rails 4'y" way with strong attributes & how to override Devise's sign_up_params (to catch all the data coming from my signup form).
def sign_up_params
params.require(:user).permit(:username, :email, :password, :lockers_attributes)
end
Gemfile addition: gem 'strong_parameters'
Commenting out the attr_accessible statement in my user.rb file, since apparently strong parameters eliminate the need for attr_accessible declarations.
# attr_accessible :username, :email, :password, :password_confirmation, :lockers
And the/a correct way of building a Locker before submitting the form: at the beginning of the nested form:
<%= l.input :name, :required => true, label: "Locker name", :placeholder => "Name your first locker" %>
Thanks again for all your help. I know a question like this is difficult to answer without seeing the whole codebase.
I have a Flight model nested inside a FlightLog model. A FlightLog may contain many flights.
I'm using SimpleForm with the bootstrap installation, which makes it possible to surround form elements with errors with the error class when a validation fails.
The problem is, that even though validations are triggered for the nested model, the fields with errors inside the simple_fields_for are not being marked, so it's not possible to determine which attribute is not valid.
After examining the errors hash when calling the create action, I can see that it is correctly populated with the errors at the top level, and the errors of the nested resources inside each resource.
How could I modify the behavior of simple_form to add the errors class to the control group of each nested model to match the behavior of the parent?
Thanks in advance.
If you are using simple_form with bootstrap, this does work - you just need to set up a few items correctly:
1 - Use the simple_form bootstrap wrappers (from simple_form 2.0) - you can find them in the github repo under config/initializers/simple_form.rb (https://github.com/rafaelfranca/simple_form-bootstrap)
2 - For nested forms to display the errors, you must be sure you provide an object. f.simple_fields_for :nested_model won't work, you need to use f.simple_fields_for parent_model.nested_model or f.simple_fields_for :nested_model, parent_model.nested_model so that the form can get the necessary object.
If you still don't get anything, verify that the form is really getting the object you think it is, with errors, by outputting the errors data on your nested object: parent_model.nested_model.errors.full_messages.to_sentence
I have been using custom accessors instead of the _id fields, so that's why they weren't getting notified when they had errors. I finally resolved to use f.error :attr_name under each accessor and changing the styling manually with JS
There might be several things wrong along the way. I had issues with this as well using bootstrap simple form. After I fixed everything in the controller, model and form it worked.
For me I had several issues, especially the commented lines where crucial in my case.
check that you have the following in place:
survey.rb:
class Survey < ApplicationRecord
has_many :answers
accepts_nested_attributes_for :answers, allow_destroy: true
#errors have to come from answer validation for answer form
validates_associated :answers
validates :question, :answers, presence: true
answer.rb:
class Answer < ApplicationRecord
belongs_to :survey
# make sure there is a validation on answer
validates :answer, presence: true
end
_form.html.slim
# make sure you have given the right attributes for .input and simple_fields_for
= f.simple_fields_for :answers, #survey.answers do |answer_form|
= answer_form.input :answer
surveys_controller.rb
def new
#survey = Survey.new
#when answers are not builded it wont show any simple fields for
#survey.answers.build
end
def create
#survey = Survey.new(survey_params)
#survey.user = current_backend_user
if #survey.save
redirect_to backend_surveys_path, notice: 'Umfrage erfolgreich erstellt'
else
render :new
end
end
def survey_params
# make sure everything is permitted correctly
params.require(:survey).permit(:some_attribute, ..., answers_attributes: %i[id answer])
end
reject if on model validation can cause errors some times on this as well. be cautious.
Let's say I have a form_tag in a view gathering a view user input . With this input that I get through the params hash, I want to verify some of htis information such as email phone name.
Can I just verify in the controller or is this a bad thing? I don't plan to save the thing I get to a db or anything, it's just getting put into an email and sent off, but I want to verify these things before it's sent.
Thank you.
EDIT:
Found this http://www.viddler.com/explore/rails3/videos/6/
class Test
include ActiveRecord::Validations
validates_presence_of :name, :email
attr_accessor :name, :email
end
You can use the model for whatever you need that is related to the object and don't need to save it. Keeping stuff like this in the model is desirable in order to keep controller tidy. Say you have a user model:
#controller
#user.new params[:user]
#user.check_valid
#user.save # is optional
#user.rb
def check_valid
!email.blank? and !phone.blank?
end