谢谢杰森,
我同意这对新手来说有点大,但要求是我们做一个需要从/向数据库读取和写入的网站。我们通过合作伙伴对 Rails 进行了快速介绍,现在我们独自一人,“有点”迷路了,时间不多了。
这是我收到的错误消息:
房屋中的名称错误#index
未定义的局部变量或方法“图像”
<#:0xb62f7aa4>
提取的源代码(大约第 20 行):
17: <% @homes.each do |home| %>
18: <tr>
19: <td><%= home.name %></td>
20: <td><%= image.home_id %></td>
21: <td><%= home.details %></td>
22: <td><%= home.region %></td>
23: <td><%= home.address %></td>
当我创建数据库表时,我在 home 表中有一个 Image_ID,但我被告知我不需要它,并且在 images 表中只有 Home_ID 就足够了。
我了解该错误是由于 image.home_id 引起的。你有什么意见?我应该将 Image_ID 添加回 home 表以显示相应 home_id 的所有图像还是有其他方法?我希望能够决定将哪些图片显示为主图片,哪些图片显示为较小的图片。
这是我使用的代码:
模型/home.rb
class Home < ActiveRecord::Base
attr_accessible :address, :admin_id, :details, :name, :price, :region
has_many :images
end
模型/image.rb
class Image < ActiveRecord::Base
attr_accessible :image_description, :image_name, :image_path
belongs_to :home
end
意见/家园/index.html.erb
<h1>Listing homes</h1>
<table>
<tr>
<th>Name</th>
<th>Image</th>
<th>Details</th>
<th>Region</th>
<th>Address</th>
<th>Price</th>
<th>Admin</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @homes.each do |home| %>
<tr>
<td><%= home.name %></td>
<td><%= image.home_id %></td>
<td><%= home.details %></td>
<td><%= home.region %></td>
<td><%= home.address %></td>
<td><%= home.price %></td>
<td><%= home.admin_id %></td>
<td><%= link_to 'Show', home %></td>
<td><%= link_to 'Edit', edit_home_path(home) %></td>
<td><%= link_to 'Destroy', home, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Home', new_home_path %>
意见/家园/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @home.name %>
</p>
<p>
<b>Image:</b>
<%= @image.home_id %>
</p>
<p>
<b>Details:</b>
<%= @home.details %>
</p>
<p>
<b>Region:</b>
<%= @home.region %>
</p>
<p>
<b>Address:</b>
<%= @home.address %>
</p>
<p>
<b>Price:</b>
<%= @home.price %>
</p>
<p>
<b>Admin:</b>
<%= @home.admin_id %>
</p>
<%= link_to 'Edit', edit_home_path(@home) %> |
<%= link_to 'Back', homes_path %>
视图/图像/_form.html.erb
<%= form_for(@image) do |f| %>
<% if @image.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@image.errors.count, "error") %> prohibited this image from being saved:</h2>
<ul>
<% @image.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :image_name %><br />
<%= f.text_field :image_name %>
</div>
<div class="field">
<%= f.label :image_path %><br />
<%= f.text_field :image_path %>
</div>
<div class="field">
<%= f.label :image_description %><br />
<%= f.text_area :image_description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
控制器/images_controller.rb
class ImagesController < ApplicationController
# GET /images
# GET /images.json
def index
@images = Image.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @images }
end
end
# GET /images/1
# GET /images/1.json
def show
@image = Image.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @image }
end
end
# GET /images/new
# GET /images/new.json
def new
@image = Image.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @image }
end
end
# GET /images/1/edit
def edit
@image = Image.find(params[:id])
end
# POST /images
# POST /images.json
def create
@image = Image.new(params[:image])
respond_to do |format|
if @image.save
format.html { redirect_to @image, notice: 'Image was successfully created.' }
format.json { render json: @image, status: :created, location: @image }
else
format.html { render action: "new" }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# PUT /images/1
# PUT /images/1.json
def update
@image = Image.find(params[:id])
respond_to do |format|
if @image.update_attributes(params[:image])
format.html { redirect_to @image, notice: 'Image was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.json
def destroy
@image = Image.find(params[:id])
@image.destroy
respond_to do |format|
format.html { redirect_to images_url }
format.json { head :no_content }
end
end
end
非常感谢。我真的很感谢你的帮助!!!