2016-03-27 5 views
0

:item_galleriesからのネストされたアトリビュートを持つitemモデルが複数あります。ネストされた画像でアイテムを作成できますが、編集に問題があります。ネストされたアトリビュートをレール形式で表示および更新する

アイテムに付いているそれぞれのイメージを表示し、それぞれを編集できるようにしたいと思います。

誰でも私に仮想のクッキーまたはパイを手伝ってもらえますか? 「アイテムフォームビュー」については

<%= f.fields_for :item_galleries do |p| %> 
    <%= p.label :image %> 
    <%= link_to "Edit Attachment", edit_item_gallery_path(p) %> 
    <%= p.file_field :image, :multiple => true, name: "item_galleries[image][]" %> 
<% end %> 

私は右の次の編集添付ファイルのリンクに画像を表示したいと思います。 edit_item_galleries_path(p)は画像だけでIMAGE_TAGを使用表示するには"http://localhost:3000/item_galleries/%23%3CActionView::Helpers::FormBuilder:0x007ffce80b2358%3E/edit"

答えて

1

をであるために私をもたらすこと

def edit 
    @item = Item.find(params[:id]) 
    @item_galleries = @item.item_galleries.all 
    end 
    def update 
    respond_to do |format| 
     if @item.update(item_params) 
     format.html { redirect_to @item, notice: 'Item was successfully updated.' } 
     else 
     format.html { render :edit } 
     end 
    end 
    end 

現在リンク:

これはitems_controllerでの編集機能です。あなたの編集リンクを修正するには

<%= p.label :image %> 
<%= link_to "Edit Attachment", edit_item_gallery_path(p) %> 
<%= image_tag p.image if p.object.image.present? %> 

:あなたはあなたのURLにidと全体ではなく、フォームビルダヘルパーを渡したいです。 p.indexを使用すると、永続性がある場合は要素のidを返し、まだ永続化されていない場合は生成されたuidを返します。

<%= link_to "Edit Attachment", edit_item_gallery_path(p.index) %> 

だから、全体のコードは次のようになります。私はコメントでそれを指摘し、おかげでイメージを固定 :

<%= p.label :image %> 
<%= link_to "Edit Attachment", edit_item_gallery_path(p.index) %> 
<%= image_tag p.image if p.image.present? %> 

編集。

+0

私はこれを試しましたが、#のための "未定義のメソッド'イメージ 'エラーを受け取ります。 – Dileet

+0

p.indexを使用するとエラーが発生しました " id '= 0」となる。だから、私がしたのはp.index + 1だけだった。ちょうど別の問題をソートする必要があります。 – Dileet

+0

それを考え出してください!正しい方向を指してくれてありがとう。これは、p.object.image.presentの場合、<%= image_tag p.object.imageを修正しましたか? %> – Dileet