I'm getting this error when I try to use redirect_via_turbolinks_to in my controller
undefined method `redirect_via_turbolinks_to' for
I extracted this method from the doc:
Triggering a Turbolinks visit manually
You can use Turbolinks.visit(path) to go to a URL through Turbolinks.
You can also use redirect_via_turbolinks_to in Rails to perform a redirect via Turbolinks.
I'm using turbolinks '1.3.1' in rails 3.2.14
Turbolinks.visit(path) can be used in your JS/Coffee assets or within some embedded JS:
$ ->
$(document).dontBeNaughty()
jQuery.fn.dontBeNaughty = ->
$(".ReportsHistory").on "click", ->
action = $(this).data('url')
Turbolinks.visit("/reports?" + action)
You can use the redirect_via_turbolinks_to in your controller instead of rendering a template.
More information can be seen in the source of turbolinks here:
For anyone that comes across this, redirect_via_turbolinks_to is deprecated in favor of redirect_to url, turbolinks: true
Source: https://github.com/turbolinks/turbolinks-classic/blob/master/CHANGELOG.md
Related
background
I am trying to migrate an old Rails 2 (Ruby 1.8.7) app to Rails 3.0.9 (Ruby 1.9.3) — yes it's a stepping stone to get it to Rails 4 and Ruby 2.2 — and I've hit the following problem.
The original app makes extensive use of an old Active Form gem which we've hacked slightly to support Ruby 1.9.
It mostly works, but there appears to be some issue with how it interacts with the ActionView::Helpers::AssetTagHelper that's part of ActionPack 3.0.9.
In my specific case I have an ActiveForm::DateCalendarSection (built dynamically) which subclasses ActiveForm::Element::Section, which, according to self.class.ancestors, is a subclass of ActionView::Helpers::AssetTagHelper. Looking at the ActiveForm source however there is no mention of AssetTagHelper or asset_tag_helper so how they are actually connected remains a mystery to me.
Problem
Calls to the method image_path result in an error
undefined local variable or method 'config'
The call to image_path is simply a wrapper around a call to compute_public_path in ActionView::Helpers::AssetTagHelper
# File actionpack/lib/action_view/helpers/asset_tag_helper.rb, line 741
def compute_public_path(source, dir, ext = nil, include_host = true)
return source if is_uri?(source)
source += ".#{ext}" if rewrite_extension?(source, dir, ext)
source = "/#{dir}/#{source}" unless source[0] == //
source = rewrite_asset_path(source, config.asset_path)
has_request = controller.respond_to?(:request)
if has_request && include_host && source !~ %{^#{controller.config.relative_url_root}/}
source = "#{controller.config.relative_url_root}#{source}"
end
source = rewrite_host_and_protocol(source, has_request) if include_host
source
end
Diving into that with binding.pry it's evident that config is indeed not defined. Likewise controller is also not defined.
Question
What would have changed between Rails 2 and Rails 3, such that methods from ActionView::Helpers::AssetTagHelper can no longer access Rails' config or the current controller?
It could be you don't have enough of the Rails support loaded. Maybe pull in everything inside your gem with
require "active_support/all"
Alternatively, have a look at how modern Rails hooks things up via a proxy to the config:
https://github.com/rails/rails/pull/12622/files
I am using Cucumber with Selenium webdriver, Ruby 2.0.
When I use simple def's everything is ok, but when I tried to create module
module ELEM_TEXT_CONVERTING
def self.convert(element1, element2)
element1_blink = human2selector(element1)
element2_blink = human2selector(element2)
blink_bckgr(element1_blink, element2_blink)
...
end
where blink_bckgr launches the following script
page.execute_script <<-EOS
var color_orig1 = document.querySelector('#{elem1}').style.backgroundColor;
var color_orig2 = document.querySelector('#{elem2}').style.backgroundColor;
window.color_orig1 = color_orig1;
window.color_orig2 = color_orig2;
document.querySelector('#{elem1}').style.backgroundColor = "#FF0000";
document.querySelector('#{elem2}').style.backgroundColor = "#FF0000";
EOS
Finally I get an error:
undefined local variable or method `page' for ELEM_TEXT_CONVERTING:Module
./features/step_definitions/<...>.rb:22:in `blink_bckgr'
What is the trick here? Do I need to put some 'require's of standard libs inside a new module or what? Thanks beforehand.
Require the .rb that you are initialising capybara on, in rspec you usually place it in spec_helper etc...
Then once you have that required you don't even need page... you should be able to call execute_script directly...
Something you can do to make sure the problem is the lack of visibility of capybara's instance is by calling something like the below:
Capybara.page.execute_script
if that works then the suggestion above solves your issue.
I am using page object gem with selenium web-driver. I am trying to automate gmail sign in page. So to enter mail_id and password I am using populate_page_with method.
I am storing my login credentials in a variable "data"
data = { :mail_id => 'abc#abc.com', :mail_password=> '12345' }
And calling populate_page_with method like below
populate_page_with data
When I am trying run the script it gives an error Undefined method send_keys.
But the implementation working fine when I am trying to automate yahoo mail sign in page.
My page object class is
class GmailSignInPage
include PageObject
button :gsubmit, :id => 'signIn'
text_field :mail_id, :id => 'Email'
text_field :mail_password, :id => 'Passwd'
def log_in_to_gmail(data = {})
self.mail_password_element.when_visible
populate_page_with data
self.gsubmit
end
end
My step-definition is
Given /^I navigate to gmail page$/ do
data = { :mail_id => 'abc#abc.com', :mail_password=> '12345' }
on(GmailSignInPage).log_in_to_gmail data
end
In supports/env.rb, I have added PageFactory class also
World(PageObject::PageFactory)
If I modify my log_in_to_gmail method like below then also I am getting same exception
undefined method 'send_keys' for #<NoMethodError: undefined method 'current' for Time:Class> (NoMethodError)
def log_in_to_gmail(data = {})
self.mail_password = data['mail_id']
self.mail_password = data['mail_password']
self.gsubmit
end
But if I use send_keys method its working fine except warning message
def log_in_to_gmail(data = {})
mail_id_element.send_keys data['mail_id']
mail_password_element.send_keys data['mail_password']
self.gsubmit
end
And the warning message is
*** DEPRECATION WARNING
*** You are calling a method named bridge at C:/jruby-1.7.6/lib/ruby/gems/shared/gems/page-object-0.9.2/lib/page-object/elements/element.rb:27:in 'wait_for_document_ready'.
*** This method does not exist in page-object so it is being passed to the driver.
*** This feature will be removed in the near future.
*** Please change your code to call the correct page-object method.
*** If you are using functionality that does not exist in page-object please request it be added.
So I think, this is not the issue with populate_page_with method because even assignment operator = gives same exception. This may be due to page_object gem unable handle gmail sign-in page.
Ok, lets try this again.
I have created a test and copy pasted all of the sample code you have above (Using the populate_page_with() method). I did make one key change to my code compared to yours:
Yours
on(GmailSignInPage).log_in_to_gmail data
Mine
GmailSignInPage.new(#browser).log_in_to_gmail data
I don't know what gem the on() method from your code is from. I could guess, but I want to make this answer more fact based that my previous one. :)
Once I did this, I was able to successfully sign in to Google. So there is nothing unusual about the Google sign in page or any limitation I can come across in the page-object gem or anything wrong with your approach.
So the only things that are different between us is the one line of code I put above, our dev environments, other dependent gems each of us are using.
Try replacing the above line and see if that works.
If not, I would recommend seeing if you have a gem conflict of some sort. Reason I suspect this is due to the strange exception methods you are getting:
undefined method 'send_keys' for # (NoMethodError)
The send_keys part I get, but 'current'? No 'current' method is being called and the Time class is no where to be seen in this example. That's one issue to look at isolating and seeing if you can clean up.
I have an application (rails 3.2) with haml (3.1.4) emails. In the email template I wanted to use link_to, but apparently none of those 4 is working in production:
#{link_to(my_models_url)}
= link_to(my_models_url)
/ #url set to my_models_url
#{link_to(#url)}
= link_to(#url)
On development mode everything works fine, but in production, I keep getting the following error:
No route matches {}
actionpack (3.2.0) lib/action_dispatch/routing/route_set.rb:503:in `raise_routing_error'
It works when I use helper methods before:
/ #my_link set to link_to(my_models_url)
#{#my_link}
= #my_link
But this is not convenient, if there are more links in the email and in general I don't see why any of the first 4 options should not be ok. I have no idea where is this problem comming from. I would appreciate any help on this...
SOLUTION:
Thanks to iWasRobbed I found where my problems were:
all {resource}_path and {resource}_url have to be set in mailers as #variables, they are not available in mailer views
apparently link_to() method in mailer is not the same as in rails views... it always needs 2 arguments, so instead of link_to(#link) available in views, one needs to do link_to(#link,#link). Pffff...
You need to declare the URL within the mailer.rb file. This isn't a HAML issue so much as it's just the way ActionMailer was designed.
def some_mailer_here
#url = my_models_url
end
Then in your mailer view you can do:
= link_to("My models URL", #url)
http://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views
I want to define a function available_translations which lists the translations I have made for my application into the I18n module.
I tried putting the following into the file lib/i18n.rb, but it doesn't work when I try to use it from the rails console:
module I18n
# Return the translations available for this application.
def self.available_translations
languages = []
Dir.glob(Rails.root.to_s + '/config/locales/*.yml') do |filename|
if md = filename.match #^.+/(\w+).yml$#
languages << md[1]
end
end
languages
end
end
Console:
ruby-1.9.2-p290 :003 > require Rails.root.to_s + '/lib/i18n.rb'
=> false
ruby-1.9.2-p290 :004 > I18n.available_translations
NoMethodError: undefined method `available_translations' for I18n:Module
...
Besides solving my concrete problem, I would be very pleased to learn how this whole module thing in Ruby on Rails works because it still confuses me, so I would appreciate links to the docs or source code very much.
Either of these will solve your problem:
move the code to config/initializers/i18n.rb, or
require your file from config/application.rb, or
name your class otherwise (to trigger autoload)
The code in lib/i18n.rb wil not be loaded by autoload since I18n name will be already loaded, so either you load it yourself or change the class name (and file name) so the new name will trigger autoload behavior.
BTW, the I18n.available_locales() method is presented in rails.