简体   繁体   中英

Rails: New migration but nil in controller

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

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

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

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

可以肯定的是,您已经运行rake db:migrate来运行迁移,是吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM