Guest User

Untitled

a guest
May 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. ## controller:
  2.  
  3. def create
  4. @service = Service.new(params[:service])
  5. @notigroups = Notificationgroupdetail.find(:all)
  6. @notigroups << ["None", "0"]
  7. @hosts = Host.find(:all).collect {|p| [p.name, p.id] }
  8. @hosts << ["None", "0"]
  9. if @service.save
  10. flash[:notice] = "Service has been added!"
  11. redirect_to :action => "index"
  12. else
  13. flash[:error] = "Could not add service. Check error messages."
  14. render :action => "new"
  15. end
  16. end
  17.  
  18. ## view:
  19. <div id="service-new">
  20. <% form_for :service, :url => { :action => "create" } do |f| %>
  21. <fieldset>
  22. <legend>Main</legend>
  23. <dl>
  24. <dt>Name</dt>
  25. <dd><%= f.text_field :name %> <%= error_message_on(:service, :name) %></dd>
  26. <dt>Host</dt>
  27. <dd><%= f.text_field :host %> <%= error_message_on(:service, :host) %></dd>
  28. <dt>Port</dt>
  29. <dd><%= f.text_field :port %> <%= error_message_on(:service, :port) %></dd>
  30. <dt>Protocol check</dt>
  31. <dd><%= f.select :type, getServiceTypes.sort %> <%= error_message_on(:service, :type) %></dd>
  32. <dt>Timeout (seconds)</dt>
  33. <dd><%= f.text_field :timeout, :value => "5" %> <%= error_message_on(:service, :timeout) %></dd>
  34. <dt>Max. response time (ms)</dt>
  35. <dd><%= f.text_field :maxres, :value => "250" %> <%= error_message_on(:service, :maxres) %></dd>
  36. <dt>Notification group</dt>
  37. <dd>
  38. <%= f.select :warninggroup, @notigroups %>
  39. <%= error_message_on(:service, :warninggroup) %>
  40. </dd>
  41. <dt>Link to host</dt>
  42. <dd>
  43. <%= f.select :linkedhost, @hosts %>
  44. <%= error_message_on(:service, :linkedhost) %>
  45. </dd>
  46. </dl>
  47. </fieldset>
  48.  
  49. <fieldset>
  50. <legend>Control</legend>
  51. <%= submit_tag %>
  52. </fieldset>
  53. <% end %>
  54. </div>
  55.  
  56. ## model/validation
  57. class Service < ActiveRecord::Base
  58. validates_presence_of :name
  59. validates_presence_of :host
  60. validates_presence_of :type
  61. validates_presence_of :port
  62. validates_presence_of :maxres
  63. validates_presence_of :timeout
  64. validates_presence_of :warninggroup
  65.  
  66. validates_numericality_of :port
  67. validates_numericality_of :maxres
  68. validates_numericality_of :timeout
  69. validates_numericality_of :warninggroup
  70. end
  71.  
  72. ## html output of the field that is complained about:
  73.  
  74. <dd><div class="fieldWithErrors"><select id="service_type" name="service[type]"><option value="ftp">FTP</option>
  75. <option value="http">HTTP</option>
  76. <option value="imap">IMAP</option>
  77. <option value="none">None</option>
  78. <option value="pop3">POP3</option>
  79. <option value="smtp">SMTP</option>
  80. <option value="ssh">SSH</option></select></div> <div class="formError">can't be blank</div></dd>
  81.  
  82. ## params[:service]
  83. name: somename
  84. port: 80
  85. timeout: 5
  86. linkedhost: 0
  87. warninggroup: 0
  88. type: ftp
  89. maxres: 250
  90. host: somehost
Add Comment
Please, Sign In to add comment