1

I'm new to Rails, so I have a newbie question.

We have a form that lets administrators set up a new user that looks like this:

<%= form_for :user, :url => url_for(:action => 'create_user', :account_id => @account.id, :hide_form => 1), :html => {:name => 'new_user_form', :id => 'new_user_form', :remote => true} do |f| %>
  First Name:
  <% f.text_field 'first_name' %><br/>
  Last Name:
  <%= f.text_field 'last_name' %><br/>
  Username:
  <%= f.text_field 'login' %><br/>
  Email:
  <%= f.text_field 'email' %><br/>
  Agency Code:
  <%= text_field_tag 'agency_code', @default_agency_code %><br/>

  <div class="button-bar">
    <%= f.submit :id => "submit_button" %>
  </div>
<% end %>

So far, so good. The action that is called when the form is submitted shoves all of the form values into a User object and saves it to the database:

def remote_create_user
  @user = User.new(params[:user])
  @user.agency = Agency.find{|agency| agency.product_code == params[:agency_code]}
  if @user.valid? and @user.save
    # Move some stuff around for the new user
  else
    @error = "Failure to Save:"
    @user.errors.full_messages.each {|msg| @error += " - #{msg}"}
  end
end

My understanding is that the line in the view that starts <%= form_for :user lets the ERB view know to validate all of the form fields that correspond directly to the User class using the validation logic specified in the User model.

However, the last field in the form (Agency Code: <%= text_field_tag 'agency_code', @default_agency_code %><br/>) doesn't correspond with an attribute in the User model. Instead, it corresponds with Agency.product_code. The Agency model defines validation for this attribute. How can I tell Rails to use the validation logic in the Agency model for this field? If there isn't a way to do that directly, how can I add validation directly to the agency code text field?

1 Answer 1

1

You can simply use

@user.agency = Agency.find_by_id{|agency| agency.product_code == params[:agency_code]}

and in your user model,

validates :agency_id, :presence => true

The find_by_id will work better in this case than simply find because it returns nil if the model is not found.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for responding so quickly! One think confuses me a little bit, still: How does the change to the User model let Rails know to look up the validation in the Agency model?
If I got it well, looking your code, it seems that you are selecting an existent agency, and not updating one when creating the user. In this case, the agency will always be valid if found, so in fact you want to validate a user attribute - he should have an agency. There is no validation on the agency model, it is on the user - it needs to have an agency (thus, agency_id can't be null). If you were modifying the agency after add the user, you would need to use a nested form instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.