Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # edit call partial _form.haml, from _form, in the loop for photo shows, render a partial call: _photo_fields
- # here is a part of _form.haml:
- = simple_form_for @travail, remote: true do |article|
- .
- .
- .
- %h3 Photos
- #photos
- = article.simple_fields_for :photos do |photo|
- = render partial: 'photo_fields', locals: { f: photo }
- %div{ :style => "display: block; position: relative; clear: both;"}
- %br= link_to_add_association 'add photo', article, :photos
- .actions
- = article.button :magic_submit, cancel: travails_path
- # here is _photo_fields.haml
- .nested-fields
- %hr
- .col-md-5
- = f.hidden_field :retained_photo
- = f.input :photo, as: :file, label: "Add a photo", class: "upload", :input_html => { onchange: "readURL(this);" }
- - if f.object.photo
- = image_tag(f.object.photo.thumb('140x140!').url, alt: f.object.title, class: 'preview')
- - else
- %img.preview
- .col-md-7
- = f.input :title
- = f.input :description
- = link_to_remove_association 'remove', f
- #here is important part of my controller: travails_controller.rb:
- class TravailsController < ApplicationController
- before_action :set_travail, only: [:show, :edit, :update, :destroy]
- before_action :authenticate_user!, only: [:edit, :update, :destroy, :create, :new]
- before_action :role_required, except: [ :index, :show ]
- private
- def set_travail
- @travail = Travail.find(params[:id])
- end
- def travail_params
- params.require(:travail).permit(:title, :description, :content, :location, :company,
- :user_id, :image, :wconcern_id,
- photos_attributes: [:id, :title, :categorie,
- :description, :photo, :_destroy])
- end
- end
- # this is important part of my model for Travail: travail.rb
- class Travail < ActiveRecord::Base
- has_many :photos
- accepts_nested_attributes_for :photos, :allow_destroy => true
- end
- # part of my photo.rb model:
- class Photo < ActiveRecord::Base
- belongs_to :travail
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement