Ruby on rails不会更新配置文件模型的属性

时间:2012-01-08 18:43:04

标签: ruby-on-rails ruby ruby-on-rails-3 rubygems ruby-on-rails-3.1

在我的用户模型中,更新工作完全正常... 无论我在我的配置文件模型中做什么,控制器视图我的数据库都不会更新。在我的用户模型中,我遇到了同样的问题,但发现这是因为我需要一个密码字段。但是,我没有,因为我在配置文件模型中没有任何验证集。

我不知道发生了什么事。终端中的日志显示正在找到current_user.id并且我正在使用该结果在我的配置文件表中查找user_id并且该工作正常。但是不会更新。

可能出现什么问题?

亲切的问候

日志

--- &id001 !ruby/object:Profile 
_already_called: 
  ? 
    - :autosave_associated_records_for_user
    - :user
  : false

_start_transaction_state: 
  :id: 1
  :new_record: false
  :destroyed: false
  :level: 1
aggregation_cache: {}

association_cache: {}

attributes: 
  id: 1
  user_id: 1
  motd: Success is guaranteed!
  first_name: Foo
  last_name: Bar
  birthday: 
  star_sign: 
  gender: 
  marital_status: 
  sexual_preference: 
  racial_background: 
  location: 
  profile_url: 
  about_me: 
  height: 
  body_type: 
  eye_colour: 
  drugs: 
  alcohol: 
  cigarettes: 
  likes: 
  dislikes: 
  bad_habits: 
  food: 
  music: 
  television: 
  book: 
  animal: 
  place: 
  possesion: 
  profile_visits: 
  created_at: 2012-01-07 23:02:29.228500
  updated_at: 2012-01-07 23:02:29.228500
attributes_cache: 
  birthday: 
  created_at: 2012-01-07 23:02:29.228500 Z
  updated_at: 2012-01-07 23:02:29.228500 Z
changed_attributes: {}

destroyed: false
errors: !ruby/object:ActiveModel::Errors 
  base: *id001
  messages: !omap 
    - :motd: []

    - :first_name: []

    - :last_name: []

marked_for_destruction: false
new_record: false
previously_changed: !map:ActiveSupport::HashWithIndifferentAccess {}

readonly: false
relation: 
validation_context: 
Profile updated
--- !map:ActiveSupport::HashWithIndifferentAccess 
utf8: "\xE2\x9C\x93"
_method: put
authenticity_token: YZwwOx5CqfKTfsybXE4NH2o2dTg4asKZZu6QOR9Y3Zo=
profile: !map:ActiveSupport::HashWithIndifferentAccess 
  motd: Success is guaranteed!
  first_name: Foo
  last_name: Bar
commit: update
action: update
controller: profiles
id: "1"

路线

  resources :users
  resources :sessions
  resources :passwords
  resources :profiles


  root :to                   => "users#new"
  match 'success'            => "users#success"
  match 'login'              => "sessions#new"
  match 'logout'             => "sessions#destroy"
  match 'reset_password'     => "passwords#new"
  match 'setup_new_password' => "passwords#edit"
  match 'settings', :to      => "users#settings"


  match "/settings/account", :to => "users#account"
  match "/settings/edit_profile", :to => "profiles#edit_profile"


  match '/:username', :controller => 'users', :action => 'show'

模型

class Profile < ActiveRecord::Base

  belongs_to :user

   attr_accessible :first_name, :last_name, :motd


end

Profiles_controller

    def new
    @user = Profile.new 
    end

    def update
        @user = Profile.find_by_user_id(current_user.id)
        if @user.update_attributes(params[:user])
        render 'edit_profile'
        flash[:success] = "Profile updated" 
        else
        redirect_to root_path
        end
      end

  def edit

  end

  def edit_profile
  @user = Profile.find_by_user_id(current_user.id)
  end

end

查看

<div id="formo"> <%= form_for @user,  do |f| %>

<%= f.text_field :motd, :placeholder => "motd"  %><br />
<%= f.text_field :first_name, :placeholder => "fname"  %><br />
<%= f.text_field :last_name, :placeholder => "lname"  %><br />

<%= f.submit 'update' %><br />
<% end %>
<%= debug(@user) %>
<div class="notify">
    <% flash.each do |name, msg| %>
          <%= content_tag :div, msg, :class => "#{name}" %>
    <% end %>
</div>

</div>

1 个答案:

答案 0 :(得分:2)

变化:

if @user.update_attributes(params[:user])

要:

if @user.update_attributes(params[:profile])

此外,您可能希望将该实例变量重命名为@profile,这样您就不会感到困惑。