Carrierwave - 从索引页面上传照片

时间:2015-04-23 14:37:09

标签: jquery ruby-on-rails indexing carrierwave

我有两个关联模型,Boats 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| %>是网址不正确的问题。没有提交按钮来发送它,但这是因为我将使用JQuery文件上传。

1 个答案:

答案 0 :(得分:1)

是的,这部分肯定是错误的。

在您的控制台中输入:rake routes并在列表中查找正确的路径。

我认为这将是boat_pictures_path(@boat)