Carrierwave - 从索引页上传照片
Carrierwave - upload photo from index page
我有两个关联模型,船has_many :pictures
,图片模型belongs_to :boat
,这些是嵌套路线;
resources :boats, except: :destroy do
resources :pictures
end
所以,我有索引页,所有的船都在那里显示。但我也想在那里添加上传照片表单,之后我会添加一个 JQuery 选项。把我的表格有问题。因为我在 index
页面中,所以我必须从索引页面将表单发送到 #create 操作;
这里是图片控制器;
class PicturesController < ApplicationController
before_action :logged_in_user
before_filter :load_parent
def index
@pictures = @boat.pictures.all
end
def new
@picture = @boat.pictures.new
end
def show
@picture = @boat.pictures.find(params[:id])
end
def create
@picture = @boat.pictures.new(picture_params)
if @picture.save
#flash[:success] = "Continue from here"
redirect_to boat_pictures_path(@boat)
#redirect_to boat_path(@boat)
else
render 'new'
end
end
def edit
@picture = Picture.find(params[:id])
end
def update
@picture = @boat.pictures.find(params[:id])
if @picture.update_attributes(picture_params)
flash[:notice] = "Successfully updated picture."
render 'index'
else
render 'edit'
end
end
def destroy
@picture = @boat.pictures.find(params[:id])
@picture.destroy
flash[:notice] = "Successfully destroyed picture."
redirect_to boat_pictures_path(@boat)
#redirect_to boat_path(@boat)
end
private
def picture_params
params.require(:picture).permit(:name, :image)
end
def load_parent
@boat = Boat.find(params[:boat_id])
end
end
这里是索引视图;
<% @pictures.each do |pic| %>
</br>
<%= pic.name %>
<%= image_tag pic.image_url(:thumb).to_s %>
<%= link_to "edit", edit_boat_picture_path(@boat, pic) %> |
<%= link_to 'Destroy', boat_picture_path(@boat, pic), confirm: 'Are you sure?', method: :delete %> |
</br>
<% end %>
<%= link_to "add", new_boat_picture_path(@boat, @picture) %>
<%= form_for Picture.new, :as => :post, :url => new_boat_picture_path(@boat, @picture) do |f| %>
<%= f.file_field :image, multiple: true, name: "picture[image]" %>
<% end %>
我认为 <%= form_for Picture.new, :as => :post, :url => new_boat_picture_path(@boat, @picture) do |f| %>
是 url 不正确的问题。没有提交按钮来发送它,但这是因为我将使用 JQuery 文件上传。
是的,这部分肯定是错的。
在您的控制台中输入:rake routes
并在列表中查找正确的路径。
我想会是boat_pictures_path(@boat)
我有两个关联模型,船has_many :pictures
,图片模型belongs_to :boat
,这些是嵌套路线;
resources :boats, except: :destroy do
resources :pictures
end
所以,我有索引页,所有的船都在那里显示。但我也想在那里添加上传照片表单,之后我会添加一个 JQuery 选项。把我的表格有问题。因为我在 index
页面中,所以我必须从索引页面将表单发送到 #create 操作;
这里是图片控制器;
class PicturesController < ApplicationController
before_action :logged_in_user
before_filter :load_parent
def index
@pictures = @boat.pictures.all
end
def new
@picture = @boat.pictures.new
end
def show
@picture = @boat.pictures.find(params[:id])
end
def create
@picture = @boat.pictures.new(picture_params)
if @picture.save
#flash[:success] = "Continue from here"
redirect_to boat_pictures_path(@boat)
#redirect_to boat_path(@boat)
else
render 'new'
end
end
def edit
@picture = Picture.find(params[:id])
end
def update
@picture = @boat.pictures.find(params[:id])
if @picture.update_attributes(picture_params)
flash[:notice] = "Successfully updated picture."
render 'index'
else
render 'edit'
end
end
def destroy
@picture = @boat.pictures.find(params[:id])
@picture.destroy
flash[:notice] = "Successfully destroyed picture."
redirect_to boat_pictures_path(@boat)
#redirect_to boat_path(@boat)
end
private
def picture_params
params.require(:picture).permit(:name, :image)
end
def load_parent
@boat = Boat.find(params[:boat_id])
end
end
这里是索引视图;
<% @pictures.each do |pic| %>
</br>
<%= pic.name %>
<%= image_tag pic.image_url(:thumb).to_s %>
<%= link_to "edit", edit_boat_picture_path(@boat, pic) %> |
<%= link_to 'Destroy', boat_picture_path(@boat, pic), confirm: 'Are you sure?', method: :delete %> |
</br>
<% end %>
<%= link_to "add", new_boat_picture_path(@boat, @picture) %>
<%= form_for Picture.new, :as => :post, :url => new_boat_picture_path(@boat, @picture) do |f| %>
<%= f.file_field :image, multiple: true, name: "picture[image]" %>
<% end %>
我认为 <%= form_for Picture.new, :as => :post, :url => new_boat_picture_path(@boat, @picture) do |f| %>
是 url 不正确的问题。没有提交按钮来发送它,但这是因为我将使用 JQuery 文件上传。
是的,这部分肯定是错的。
在您的控制台中输入:rake routes
并在列表中查找正确的路径。
我想会是boat_pictures_path(@boat)