views:

56

answers:

4

Hello! I have created a new migration:

class AddSignatureToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :signature, :text
  end

  def self.down
    remove_column :users, :signature
  end
end

Now my usertable has a new column called signature. On my edit page I wrote:

<h1>Editing user</h1>
<% form_for(@user) do |f| %>



  <%= f.error_messages %>
  <div class="form-wrapper">
    <p>
      <label for="email">Email</label>
      <%= f.text_field :email %>
    </p>
    <p>
      <label for="user_signature">Signature</label>
      <%= f.text_area(:signature, :value => @user.signature) %>
    </p>
    <div class="form-submit">
      <%= f.submit 'Update', :class => "form-submit-button" %>
    </div>

  </div>


<% end %>

But this won't work. In my controller I always get nil as value for signature. Any ideas why?

  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        puts @user.signature #always nil
        flash[:notice] = 'User was successfully updated.'
        format.html { redirect_to(@user) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
A: 

Just to be sure, you have run rake db:migrate to run the migration, yes?

Eifion
yea, I ran rake db:migrate and the column exists in my database(select signature from users;)
Newbie
+1  A: 

Check to make sure you ran the migration for the proper environment (development, production)

RAILS_ENV=development rake db:migrate

This is the default, but you may be setting the environment somewhere. I think you'd get a method not found error if you hadn't, but just be sure, I've been hot by this before.

Next, if you're using the mongrel/webrick, try using the debugger, by starting the server with:

./script/server --debugger --environment=development

And then in your controller:

  respond_to do |format|
      debugger
      if @user.update_attributes(params[:user])
        puts @user.signature #always nil
  ...

And check what params is here, specifically params[:user][:signature], make sure it's getting passed correctly.

Lastly, in the view, all you need is:

  <%= f.label :signature %>
  <%= f.text_area :signature %>

The value will already be the current value since you're calling the form on @user in the form_for. The explicit setting of :value might be interfering somewhere

Dan McNevin
Well, I migrated the mirgration to my database and the column exists already. I Tried to write <%= f.label :signature %> <%= f.text_area :signature %>without any success. But I think it doesn't matter weather I write <label for="user_signature">Signature</label> <%= f.text_area(:signature, :value => @user.signature) %>or <%= f.label :signature %> <%= f.text_area :signature %>right?do I have to write something in my user model? I don't think so, but you never know... I am new at rails, sorry.
Newbie
Are you using a newer version of Rails (2.3.x)? You shouldn't need to write anything in your user model, activerecord sets up the methods for you automatically.
Dan McNevin
Yea, I use V2.3.2 and Netbeans as IDE.
Newbie
+1  A: 

Two quick questions-

  1. Why are we looking at edit.html.erb and update here? Did you already create this record with new.html.erb and create?

  2. Why do you have <%= f.text_area(:signature, :value => @user.signature) %> instead of just <%= f.text_area :signature %>

MattMcKnight
1. Yes, my user is created in a registration form (users/new.html.erb)2. Now I am using <%= f.text_area :signature %> but this won't work after all.
Newbie
What do you get if your try puts params[:user][:signature] in your controller?
MattMcKnight
When I puts params[:user][:signature] in my update function, I get the right value. I am new to Rails, so how to go on? What's the difference between puts params[:user][:signature] and puts @user.signature?
Newbie
Well, it looks like update_attributes isn't putting the params into the @user object. Very odd.. do you have any special handlers or anything in your User model?
MattMcKnight
+1  A: 

Okay, I found my error! In my user model, I had

attr_accessible :login, :email, :password, :password_confirmation

I added :signature and now it's working!

attr_accessible :login, :email, :password, :password_confirmation, :signature
Newbie