I have established a connection to Active Directory through ldap/net. I am trying to pull AD attributes and values.
If I use the following code (using values.inspect) the query works.
<% #temp_search.each do |user| %>
TS = <%= user.sn %> <br>
<% user.each do |attribute, values| %>
<%= attribute %> <br>
<% values.each do |value| %>
<%= value.inspect %><br>
<% end %>
<hr style="border-color: red">
<% end %>
<% end %>
The portion of my controller doing the ldap query is s
filter = Net::LDAP::Filter.eq( "sn", "mendla" )
treebase = "dc=ccttapes1,dc=com"
#temp_search = ldap.search( :base => treebase, :filter => filter )
p ldap.get_operation_result
What I can't seem to get to is to just get the value for one particular attribute - for example, givenname.
I see results such as
cn
"Chris G. Mendla"
sn
"Mendla"
description
"Test account 1 for rails apps - DO NOT CHANGE PW"
givenname
"Chris"
initials
"G"
distinguishedname
"CN=Chris G. Mendla,OU=Users CCT,DC=CCTTAPES1,DC=com"
However, if I change <%= value.inspect %><br> to <%= value.givenname %><br> I get an error of
NoMethodError in Observations#index
Showing C:/Users/cmendla/RubymineProjects/employee_observations/app/views/observations/index.html.erb where line #61 raised:
undefined method `givenname' for "CN=Christopher Mendla,OU=Users CCT,DC=CCTTAPES1,DC=com":Net::BER::BerIdentifiedString
Rails.root: C:/Users/cmendla/RubymineProjects/employee_observations
Application Trace | Framework Trace | Full Trace
app/views/observations/index.html.erb:61:in `block (3 levels) in _app_views_observations_index_html_erb__474218211_78240600'
app/views/observations/index.html.erb:60:in `each'
app/views/observations/index.html.erb:60:in `block (2 levels) in _app_views_observations_index_html_erb__474218211_78240600'
app/views/observations/index.html.erb:58:in `block in _app_views_observations_index_html_erb__474218211_78240600'
app/views/observations/index.html.erb:56:in `each'
app/views/observations/index.html.erb:56:in `_app_views_observations_index_html_erb__474218211_78240600'
and if I try ``<%= givenname.value %>` I get
NameError in Observations#index
Showing C:/Users/cmendla/RubymineProjects/employee_observations/app/views/observations/index.html.erb where line #61 raised:
undefined local variable or method `givenname' for #<#<Class:0x9cd0230>:0x9fadaf0>
Rails.root: C:/Users/cmendla/RubymineProjects/employee_observations
Application Trace | Framework Trace | Full Trace
app/views/observations/index.html.erb:61:in `block (3 levels) in _app_views_observations_index_html_erb__474218211_83715540'
app/views/observations/index.html.erb:60:in `each'
app/views/observations/index.html.erb:60:in `block (2 levels) in _app_views_observations_index_html_erb__474218211_83715540'
app/views/observations/index.html.erb:58:in `block in _app_views_observations_index_html_erb__474218211_83715540'
app/views/observations/index.html.erb:56:in `each'
app/views/observations/index.html.erb:56:in `_app_views_observations_index_html_erb__474218211_83715540'
Request
My goal is to be able to search for an AD record by first and last names and then pull values for attributes such as mail or memberof.
(I am using ldap-net
In your example #temp_search should be an array of users that meet your search criteria. Each one of those users is a Net::LDAP::Entry object. On those objects you can call methods are available corresponding to the users attributes.
If only one user is returned, the Net::LDAP::Entry object will still be inside an array. In that case you could call something like:
#temp_search.first.cn
You can also call:
#temp_search.first.attribute_names to see all available attributes for that object.
For example, you could do something like:
<% #temp_search.each do |user| %>
#call user attributes
user.cn
user.memberof
#etc, other attributes
<% end %>
Related
Hi i am a beginner in rails and encounter some issues when i try to run rspec capybara test.
require 'rails_helper'
describe "Upload Process", :type => :feature do
it "Can upload a file" do
visit new_document_path
page.attach_file('file', '/Users/yaomin/Desktop/my_travel_pic/uploadtest.jpg', visible: false)
click_button 'Upload'
page.should have_content("Uploadtest")
end
end
but i got the error
Failures:
1) Upload Process Can upload a file
Failure/Error: attach_file('file', '/Users/yaomin/Desktop/my_travel_pic/uploadtest.jpg', visible: false)
Capybara::ElementNotFound:
Unable to find file field "file"
# /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/node/finders.rb:44:in `block in find'
# /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/node/base.rb:85:in `synchronize'
# /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/node/finders.rb:33:in `find'
# /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/node/actions.rb:256:in `attach_file'
# /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/session.rb:769:in `block (2 levels) in <class:Session>'
# /Users/yaomin/.rvm/gems/ruby-2.3.1/gems/capybara-2.13.0/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
# ./spec/features/upload_test.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.40034 seconds (files took 1.99 seconds to load) 1 example, 1 failure
Failed examples:
rspec ./spec/features/upload_test.rb:5 # Upload Process Can upload a file
my view codes are
h1 Listing documents
table
thead
tr
th
th
th
tbody
- #documents.each do |document|
tr
td = link_to 'Show', document
td = link_to 'Edit', edit_document_path(document)
td = link_to 'Destroy', document, data: { confirm: 'Are you sure?' }, method: :delete
br
= link_to 'New Document', new_document_path
below is the new document view
h1 New document
= render 'form'
= link_to 'Back', documents_path
- if #document.folder
= link_to "Back to '#{#document.folder.name}' Folder", browse_path(#document.folder)
- else
= link_to "Back", root_url
and my form partial
= form_for #document do |f|
- if #document.errors.any?
#error_explanation
h2 = "#{pluralize(#document.errors.count, "error")} prohibited this document from being saved:"
ul
- #document.errors.full_messages.each do |message|
li = message
= f.label :file
= f.file_field :file
- f.hidden_field :folder_id
= f.submit "Upload"
For information, i can actually upload files manually but the upload test i run fails.
Thanks in advance for all your help :)
The first parameter passed to attach_file needs to be either the id, name, or associated labels text. In your case that's probably not 'file'. You state the associated label text is "Choose File" so probably something like
attach_file('Choose File', file_path)
is what you want (impossible to say exactly what the first parameter should be without the actual HTML). Other things that may work would be
attach_file('document_file', file_path) # match the elements id
attach_file('document[file]', file_path) # match the elements name
but again, the first parameter could be slightly different, the HTML will show what they should be.
The other issue that is common with file inputs is that they may be hidden with CSS (made transparent, etc) in order to style the same across browsers. If that is the case and you are using a JS capable driver you can use something like
attach_file('Choose File', file_path, make_visible: true)
which will attempt to make the field visible, set the file, and then restore the original CSS.
I am currently working on an RSS feed that was working for a while but now has an encoding issue. After trying many of the solutions here unsuccessfully, I have deduced that I may be having issues with the feed parser.
The error starts with the index page:
ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8):
11:
12: <div class="entry_wrapper">
13: <div class="entry_box">
14: <% feed.entries.each do |entry| %>
15: <p class="entry_title"><%= sanitize link_to entry.title, entry.url %></p>
16:
17:
app/models/feed.rb:6:in `entries'
app/views/feeds/index.html.erb:14:in `block in _app_views_feeds_index_html_erb__2672739530113604393_70126099705280'
app/views/feeds/index.html.erb:5:in `_app_views_feeds_index_html_erb__2672739530113604393_70126099705280'
Here is larger section of the Feed#Index page:
<% #feeds.each do |feed| %> # Line 5 here
<p class="feed_url">
<%= link_to feed.url, feed %>
<%= link_to "Edit", edit_feed_path(feed), class: "blue" %>
<%= link_to "Delete", feed_path(feed), method: :delete, data: { confirm: 'Are you sure you want to delete this feed url?' }, class: "blue" %>
</p>
<div class="entry_wrapper">
<div class="entry_box">
<% feed.entries.each do |entry| %>
<p class="entry_title"><%= sanitize link_to entry.title, entry.url %></p>
I deleted the Feed URL's in the console so the page rendered fine without any urls to pull from. However, as soon as I added one, I got the same error as before.
I tried testing the encoding of the Feed entries in the console and got the following error:
2.1.1 :001 > g = Feed.last
Feed Load (0.1ms) SELECT "feeds".* FROM "feeds" ORDER BY "feeds"."id" DESC LIMIT 1
=> #<Feed id: 9, name: nil, created_at: "2016-01-18 05:01:54", updated_at: "2016-01-18 05:01:54", url: "http://feeds.feedburner.com/MattsTravelSite">
2.1.1 :002 > g.entries
Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/entities.rb:77:in `gsub'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/entities.rb:77:in `decode'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/entity_decoder.rb:14:in `decode'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/entity_decoder.rb:5:in `try_decode'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/sax_parser.rb:151:in `on_text'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:541:in `_rule_33'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:239:in `block in each_token'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:237:in `call'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:237:in `add_token'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:439:in `on_element_end'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:190:in `advance_native'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:190:in `block in advance'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:137:in `read_data'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:189:in `advance'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:236:in `each_token'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:269:in `parse'
... 20 levels...
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `block in load'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/danieluribe/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/danieluribe/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
I double-checked to see if the website I am pulling from is in UTF-8 encoding at the W3C-Validator website:
This document was successfully checked as well-formed XML!
Result: Passed, 1 warning(s)
Address:
http://feeds.feedburner.com/MattsTravelSite
Encoding: utf-8
Doctype: XML
Root Element: feed
Root Namespace: http://www.w3.org/2005/Atom
I tried "forcing" the encoding in the model, but I think my coding skills are a little lacking in terms of creating methods. Here is the entries model I created originally for the Feed that is being used in the Feed#Index.
class Feed < ActiveRecord::Base
has_many :items
def entries(num = 3)
Feedjira::Feed.add_common_feed_entry_element("img")
feed = Feedjira::Feed.fetch_and_parse(url)
feed.entries.take(num)
# add_items(feed.entries) # Saving items to database
end
....
Since this model was working before, I'm not sure what happened. I did take a few months from the app so I don't know if anything changed with Feedjirra. Any help would be greatly appreciated.
It was working few days ago, don't know what had went wrong...
**undefined method `name' for nil:NilClass**
Extracted source (around line #26):
23: %td= number_with_precision(employee.compensation_leave_balance, precision:1)
24: #calendar.tab-pane.fade
25: = calendar(:year => 2012, :month => 6, :first_day_of_week => 1, summary: "Leave Calendar", calendar_title: "June", month_header: true) do |date|
26: - render_leave_calendar_cell(date)
27: #trash.tab-pane.fade
28: = render 'table', leaves: #leaves.where(deleted: true)
app/helpers/leaves_helper.rb:11:in `block in events_for'
app/helpers/leaves_helper.rb:10:in `events_for'
app/helpers/leaves_helper.rb:4:in `render_leave_calendar_cell'
app/views/human_resources/leaves/index.html.haml:26:in `block in _app_views_human_resources_leaves_index_html_haml__145883348_88978910'
app/helpers/calendar_helper.rb:146:in `call'
app/helpers/calendar_helper.rb:146:in `block in calendar'
app/helpers/calendar_helper.rb:145:in `upto'
app/helpers/calendar_helper.rb:145:in `calendar'
app/views/human_resources/leaves/index.html.haml:25:in `_app_views_human_resources_leaves_index_html_haml__145883348_88978910'
really don't know what went wrong
a/h/leaves_helper.rb
1 module LeavesHelper
2 def render_leave_calendar_cell(date)
3 html = content_tag(:span, date.day, class: 'dayDisplay')
4 html += content_tag(:div, events_for(date))
5 raw(html)
6 end
7
8 def events_for(date)
9 html = ""
10 current_company.leaves.where("start_date <= '#{date}' and return_date > '#{date}'").where(deleted: false).each do |leave|
11 html += content_tag(:div, leave.applicant.name, class: 'leaveName')
12 end
13 raw html
14 end
could it be the date nil? how to fix this ><
much appreciate
Billy
as abhas already stated, leave.applicant is nil for at least one of the leaves.
go to your database and find out which it is. then figure out what to do with your leaves. delete them too, re-add the missing applicant or what ever data migration might be sensible.
a quick fix would be to skip if an applicant is missing:
html += content_tag(:div, leave.applicant.name, class: 'leaveName') if leave.applicant.present?
i would also have a look if you properly configured the delete cascades in your application. this often causes such problems. if you want to enforce safety in this regard, you should add database constraints, that ensure that no referenced entity gets deleted.
Im trying to find all records that have similar tags to the currently viewed record.
My controller has:
def show
#tattoo =Tattoo.find(params[:id])
tags = #tattoo.style_list.join(", ")
#tattoos = Tattoo.tagged_with(tags, :any => true).limit(6)
end
(bonus points if anyone can tell me how to randomize the order of records in the arrary)
My view just loops through the array.
Anyway, it works almost all the time but I noticed it breaks occasionally and while troubleshooting I found that it breaks when I use tagged_with("jesse smith", :any => true) but it works when I try tagged_with("jason stephan", :any => true) or tagged_with("black ink", :any => true)
So each term has a space in it but for whatever reason 'jesse smith' kills the action.
My console shows that I have a routing error too:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"tattoos", :member_id=>nil, :id=>#<Tattoo id: 170, description: "", status: "approved", member_id: nil, created_at: "2011-10-25 23:08:17", updated_at: "2011-11-17 16:56:55", file_file_name: "starry-eyed-rabid-squirrelweb.jpg", file_content_type: "image/jpeg", file_file_size: 294782, file_updated_at: "2011-10-25 23:08:17", album_id: nil, position: 116, favorite_count: 0, share_count: 1, file_remote_url: "http://www.jessesmithtattoos.com/wp-content/gallery...">}):
22: <ol class="small_tattoos">
23: <% #tattoos.each do |t| %>
24: <li>
25: <%= link_to image_tag(t.file.url(:tiny),:alt=>"#{t.style_list}, rtattoos, tattoos"), member_tattoo_path(t.member, t) %>
26: </li>
27: <% end %>
28: </ol>
app/views/index/show.html.erb:25:in `block (2 levels) in _app_views_index_show_html_erb___1839804211534816245_69842632179360__4333294961394575926'
app/views/index/show.html.erb:23:in `block in _app_views_index_show_html_erb___1839804211534816245_69842632179360__4333294961394575926'
app/views/index/show.html.erb:11:in `_app_views_index_show_html_erb___1839804211534816245_69842632179360__4333294961394575926'
So why does the one term cause a routing error and not the others?
I guess you have problem with path helper:
member_tattoo_path(t.member, t)
I see in your error description that
:member_id=>nil
So it turns out that tattoo tagged with jesse smith hasn't corresponding association with name 'member', and path helper, which need valid id, throws exception.
I realize there's a nearly identical question about this here but the fix there did not fix my problem.
My Authlogic is working in my development environment but not in my production environment. Below is the error I'm getting.
Started GET "/users/new" for 71.205.207.120 at Tue Feb 22 19:24:49 -0800 2011
Processing by UsersController#new as HTML
Rendered users/_form.html.erb (19.3ms)
Rendered users/new.html.erb within layouts/application (21.6ms)
Completed in 38ms
ActionView::Template::Error (undefined method `password' for #<User:0x2b95caea8590>):
21: </div>
22: <div class="field">
23: <%= f.label :password %><br />
24: <%= f.password_field :password %>
25: </div>
26: <div class="field">
27: <%= f.label :password_confirmation %><br />
app/views/users/_form.html.erb:24:in `_app_views_users__form_html_erb___1397135998_23961177217120_1124190'
app/views/users/_form.html.erb:1:in `_app_views_users__form_html_erb___1397135998_23961177217120_1124190'
app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb___1081160896_23961177226000_0'
app/controllers/users_controller.rb:29:in `new'
I didn't misspell any of the database columns, I'm already doing acts_as_authentic and I have restarted my server, so I'm not sure what else to try. It seems like it must be something to do with my database but my database structure seems to be identical.
Database password field (and/or salt field) may not match any of the defaults!!!
I've had this problem twice now and in both cases it was tricky because of two things:
1) The message "undefined method 'password' really (often) means password field not found in database using defaults.
2) The location to ADD your database field for password (or salt for that matter) is NOT anywhere in your applications directory, it in your ruby gem directory, something like ~/.gem/ruby/1.8/gems/authlogic-2.1.6/lib/authlogic/acts_as_authentic !
The field to change is password.rb. Add your field name(s) in to the list, e.g. i added pd_hashed changed:
module Config
# The name of the crypted_password field in the database.
#
# * Default: :crypted_password, :encrypted_password, :password_hash, or :pw_hash
# * Accepts: Symbol
def crypted_password_field(value = nil)
rw_config(:crypted_password_field, value, first_column_to_exist(nil, :pd_hashed, :crypted_password, :encrypted_password, :password_hash, :pw_hash))
end
Do the same for the salt field.
Restart and this may help.