Active Record中的项目无法正确呈现 - Rails 4.2

时间:2016-08-20 01:44:37

标签: ruby-on-rails postgresql

不太确定'Active Record'是否是正确的术语。 DB? Postgres的?

我正在关注Rails Tutorial并遇到一个非常令人沮丧的问题。我在SO上发现了很多关于人们苦苦挣扎的帖子,但是大多数人都没有找到答案的基础,所以我试图找出我的例子有什么问题。

我的用户控制器

类UsersController< ApplicationController的   before_action:set_user,only:[:show,:edit,:update,:destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:name, :email)
    end
end

我的用户模型

class User < ApplicationRecord
  has_many :micropost
  validates :name, presence: true
  validates :email, presence: true

end

My Microposts Model

class Micropost < ApplicationRecord
  belongs_to :user
  validates :content, length: { maximum: 140 },
                      presence: true
end

My Microposts Controller

class MicropostsController < ApplicationController
  before_action :set_micropost, only: [:show, :edit, :update, :destroy]

  # GET /microposts
  # GET /microposts.json
  def index
    @microposts = Micropost.all
  end

  # GET /microposts/1
  # GET /microposts/1.json
  def show
  end

  # GET /microposts/new
  def new
    @micropost = Micropost.new
  end

  # GET /microposts/1/edit
  def edit
  end

  # POST /microposts
  # POST /microposts.json
  def create
    @micropost = Micropost.new(micropost_params)

    respond_to do |format|
      if @micropost.save
        format.html { redirect_to @micropost, notice: 'Micropost was successfully created.' }
        format.json { render :show, status: :created, location: @micropost }
      else
        format.html { render :new }
        format.json { render json: @micropost.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /microposts/1
  # PATCH/PUT /microposts/1.json
  def update
    respond_to do |format|
      if @micropost.update(micropost_params)
        format.html { redirect_to @micropost, notice: 'Micropost was successfully updated.' }
        format.json { render :show, status: :ok, location: @micropost }
      else
        format.html { render :edit }
        format.json { render json: @micropost.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /microposts/1
  # DELETE /microposts/1.json
  def destroy
    @micropost.destroy
    respond_to do |format|
      format.html { redirect_to microposts_url, notice: 'Micropost was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_micropost
      @micropost = Micropost.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def micropost_params
      params.require(:micropost).permit(:content, :user_id)
    end
end

我的show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @user.name %>
</p>

<p>
  <strong>Email:</strong>
  <%= @user.email %>
  <% if @user.micropost.any? %>
  <%= @user.micropost.first %>
  <% end %>
</p>

<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>

当我加载一个用户页面(在我的情况下是6或7)时,我看到以这种格式输出'某事',但它正在显示 我觉得这是一个Active Record(?)索引?我不知道如何让它显示用户的第一个(或任何)Micropost。

在某些解决方案中,我看到人们使用了render @ user.micropost但是我遇到了一个关于partials的问题(我很熟悉),但教程说你应该能够使用之前使用的语法(aka @ user.email) )解决它。所以我觉得我过于复杂了?

2 个答案:

答案 0 :(得分:0)

我的问题是我需要使用

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @user.name %>
</p>

<p>
  <strong>Email:</strong>
  <%= @user.email %>
  <% if @user.micropost.any? %>
  <%= @user.micropost.first.content %>
  <% end %>
</p>

<%= link_to 'Edit', edit_user_path(@user) 

%> |
<%= link_to 'Back', users_path %>

我应该意识到它何时报告哈希值。

答案 1 :(得分:0)

你无法渲染展示页面,因为如果你这样做,你会得到一个no方法或nil类。

您的show动作没有名为@user的实例变量。

要让您的节目显示数据,您需要一个用户对象。在你的情况下你没有。

所以在你的show方法中,添加:

@ user.find_by(PARAMS [:ID])

这将找到用户6或7,并允许您拨打@

您可以从浏览器粘贴网址,以便查看您的实际位置吗?