Rails #show 页面仅反映第一个种子对象而不是所有种子对象?

如何解决Rails #show 页面仅反映第一个种子对象而不是所有种子对象?

我有一个显示页面连接到我的租赁对象。当迭代属于租赁的评论时,它只返回第一个评论对象。我有几个 Review 对象通过我的种子文件连接到每个 Rental ,但似乎无法让所有这些对象都反映出来。这是我的文件

租金/show.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" %>

我的租赁控制器


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,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.",user_id: 2)

Review.create(title: "I loved it,user_id: 3)


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

Review.create(title: "Beautiful Trees in Backyard",body: "Beautiful trees in backyard although the owls made strange noises at night.",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.",user_id: 3)

puts "data loaded success"

我想要完成的是为每次租赁列出多条评论。这是我在rentals#show 页面上的截图与所有评论的截图:

Rental Show Page Rental Index

非常感谢任何帮助!

解决方法

更新:我能够通过更新显示页面以反映 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" %>

并像这样更新控制器:


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

感谢所有回答的人!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?