在我的rails应用程序中,当客户点击主页上的用户名时,我需要显示用户列表

时间:2016-01-01 04:10:34

标签: ruby-on-rails-4

我有一个索引页面,显示来自多个用户的所有列表。我正在尝试创建一个页面,当购物者点击主页上的卖家名称时,该页面会动态显示特定的用户列表。我在列表控制器中创建了seller id,创建了路径和节目视图。我遇到的问题是在控制器中构建逻辑,该逻辑仅显示主页上购物者选择的特定用户的帖子。我希望这是有道理的,任何帮助都会很棒。

列表控制器

class ListingsController < ApplicationController
  before_action :set_listing, only: [:show, :edit, :update, :destroy]
  before_filter :authenticate_user!, only: [:seller, :new, :create, :edit, :update, :destroy]
  before_filter :check_user, only: [:edit, :update, :destroy]

  def seller
    @listings = Listing.where(user: current_user).order("created_at DESC")
  end

 def seller_id 
  #??????
  end

  # GET /listings
  # GET /listings.json
  def index
    if params[:category].blank?
      @listings = Listing.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 16)
    else
      @category_id = Category.find_by(name: params[:category]).id
      @listings = Listing.where(category_id: @category_id).order("created_at DESC").paginate(:page => params[:page], :per_page => 16)
    end
  end

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

  # GET /listings/new
  def new
    @listing = Listing.new
  end

  # GET /listings/1/edit
  def edit
  end

  # POST /listings
  # POST /listings.json
  def create
    @listing = Listing.new(listing_params)
    @listing.user_id = current_user.id

    if current_user.recipient.blank?
      Stripe.api_key = ENV["STRIPE_API_KEY"]
      token = params[:stripeToken]

      recipient = Stripe::Recipient.create(
        :name => current_user.name,
        :type => "individual",
        :bank_account => token
        )


    current_user.recipient = recipient.id
    current_user.save

end

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

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

  # DELETE /listings/1
  # DELETE /listings/1.json
  def destroy
    @listing.destroy
    respond_to do |format|
      format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def listing_params
      params.require(:listing).permit(:name, :category_id, :description, :price, :image)
    end

    def check_user
      if current_user != @listing.user
        redirect_to root_url, alert: "Sorry, this listing belongs to someone else"
      end
    end  
end

路由

get 'seller_id' => "listings#seller_id"

和我希望能够链接列表

的索引页面
<div class="jumbotron">
  <h1>Demo Site </h1>
  <h2>Discover one-of-a-kind items</h2>
</div>

<div class="center">
  <div class="row">
    <% @listings.each do |listing| %>
      <div class="col-md-3">
      <div class="thumbnail">
       <%= link_to image_tag(listing.image.url), listing %>
        <div class="caption">
          <h3><%= listing.name %></h3>
          <p><%= number_to_currency(listing.price) %></p>
          <p><%= "Sold by #{listing.user.name}" %></p>

          <P><%= link_to "Shop Page", seller_id_path %></P>
        </div>
      </div>
    </div>
    <% end %>
  </div>
</div>

<br>

<div class="center">
  <%= will_paginate @posts, renderer: BootstrapPagination::Rails %>
</div> 

<% if user_signed_in? %>

<div class="right">
    <%= link_to new_listing_path, class: "btn btn-primary", data: { no_turbolink: true } do %>
    <i class="glyphicon glyphicon-plus"></i> New Listing
    <% end %>
</div>    
<% end %>

<br>

和seller_id页面

<table class="table table-striped table-bordered">
  <tr>
    <th class="center">Image</th>
    <th class="center">Name</th>
    <th class="center">Description</th>
    <th class="center">Category</th>
    <th class="center">Price</th>

  </tr>  

  <% @listings.each do |listing| %>
  <tr>
    <td><%= image_tag listing.image.url(:thumb) %> </td>
    <td><%= listing.name %> </td>
    <td><%= listing.description %> </td>
    <td><%= listing.category.name %> </td>
    <td><%= number_to_currency(listing.price) %> </td>

</tr>

  <% end %>

</table>

<br>

<br>

1 个答案:

答案 0 :(得分:0)

你需要这样的东西,但这仅仅是例如。

路由

resources :sellers do
  resources :listings
end

列表控制器

  def index
    @seller = Seller.find(params[:seller_id])
    @listings = @seller.listings
  end

URL

yourapp.com/sellers/2/listings