Rails匹配? attribute_missing(match,* args,&block):在StaticPagesController #home中的super,NoMethodError(代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails匹配? attribute_missing(match,* args,&block):在StaticPagesController #home中的super,NoMethodError(代码相关的知识,希望对你有一定的参考价值。

我正在尝试从railstutorial修改Michael Hartl示例应用程序。

我的static_pages控制器看起来像这样:

class StaticPagesController < ApplicationController

  def home
    if logged_in?
      @micropost  = current_user.microposts.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end

  def help
  end

  def about
  end

  def contact
  end
end

和我得到这样的错误消息:

NoMethodError in StaticPagesController#home
undefined method `microposts' for #<User:0x00000004d39d58>

else
    match = match_attribute_method?(method.to_s)
    match ? attribute_missing(match, *args, &block) : super
  end
end

用户模型:

class User < ActiveRecord::Base
  has_many :listings, :class_name => "Micropost", :foreign_key => "seller_id", dependent: :destroy
  has_many :bids, foreign_key: "bidder_id"
  has_many :active_relationships, class_name:  "Relationship",
                              foreign_key: "follower_id",
                              dependent:   :destroy
  has_many :passive_relationships, class_name:  "Relationship",
                               foreign_key: "followed_id",
                               dependent:   :destroy
  has_many :following, through: :active_relationships, source: :followed
  has_many :followers, through: :passive_relationships, source: :follower
  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i
  validates :email, presence: true, length: { maximum: 255 },
                format: { with: VALID_EMAIL_REGEX },
                uniqueness: {case_sensitive: false}
  has_secure_password
  validates :password, length: { minimum: 6 }, allow_blank: true

  def to_s
    self.name
  end

  # Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                              BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random token.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

 # Remembers a user in the database for use in persistent sessions.
 def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
 end 

  # Returns true if the given token matches the digest.
  def authenticated?(attribute, token)
     digest = send("#{attribute}_digest")
     return false if digest.nil?
     BCrypt::Password.new(digest).is_password?(token)
  end

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

  # Activates an account.
  def activate
    update_attribute(:activated,    true)
    update_attribute(:activated_at, Time.zone.now)
  end

  # Sends activation email.
  def send_activation_email
    UserMailer.account_activation(self).deliver_now
  end

# Sets the password reset attributes.

  def create_reset_digest
    self.reset_token = User.new_token
    update_attribute(:reset_digest,  User.digest(reset_token))
    update_attribute(:reset_sent_at, Time.zone.now)
  end

  # Sends password reset email.
  def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
  end



  # Returns true if a password reset has expired.
  def password_reset_expired?
    reset_sent_at < 2.hours.ago
  end

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
  end



  # Defines a proto-feed.
  # See "Following users" for the full implementation.
  # Returns a user's status feed.
  def feed
    following_ids = "SELECT followed_id FROM relationships
                     WHERE  follower_id = :user_id"
    Micropost.where("seller_id IN (#{following_ids})
                     OR seller_id = :seller_id", seller_id: id)
  end

  # Follows a user.
  def follow(other_user)
    active_relationships.create(followed_id: other_user.id)
  end

  # Unfollows a user.
  def unfollow(other_user)
    active_relationships.find_by(followed_id: other_user.id).destroy
  end

  # Returns true if the current user is following the other user.
  def following?(other_user)
    following.include?(other_user)
  end

  private

# Converts email to all lower-case.
def downcase_email
  self.email = email.downcase
end

# Creates and assigns the activation token and digest.
def create_activation_digest
  self.activation_token  = User.new_token
  self.activation_digest = User.digest(activation_token)
end
end

用户控制器

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update, :destroy,
                                        :following, :followers]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: :destroy

  def index
    @users = User.paginate(page: params[:page])
  end

  def show
    @user = User.find(params[:id])
    @micropost  = @user.microposts.build
    @microposts = @user.microposts.paginate(page: params[:page])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
  end

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

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

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  def following
    @title = "Following"
    @user  = User.find(params[:id])
    @users = @user.following.paginate(page: params[:page])
    render 'show_follow'
  end

  def followers
    @title = "Followers"
    @user  = User.find(params[:id])
    @users = @user.followers.paginate(page: params[:page])
    render 'show_follow'
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

    # Before filters

    # Confirms a logged-in user.
    def logged_in_user
      unless logged_in?
        store_location
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

    # Confirms the correct user.
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

    # Confirms an admin user.
    def admin_user
      redirect_to(root_url) unless current_user.admin?
    end

end

我不确定这个问题是与控制器实现或视图或meybe微博实现有关。请gyus帮助完成导轨菜鸟

答案

模型的第一行是:

class User < ActiveRecord::Base
  has_many :listings, :class_name => "Micropost", :foreign_key => "seller_id", dependent: :destroy

在您的用户模型中,您应该:

class User < ActiveRecord::Base
  has_many :microposts, dependent: :destroy
另一答案

你定义了:

has_many :listings, :class_name => "Micropost", :foreign_key => "seller_id"

所以你必须使用:

@micropost  = current_user.listings.build

而不是你的:

@micropost  = current_user.microposts.build

原因是Rails将:listings作为关系的“官方名称”。指定:class_name => "Micropost"时 - 对于Rails,表示实际名称。因此,您可以根据需要使用尽可能多的正式名称来实现一个实际依赖。

以上是关于Rails匹配? attribute_missing(match,* args,&block):在StaticPagesController #home中的super,NoMethodError(代码的主要内容,如果未能解决你的问题,请参考以下文章

opsworks rails.env 与自定义 json rails_env 不匹配

Rails,Ransack:如何在 HABTM 关系中搜索“所有”匹配项而不是“任何”匹配项

Rails 4 RoutingError:没有路由匹配 [POST]

Rails:无法匹配 json 响应中的 webhook 签名

如何使用 Axios 将 JSON 数据正确发送到 rails 服务器,以正确匹配所需的 rails 参数哈希?

当没有找到与 Rails 的 JQuery 自动完成匹配时如何显示响应