Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. # edit call partial _form.haml, from _form, in the loop for photo shows, render a partial call: _photo_fields
  2. # here is a part of _form.haml:
  3. = simple_form_for @travail, remote: true do |article|
  4. .
  5. .
  6. .
  7. %h3 Photos
  8. #photos
  9. = article.simple_fields_for :photos do |photo|
  10. = render partial: 'photo_fields', locals: { f: photo }
  11. %div{ :style => "display: block; position: relative; clear: both;"}
  12. %br= link_to_add_association 'add photo', article, :photos
  13. .actions
  14. = article.button :magic_submit, cancel: travails_path
  15.  
  16. # here is _photo_fields.haml
  17. .nested-fields
  18. %hr
  19. .col-md-5
  20. = f.hidden_field :retained_photo
  21. = f.input :photo, as: :file, label: "Add a photo", class: "upload", :input_html => { onchange: "readURL(this);" }
  22. - if f.object.photo
  23. = image_tag(f.object.photo.thumb('140x140!').url, alt: f.object.title, class: 'preview')
  24. - else
  25. %img.preview
  26. .col-md-7
  27. = f.input :title
  28. = f.input :description
  29. = link_to_remove_association 'remove', f
  30.  
  31. #here is important part of my controller: travails_controller.rb:
  32. class TravailsController < ApplicationController
  33. before_action :set_travail, only: [:show, :edit, :update, :destroy]
  34. before_action :authenticate_user!, only: [:edit, :update, :destroy, :create, :new]
  35. before_action :role_required, except: [ :index, :show ]
  36.  
  37.  
  38. private
  39. def set_travail
  40. @travail = Travail.find(params[:id])
  41. end
  42.  
  43. def travail_params
  44. params.require(:travail).permit(:title, :description, :content, :location, :company,
  45. :user_id, :image, :wconcern_id,
  46. photos_attributes: [:id, :title, :categorie,
  47. :description, :photo, :_destroy])
  48. end
  49. end
  50.  
  51. # this is important part of my model for Travail: travail.rb
  52. class Travail < ActiveRecord::Base
  53.  
  54. has_many :photos
  55. accepts_nested_attributes_for :photos, :allow_destroy => true
  56.  
  57.  
  58. end
  59.  
  60. # part of my photo.rb model:
  61. class Photo < ActiveRecord::Base
  62. belongs_to :travail
  63.  
  64.  
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement