2012-01-19 9 views
0

私はRuby on Railsの初心者です。私はこれらの属性を持つUserモデルを持っています:ログイン、電子メール、ロール、およびグループ。ここで更新記録 - Ruby On Rails 3.1

ここ

# == Schema Information 
# 
# Table name: users 
# 
# id   :integer   not null, primary key 
# login  :string(255) 
# email  :string(255) 
# role  :integer 
# group  :integer 
# created_at :datetime 
# updated_at :datetime 
# 

class User < ActiveRecord::Base 
    attr_accessible :login, :email, :role, :group 

     def self.search(search) 
     if search 
      where('login LIKE ?', "%#{search}%") 
      else 
      scoped 
     end 
    end 
end 

/app/models/user.rb私のファイル私のアプリ/ビュー/ユーザー/ edit.html.erb

<%= form_for(@user) do |f| %> 
    <div class="field"> 
    <%= f.label :login %><br /> 
    <%= f.text_field :login, :readonly => true%> 
    </br> 
    <%= f.collection_select(:role, Role.all, :id, :name) 
%> 
    </div> 
    <div class="actions"> 
    <%= f.submit "Update" %> 
    </div> 
<% end %> 

私は、[更新]ボタンをクリックし

、Iユーザーの表示ページに移動します。 は、私はこのトレースを持っている:

Started PUT "https://stackoverflow.com/users/4" 
    processing by UsersController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"HBB9IcKpWyjSbrmh6os=", "user"=>{"role"=>"3"}, "commit"=>"Update", "id"=>"4"} 
Redirected to http%3A%2F%2F10.100.2.66%3A3000%2Fusers%2F4 
Completed 302 Found in 1ms 

ここに私のアプリ/コントローラ/ users_controller.rb

class UsersController < ApplicationController 
    before_filter :require_admin 
    helper_method :sort_column, :sort_direction 


    def new 
    @title = "Sign up" 
    end 

    def show 
    @user = User.find(params[:id]) 
    @title = "User" 

    end 

    def edit 
    @title = "Edit User" 
    @user = User.find(params[:id]) 
    end 

    def index 
    @users = User.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:page => params[:page],:per_page => 10) 

    end 

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

    if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated." 
     redirect_to @user 
    else 
     @title = "Edit user" 
     render 'edit' 
    end 

    end 

    def get 
    @user = User.find(params[:login]) 
    end 

それが動作しない理由を私は理解していません。インデックスにあれば、私はそのようなユーザーの更新を強制する、それは働いています:

def index 
    @testUser = User.find(6) 
    @test.user.update_attributes(:role => "2") 
    ... 
end 

それは更新の指示が実行されないようです。私が行う場合:

def update 
    logger.info("i am in update) 
    ... 
end 

私はここにすくいroutesコマンドの結果すべてのログ

を持っていない:

を私ははっきりしていた願っています。私の英語のために申し訳ありません、彼は非常に悪いです。

ありがとうございました。

+0

あなたの問題は、正確に何ですか? –

+0

私の問題は、ユーザーレコードを更新できないことです。 –

答えて

0

ログは、それが実際に更新アクションに達していることを示している:

Started PUT "https://stackoverflow.com/users/4" 
    processing by UsersController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"HBB9IcKpWyjSbrmh6os=", "user"=>{"role"=>"3"}, "commit"=>"Update", "id"=>"4"} 
Redirected to http://10.100.2.66:3000/users/4 
Completed 302 Found in 1ms 

最初の行はshowアクションのように見えますが、アクションがGET要求されている示して覚えています。 「PUT」リクエストがあると、更新アクションにルーティングされます。

rake routes 
PUT /users/:id(.:format) {:controller=>"users", :action=>"update"} 

はこれをさらにライン2によって確認されています:rake routesを実行すると、この確認processing by UsersController#update as HTMLは、私たちは(例えばJSONとは反対に)HTMLなどがUserControllerの更新アクションを処理しているレール伝えます。

別のトピックでは、あなたのUser#role属性が誤解を招くことに気付きました。あなたはそれがrole_idそれはRoleに関連付けられた整数だ示しており、このようにコードを更新するために名前を付ける必要があります。

# user.rb 
attr_accessible :role_id 
belongs_to :role 

# app/views/users/edit.html.erb 
<%= f.collection_select(:role_id, Role.all, :id, :name) 

幸運を!

+0

こんにちは、あなたの答えとあなたの助けに感謝します。 User#role属性をUser#role_idに変更しました。しかし、主題については、あなたにとっては、すべてが大丈夫だと思われますか?詳細を掲載する必要がありますか? –

0

多分それが原因で間違った変数のためです:

def index 
    @testUser = User.find(6) 
    @test.user.update_attributes(:role => "2") 
    ... 
end 

それがなければなりません:

def index 
    @testUser = User.find(6) 
    @testUser.update_attributes(:role => "2") 
    ... 
end 

それともあなたがすることができますが、この:

@testUser = User.find(6) 
@testUser.role = 2 
@testUser.save