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:

Users controller

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

Form

<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 %>

Answers


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.


Need Your Help

XML to Excel Mapping

xml vb.net excel mapping export-to-excel

I try to export some data to Excel using XML. Here is an example of my code that generates the Excel file:

MySQL and SSL connection failing ERROR 2026 (HY000)

mysql ssl

I have a wildcard cert issued from rapidssl, using CN=*.mydomain.com. I have a web server and a mysql db server. The certs are working fine for web site access. Now I want to enable ssl for my app to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.