2014-02-19 78 views
0

我已經實現了一個誓言面向對象的方法來使用我的rails應用程序tutorial。我還將我的用戶表單分成了一個編輯&帳戶表單。未定義的方法regular_user_path(誓言面向對象的方法)

用戶編輯表單

-Name 
-Username 
-AboutMe 

用戶帳戶表格

-Email 
-Password 
-Password Confirmation 

似乎一切都還好的工作,直到我試圖編輯一個普通用戶的帳戶或配置文件。出於某種原因,我不斷收到此錯誤。

undefined method `regular_user_path' for #<#<Class:0x007fc1952f6da0>:0x007fc18bf350a8> 

我不知道爲什麼會發生這種情況。以下是我的代碼如下。

模型

class User < ActiveRecord::Base 
    attr_accessible :name, :bio, :avatar, :username 
end 

class RegularUser < User 

    has_secure_password 

    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    VALID_UNAME_REGEX = /^[a-z](\w*[a-z0-9])*$/i 

    validates :name, length: { maximum: 50 } 

    validates :username, :presence => true, 
        :length  => { :maximum => 15 }, 
        :format  => { :with => VALID_UNAME_REGEX }, 
        :uniqueness => { :case_sensitive => false } 

    validates :bio, length: { maximum: 50 } 

end 

控制器

class UsersController < ApplicationController 

    before_filter :signed_in_user, only: [:index, :profile, :update, :destroy, :following, :followers, :account] 
    before_filter :correct_user, only: [:edit, :update, :account] 
    before_filter :admin_user,  only: [:destroy] 

    def edit 
    @user = User.find_by_username(params[:id]) 
    end 

    def account 
    @title = "Account" 
    @user = User.find_by_username(params[:id]) 
    end 

    def update 
    @user = RegularUser.find(current_user.id)  ###I think this is causing it 
    if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated" 
     sign_in @user 
     redirect_to root_url 
    else 
     if URI(request.referer).path == edit_user_path ###redirects to appropriate page 
     render 'edit' 
     else 
     render 'account' 
     end 
    end 
    end 

def destroy 
    User.find_by_username(params[:id]).destroy 
    flash[:success] = "User destroyed." 
    redirect_to users_url 
end 

private 

    def signed_in_user 
    unless signed_in? 
     store_location 
     redirect_to (root_path), notice: "Please sign in." 
    end 
    end 

    def correct_user 
    @user = User.find_by_username(params[:id]) 
    redirect_to(root_path) unless current_user?(@user) 
    end 

    def admin_user 
    redirect_to(root_path) unless current_user.admin? 
    end 

end 

瀏覽

Edit View 

<%= form_for @user, :html => { :multipart => true } do |f| %> ###error comes from this line 
    <%= render 'shared/error_messages', object: f.object %> 

    <div class="statictitle">Your Profile</div> 

    <%= f.text_field :username, placeholder: "Username..", :class => "form-control" %> 

    <%= f.text_field :name, placeholder: "Name", :class => "form-control" %> 

    <%= f.text_area :bio, placeholder: "About yourself in 160 characters or less...", class: "textinput" %> 

    <%= f.submit "Update Profile", class: "btn btn-primary" %><br> 

<% end %> 

共用錯誤

<% if object.errors.any? %> 
    <div id="error_explanation"> 
    <div class="alert alert-error"> 
     The form contains <%= pluralize(object.errors.count, "error") %>. 
    </div> 
    <ul> 
    <% object.errors.full_messages.each do |msg| %> 
     <li>* <%= msg %></li> 
    <% end %> 
    </ul> 
    </div> 
<% end %> 

條路線

resources :users do 
    member do 
    get :account 
    end 
end 

日誌

Started PUT "https://stackoverflow.com/users/ClickOnComics" for 127.0.0.1 at 2014-02-20 17:17:50 -0800 
Processing by UsersController#update as HTML 

Parameters: {"utf8"=>"✓", "authenticity_token"=>"1CUMHhkE10ubS6uuc26fu1yTGn1bABKNqRIJ67EhEO4=", 
"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]"}, "commit"=>"Update Account", "id"=>"ClickOnComics"} 

User Load (0.9ms) SELECT "users".* FROM "users" 
WHERE "users"."remember_token" = 'Wqodv-nd6EhKseqQf9FhqA' LIMIT 1 

User Load (1.1ms) SELECT "users".* FROM "users" 
WHERE "users"."username" = 'ClickOnComics' LIMIT 1 

RegularUser Load (0.5ms) SELECT "users".* FROM "users" 
WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] 

(0.2ms) BEGIN 
RegularUser Exists (2.7ms) SELECT 1 AS one FROM "users" WHERE 
(LOWER("users"."username") = LOWER('ClickOnComics') AND "users"."id" != 1) 
LIMIT 1 

RegularUser Exists (2.7ms) SELECT 1 AS one FROM "users" 
WHERE (LOWER("users"."email") = LOWER('[email protected]') AND "users"."id" != 1) LIMIT 1 
(0.2ms) ROLLBACK 

CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."username" = 'ClickOnComics' LIMIT 1 
Completed 500 Internal Server Error in 237ms 

NoMethodError (undefined method `errors=' for #<User:0x007fb6fadf87d0>): 
app/controllers/users_controller.rb:40:in `update' 

回答

2

在你update動做的建議如下:

def update 
    @user = RegularUser.find(current_user.id)  
    if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated" 
     sign_in @user 
     redirect_to root_url 
    else 
     error_messages = @user.errors.messages  ### Capture the error messages 
     @user = User.find_by_username(params[:id]) ### Add this line 
     @user.errors.messages.merge!(error_messages) ### Set the error messages hash 
     if URI(request.referer).path == edit_user_path 
     render 'edit' 
     else 
     render 'account' 
     end 
    end 
    end 

當您更新失敗,您需要將@user重置爲User實例,否則當你render編輯查看它將收到RegularUser實例,而不是User實例,而您的表單預計User實例。

+0

這實際上有效,但由於某種原因,每當更新失敗並呈現正確的頁面時。錯誤警報,如「用戶名不能爲空」不再顯示。 –

+1

@SurgePedroza請參閱已更新的答案 –

+0

我其實在這裏<%= render'shared/error_messages',object:f.object%>,但它只是不顯示它們....有點奇怪。我已更新我的代碼以顯示它。 –

0

嘗試增加resources :regular_users到您的routes.rb文件