Ruby on rails 未定义方法正则用户路径(面向对象方法)

Ruby on rails 未定义方法正则用户路径(面向对象方法),ruby-on-rails,ruby,oauth,facebook-oauth,partial-classes,Ruby On Rails,Ruby,Oauth,Facebook Oauth,Partial Classes,我已经使用此工具实现了rails应用程序的面向对象方法。我还将我的用户表单拆分为编辑和帐户表单 用户编辑表单 -Name -Username -AboutMe 用户帐户表格 -Email -Password -Password Confirmation 在我尝试编辑一个普通用户的帐户或个人资料之前,一切似乎都正常。由于某种原因,我不断地犯这个错误 日志 尝试将资源添加到您的routes.rb文件中:在更新操作中,请按照以下建议执行操作: def update @user = Regu

我已经使用此工具实现了rails应用程序的面向对象方法。我还将我的用户表单拆分为编辑和帐户表单

用户编辑表单

-Name
-Username
-AboutMe
用户帐户表格

-Email
-Password
-Password Confirmation
在我尝试编辑一个普通用户的帐户或个人资料之前,一切似乎都正常。由于某种原因,我不断地犯这个错误

日志

尝试将资源添加到您的routes.rb文件中:在更新操作中,请按照以下建议执行操作:

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 instance,否则当您呈现编辑视图时,它将收到regular user instance而不是user instance,而您的表单期望的是user instance。

这实际上是可行的,但由于某些原因,每当更新失败并呈现正确的页面时,它都会收到regular user instance。“用户名不能为空”之类的错误警报不再出现。我确实在这里看到了,但它只是不显示它们……有点奇怪。”。我已经更新了代码以显示它。我已经更新了日志。我目前收到这个错误。NoMethodError未定义的方法“errors=”for:@SurgePedroza请检查更新的答案。在错误消息级别合并了错误哈希,您现在将不会得到错误。
class User < ActiveRecord::Base
  attr_accessible :name, :bio, :avatar, :username
end

class RegularUser < User

  has_secure_password

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[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 "/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"=>"clickoncomics@gmail.com", "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('clickoncomics@gmail.com') 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'
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