Guest User

Untitled

a guest
Feb 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. ###main view
  2.  
  3. <%= javascript_include_tag ('insertphoto')%>
  4.  
  5.  
  6. <%= text_area 'article', 'body'%>
  7.  
  8. <a HREF="javascript:void(0)" onClick="myPopup()">insert image</a>
  9.  
  10.  
  11. ### image_upload.rhtml
  12. <%= start_form_tag({:action => 'image_upload'}, :multipart => true) %>
  13.  
  14. <p>
  15. <b>Picture:</b><br />
  16. <%= file_field_tag "picture" %>
  17. </p>
  18.  
  19. <p><%= submit_tag "Save" %></p>
  20. <%= end_form_tag %>
  21.  
  22. insertAtCursor(document.myform.article_body, ‘photo_url’);
  23.  
  24.  
  25.  
  26.  
  27. ##controller
  28.  
  29.  
  30. def image_upload
  31. if request.post?
  32. @myloc="#{RAILS_ROOT}" + '/public/images/' + "#{Time.now.to_i}" + '.jpg'
  33. File.open(@myloc , "wb") do |f|
  34. f.write(params['picture'].read)
  35. end
  36. end
  37. end
  38.  
  39. ### js functions
  40.  
  41.  
  42.  
  43. function myPopup(){
  44. window.open('/writer/image_upload', 'MyPopUp', 'width=632,height=270,toolbar=0,scrollbars=0,screenX=200,screenY=200,left=200,top=200')
  45. }
  46.  
  47. function insertAtCursor(myField, myValue) {
  48. //IE support
  49. if (document.selection) {
  50. myField.focus();
  51. sel = document.selection.createRange();
  52. sel.text = myValue;
  53. }
  54. //MOZILLA/NETSCAPE support
  55. else if (myField.selectionStart || myField.selectionStart == ‘0′) {
  56. var startPos = myField.selectionStart;
  57. var endPos = myField.selectionEnd;
  58. myField.value = myField.value.substring(0, startPos)
  59. + myValue
  60. + myField.value.substring(endPos, myField.value.length);
  61. } else {
  62. myField.value += myValue;
  63. }
  64. }
Add Comment
Please, Sign In to add comment