Tag Archives: Rails 4

Rails 4 accepts_nested_attributes with AJAX

Have been trying for some time to add some usability features to an application with nested attributes and not had much success so far. Maybe it’s only written as theoretically possible and no-one actually uses it (a familiar experience with these ‘easy’ Rails features) so I’m including a few reference links that may or may not be useful later on pending a fuller write-up.

http://stackoverflow.com/questions/20436526/rails-accepts-nested-attributes-for-with-f-fields-for-and-ajax which leads to …. ‘server not found’.

Rails integration testing working with ldap login

Now, I have spent a long time trying to get this working and have only persisted because I know how important it is to get automated testing working.

My job is harder because all my controllers require authentication and authorisation and  get the impression that they’re not well-suited to unit testing. Okay, kinda makes sense.

Integration testing s where we test the path through an application, including login.

http://guides.rubyonrails.org/testing.html#integration-testing-examples gives some clues as to how to start with an application that needs a login but try as I did there’s no way I could get a test to login. No errors, just didn’t work. How very Rails.

My post_via_redirect call was,

post_via_redirect "/sessions/login", username: "username", password: "password"

But then  came across http://apidock.com/rails/ActionController/Integration/Session/post_via_redirect#1369-post-user-authentication-info-to-sessions-create-action

which described the post_via_redirects as

post_via_redirect(?sessions?, {:user=>{:email=> user.email, :password => user.password}})

so my integration test becomes,

require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
 fixtures :roles, :sessions, :application_types
test "login and browse site" 
get "/sessions/
 assert_response :success
post_via_redirect("/sessions/login", {:session => {username: "username", password
 "password"}})
 assert_equal '/applications', path assert_equal 'Signed in successfully.', flash[:notice
get "/application_types/1"
 assert_response :success
 assert assigns(:application_types)
 end
end

And it works.Such a simple difference and such a difference. The hardest thing I’ve yet to do in Rails. Now, how on earth do I unit test my controllers.

Rails 4 form with more than one mulitple select element

When using JQuery’s tokenInput with a Rails application, the model needs to include,

has_many :application_hosts
 has_many :hosts, :through => :application_hosts, :dependent => :destroy
 attr_reader :app_host_tokens
...
def app_host_tokens=(ids)
 self.host_ids = ids.split(',')
 end

And with the strong_parameters in the controller looking like:

params.require(:application).permit(:name, ..., :app_host_tokens )

This can be repeated many times.

If we’re using a select form element with multiple => true, then the model looks like,

has_many :app_user_applications
 has_many :app_users, :through => :app_user_applications # , :class_name => "AppUser", :foreign_key => "app_user_id", :dependent => :destroy
...
def app_user_tokens=(ids)
  self.app_user_ids = ids
end

And with the strong_parameters in the controller looking like:

params.require(:application).permit(:name, :... :app_user_ids => [])

With the array as the last item in the list. Only one array is allowed here. So what if we have more than one select element with multiple => true?

The model construction is the same format, as is the _form partial template, so what we have to figure out is how to allow the parameter list to be accepted by the controller; just adding another array like,

params.require(:application).permit(:name, :... :app_user_ids => [], :db_name_ids => [])

Will just yield an ‘unpermitted parameter’ error. The solution is, not having found any references to it on t’internet, the following:

params.require(:application).permit(:name, :... { db_name_ids: [] }, { app_user_ids: [] } )

Hope someone finds this useful.

Nearly got there with Rails

After having spent a productive day or so working on a new Rails (4) site to manage temporary AD/LDAP accounts for synchronisation by MS FIM, I have suddenly hit a go-slow when getting the search form to work.

What used to work with Rails 3 just didn’t play ball and trying to debug even the simplest UJS stuff is a nightmare. Javascript is evil, plain and simple.

But I have managed to get the rake db:seed to populate the database with good values and I will prepare proper test scripts.

I had forgotten how much fun doing Rails sites is, though I still wish I could do even the most basic web design.