2015-04-23 58 views
0

我有兩個相關的模型,船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文件上傳。

+0

你能清理html代碼嗎?有'​​'沒有任何表和關閉'

'沒有任何開放'

' – jigfox

回答

1

是的,零件肯定是錯的。

在您的控制檯中鍵入:rake routes並在列表中查找正確的路徑。

我認爲這將是boat_pictures_path(@boat)

+0

謝謝你的幫助 – Shalafister