如何在用户配置文件中呈现我的 items#show partial?

How do I render my items#show partial within a user profile?

大家早上好!我正在处理一项 rails 作业,但在将我的项目部分呈现在用户个人资料中时遇到了一些问题。我相信问题是@item 变量在用户的视图中没有初始化?每当我向用户列表添加新项目时,它都应该归因于 him/her,并显示出来。此时,我知道该项目正在创建并特定于用户,但没有显示。这是我的一些代码:

物品型号:

class Item < ActiveRecord::Base
  belongs_to :user
end

用户模型:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  has_many :items
end

物品管理员:

class ItemsController < ApplicationController
  def new
    @user = User.find(params[:user_id])
    @item = Item.new
  end

  def create
    @user = User.find(params[:user_id])
    @item = Item.new(params.require(:item).permit(:name))
    @item.user_id = @user.id
     if @item.save
       flash[:notice] = "Item was saved."
       redirect_to [@user, @item]
     else
       flash[:error] = "There was an error saving the item. Please try again."
       render :new
     end
   end

   def show
     @user = User.find(params[:user_id])
     @item = Item.find(params[:id])
   end
end

用户控制器:

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @item = @user.items
  end

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

_item.html.erb 部分:

<%= @item.name %>
<br>

此时,它仅在个人资料视图中显示项目。

用户#show:

<br>
<div class="container">
  <div class="row">
    <div class="col-md-11">
      <div class="panel panel-default">
        <div class="panel-body">
          <div class="row">
            <div class="col-md-12 lead">User profile<hr></div>
          </div>
          <div class="row">
            <div class="col-md-4 text-center">
              <img class="img-circle avatar avatar-original" style="-webkit-user-select:none;
              display:block; margin:auto;" src="http://robohash.org/sitsequiquia.png?size=120x120">
            </div>
            <div class="col-md-8">
              <div class="row">
                <div class="col-md-12">
                  <h1 class="only-bottom-margin"><%= @user.email%></h1>
                </div>
              </div>
              <div class="row">
                <div class="col-md-6">
                  <span class="text-muted">Name:</span> <%= @user.name%><br>
                  <span class="text-muted">Id:</span> <%= @user.id %><br>
                </div>
                <div class="col-md-6">
                  <h2>User's To-Do List</h2>
                  <%= render '/items/item' %>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

感谢您的帮助,没有 SO 社区我不可能走这么远。

你可以试试这个,希望对你有帮助。

用户控制器:

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @items = @user.items
  end

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

_item.html.erb部分:

<% @items.blank? %>
  <% @items.each do |item| %>
    <%= item.name %>
  <% end %>
<% else %>
 <h2> No Items Found. </h2>
<% end %>

用户#show:

<div class="col-md-6">
  <h2>User's To-Do List</h2>
  <%= render 'items/item', object: @items %>
</div>

尝试更改以下内容:

users_controller.rb

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

_item.html.erb

 <% unless items.blank? %>
   <% items.each do |it| %>
     <%= it.name %>
     <br/>
   <% end %>
 <% end %>

show.html.erb

 <%= render 'items/item', items: @items %>

您可以这样修改代码:

users_controller.rb

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

users/show.html.erb

<div class="col-md-6">
   <h2>User's To-Do List</h2>
    <%= render partial: 'items/item', locals: {items: @items} %>
</div>

itemss/_item.html.erb

<% items.each do |item| %>
  <%= item.name %>
<% end %>