簡體   English   中英

Rails #show 頁面只反映第一個種子 object 而不是所有種子對象?

[英]Rails #show page only reflecting first seeded object instead of all seeded objects?

我有一個顯示頁面連接到我的租賃 object。 在遍歷屬於租賃的評論時,它只返回第一個評論 object。 我有幾個 Review 對象通過我的種子文件連接到每個 Rental,但似乎無法讓所有對象都反映。 這是我的文件:

出租/展示.html.erb


<h2> Rental Show Page </h2>

<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<td> <strong> Street Address: </strong> </td>
<td> <%= @rental.street_add %> </td>
</tr>
<tr>
<td><strong> City: </strong></td>
<td><%= @rental.city %></td>
</tr>
<tr>
<td><strong> State: </strong></td>
<td><%= @rental.state %></td>
</tr>
<tr>
<td><strong> Owner: </strong></td>
<td><%= @rental.owner %></td>
</tr>
 </tbody>
</table>

<li><%= link_to 'Edit', rental_path(@rental) %> </li>
<li><%= button_to 'Delete', rental_path(@rental), method: :delete, data: {confirm: "Are you sure?"} %></li>
<li><%= link_to 'Home', rentals_path %></li>

<h1> Reviews </h1>
<% @rental.reviews.each do |reviews| %>
<%= link_to "#{@review.title}", rental_review_path(@rental), class: "btn btn-danger" %>
<% end %>



<h2>Add a review:</h2>
<%= link_to "Add a Review", new_rental_review_path(@rental), class: "btn btn-danger" %>

我的出租屋 Controller


class RentalsController < ApplicationController
 
    def index 
        @rentals = Rental.all
    end
    
    def show
        @review = Review.new
        @review = Review.find_by(id: params[:id])
        return if @rental = Rental.find_by(id: params[:id])
        redirect_to root_path, notice: "Rental is not available"
    end 

    def new
        @rental = Rental.new
    end

    def edit
        @rental = Rental.find_by(id: params[:id])
    end
    
    def create
        @rental = Rental.new(rental_params)
        if @rental.save
          redirect_to @rental, notice: "Your rental has been succesfully added."
        else 
            render :new
        end 
    end

    def update
        @rental = Rental.find_by(id: params[:id])
        @rental.update(rental_params)
        redirect_to @rental, notice: "Your rental has been succesfully updated."
    end
    
    def destroy
        @rental = Rental.find_by(id: params[:id])
        @rental.destroy
        redirect_to root_path
    end 
    
    
    private

    def rental_params
        params.require(:rental).permit(
            :street_add,
            :city,
            :state,
            :owner,
        )
    end

end

還有我的種子文件:


Rental.create(street_add: "1428 Elm Street", city: "Springwood", state: "Ohio", owner: "Fred D. Krueger")

Rental.create(street_add: "112 Ocean Avenue", city: "Amityville", state: "NY", owner: "Butch DeFeo")

Rental.create(street_add: "8601 N Thorne Ln SW", city: "Lakewood", state: "WA", owner: "Kirtland Cutter")


User.create(username: "AAA", email: "test@test1.com", password: "123456")

User.create(username: "BBB", email: "test@test2.com", password: "1234567")

User.create(username: "CCC", email: "test@test3.com", password: "12345678")


Review.create(title: "Weird Dreams, Nice Lawn", body: "Open kitchen space, large green lawn. Had very strange dreams while living here.", rating: 3, rental_id: 1, user_id: 1)

Review.create(title: "Tenant Left Items", body: "Loved the master bedroom, but wasn't thrilled that the tenant left behind items.", rating: 2, rental_id: 2, user_id: 2)

Review.create(title: "Beautiful Furnished Basement", body: "Basement was fully furnished and was able to convert it into an office space. Loved the quiet neighborhood.", rating: 5, rental_id: 3, user_id: 3)


Review.create(title: "I loved it, husband didn't", body: "While I loved the quaint house, my husband was adamant that he didn't. (Something about voices). Great price for the location.", rating: 4, rental_id: 1, user_id: 1)

Review.create(title: "Very Cold", body: "The house was next to impossible to heat and was cold even in the summer. Heating costs were through the roof.", rating: 2, rental_id: 2, user_id: 2)

Review.create(title: "I loved it, husband didn't", body: "While I loved the quaint house, my husband was adamant that he didn't. (Something about voices). Great price for the location.", rating: 4, rental_id: 3, user_id: 3)


Review.create(title: "Sprawling Estate", body: "Plenty of space, landlord was very accomodating", rating: 5, rental_id: 1, user_id: 1)

Review.create(title: "Beautiful Trees in Backyard", body: "Beautiful trees in backyard although the owls made strange noises at night.", rating: 3, rental_id: 2, user_id: 2)

Review.create(title: "Stains Under All Rugs", body: "I noticed the rugs were placed oddly, and when lifted to clean the floors revealed large rust stains. We had someone out twice to clean them but they weren't able to be removed. Faucets produced rust colored water as well.", rating: 2, rental_id: 3, user_id: 3)

puts "data loaded success"

我想要完成的是為每個租金列出多個評論。 這是我的租金#show 頁面的屏幕截圖與所有評論的屏幕截圖:

租賃展示頁面租賃指數

很感謝任何形式的幫助!

更新:我能夠通過更新顯示頁面以反映 link_to 來找到解決方案:

<h2> Rental Show Page </h2>

<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<td> <strong> Street Address: </strong> </td>
<td> <%= @rental.street_add %> </td>
</tr>
<tr>
<td><strong> City: </strong></td>
<td><%= @rental.city %></td>
</tr>
<tr>
<td><strong> State: </strong></td>
<td><%= @rental.state %></td>
</tr>
<tr>
<td><strong> Owner: </strong></td>
<td><%= @rental.owner %></td>
</tr>
 </tbody>
</table>

<li><%= link_to 'Edit', rental_path(@rental) %> </li>
<li><%= button_to 'Delete', rental_path(@rental), method: :delete, data: {confirm: "Are you sure?"} %></li>
<li><%= link_to 'Home', rentals_path %></li>

<h1> Reviews </h1>
<% @rental.reviews.each do |reviews| %>
<li><%= link_to "#{reviews.title}", rental_review_path(@rental, reviews), class: "btn btn-danger" %></li>
<% end %> 

<h2>Add a review:</h2>
<%= link_to "Add a Review", new_rental_review_path(@rental), class: "btn btn-danger" %>

並像這樣更新 controller:


class RentalsController < ApplicationController
 
    def index 
        @rentals = Rental.all
    end
    
    def show
        @review = Review.find_by(id: params[:id])
        return if @rental = Rental.find_by(id: params[:id])
        redirect_to root_path, notice: "Rental is not available"
    end 

    def new
        @rental = Rental.new
    end

    def edit
        @rental = Rental.find_by(id: params[:id])
    end
    
    def create
        @rental = Rental.new(rental_params)
        if @rental.save
          redirect_to @rental, notice: "Your rental has been succesfully added."
        else 
            render :new
        end 
    end

    def update
        @rental = Rental.find_by(id: params[:id])
        @rental.update(rental_params)
        redirect_to @rental, notice: "Your rental has been succesfully updated."
    end
    
    def destroy
        @rental = Rental.find_by(id: params[:id])
        @rental.destroy
        redirect_to root_path
    end 
    
    
    private

    def rental_params
        params.require(:rental).permit(
            :street_add,
            :city,
            :state,
            :owner,
        )
    end

end

感謝所有回答的人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM