簡體   English   中英

載波-從索引頁面上傳照片

[英]Carrierwave - upload photo from index page

我有兩個關聯的模型,Boats has_many :pictures ,Picture Model belongs_to :boat ,它們是嵌套的路線;

resources :boats, except: :destroy do
      resources :pictures
end

所以,我有索引頁面,所有的船都顯示在那里。 但我想在這里添加JQuery選項后,也要在其中添加上傳照片表格。 我的表格有問題。 正如我在index頁中一樣,我必須將表單發送到索引頁中的#create action;

這是圖片控制器;

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| %> <%= form_for Picture.new, :as => :post, :url => new_boat_picture_path(@boat, @picture) do |f| %>是網址不正確的問題。 沒有要發送的提交按鈕,但這是因為我將使用JQuery文件上傳。

是的,該零件肯定是錯誤的。

在控制台中,輸入: rake routes然后在列表中尋找正確的路徑。

我認為這將是boat_pictures_path(@boat)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM