我正在尝试使用Railscast的Carrierwave教程作为起点,使我的Image模型成为我的List模型的子项。我更改了路线但是当我尝试上传图片时,转到此URL localhost:300 / listings / 1 / images / new我收到此错误:未定义方法`images_path'用于#&lt;#:0x00000109abfd88&gt; < /强>
有任何帮助吗?感谢。
image_controller.rb
class ImagesController < ApplicationController
before_action :set_image, only: [:show, :edit, :update, :destroy]
def index
# I changed this so the images would appear on localhost:300/listings/3/images
# Don't know if it's the best way to accomplish that
@images = Listing.find(params[:listing_id]).images
end
def new
@image = Image.new(:listing_id => params[:listing_id])
end
def edit
end
def create
@image = Image.new(image_params)
respond_to do |format|
if @image.save
format.html { redirect_to :back, notice: 'Image was successfully created.' }
format.json { render action: 'show', status: :created, location: @image }
else
format.html { render action: 'new' }
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @image.update(image_params)
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
def destroy
@image.destroy
respond_to do |format|
format.html { redirect_to :back }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_image
@image = Image.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def image_params
params.require(:image).permit(:file, :listing_id)
end
end
images.rb表格
<%= form_for [@listing, @image], :html => { :multipart => true } 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 %>
<%= f.hidden_field :listing_id %>
<div>
<%= f.label :file, 'Upload Images' %>
<%= f.file_field :file %>
</div>
<div>
<%= f.submit %>
</div>
<% end %>
答案 0 :(得分:0)
您尚未在控制器中初始化@listing
对象,但在视图中使用它。
进行此更改
def new
@listing = Listing.find params[:listing_id]
@image = Image.new(:listing_id => params[:listing_id])
end