Conditional Validation on Different Pages - Rails
A quick Rails question that I would love your guys' help on. Been looking around for the answer and everything out there seems a bit on the more complex side for what I think is the solution. I have two pages that use a model (User) and a form on each page which updates a different attribute when saved.
One updates their password details (validation is for presence of password)
One updates their quiz's response (validation is for 300 max characters)
How can I do conditional validation so that when I submit the form, validations appear on their respective pages? Do I need two separate actions? How do I link these actions to the form submit?
Here is the generic update action and the form:
def update @user = User.find(params[:id]) if @user.update_attributes(params[:user]) flash[:success] = "All is updated" sign_in @user redirect_to @user else render 'edit' end end
<p><h3>Linked-In Optimization</h3></p> <%= form_for(@user) do |form| %> <p><%= form.label :linkedin, 'Please copy and paste your Linked-In profile here (575 chars max.)' %></p> <p><%= form.text_area :linkedin %></p> <%= form.submit 'Save' %> <img class="linkedin" src="/linkedin.png"> <% end %>
To simplify things, use two separate controller actions.
To link your form submit to the new paths, do this
<%= form_for(@user, :url => update_password_user_path(@user)) do |f| %>
If you've added the necessary validations in your model, the error messages will appear on your respective forms.