我有兩個相關的模型,船has_many :pictures
,圖片模型belongs_to :boat
,這些是嵌套的路線;Carrierwave - 從索引頁上載照片
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文件上傳。
你能清理html代碼嗎?有''沒有任何表和關閉'
'沒有任何開放'' – jigfox