Cydonia7
Cydonia7

Reputation: 3826

Simple form issue with Rails 3

I've been trying recently to show a list of the fields modified with success on submitting a form. The only problem is that my form (I use simple form) doesn't show the errors when there are some and the form can't be submitted.

Here's my code simplified :

def update
  @wizard.assign_attributes(params[:wizard])
  # Get changed attributes

  if @wizard.save
    # Set the success flash with fields modified
    redirect_to @wizard
  else
    @title = "Edition du profil"
    render 'edit'
  end
end

The view :

<%= simple_form_for @wizard do |f| %>
    <%= f.input :email %>
    <%= f.input :story %>

    <%= f.submit "Modifier", :class => "btn success small" %>
<% end %>

The model :

class Wizard < ActiveRecord::Base
  has_secure_password

  attr_accessible :email, :story, :password, :password_confirmation, :password_digest

  serialize :ranks, Array

  validates_presence_of :email, :first_name, :last_name, :gender, :story
  validates_presence_of :password, :password_confirmation, :unless => Proc.new { |w| w.password_digest.present? }

  # Other validations here

  has_one :subject, :foreign_key => "teacher_id"

  ROLES = %w[teacher]

  scope :with_role, lambda { |role| {:conditions => "roles_bitmask & #{2**ROLES.index(role.to_s)} > 0"} }

  # Other functions here
end

Has anyone an idea ?

Thank you in advance !

Upvotes: 1

Views: 508

Answers (2)

charlysisto
charlysisto

Reputation: 3700

It has probably something to do with how you overwrote AR. I remember some plugin getting in trouble with assign_attributes. Meanwhile you can try :

@wizard.assign_attributes(params[:wizard], :without_protection => true)

If that works it will at least narrow down the problem to mass assignment.

Upvotes: 3

Muhammad Sannan Khalid
Muhammad Sannan Khalid

Reputation: 3137

you perhaps missing this part in edit/new view.Where @wizard is your model_name. Write this piece of code in the form tag.

<% if @wizard.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@wizard.errors.count, "error") %> prohibited this task from being saved:</h2>

          <ul>
            <% @wizard.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

Upvotes: 0

Related Questions