ReCAPTCHA throwing error when used inside jQuery Stepy Wizard - ruby

I have used multi step sign up form using jQuery.stepy.js Also I have added google ReCAPTCHA using 'gem "recaptcha"'. When I am placing
<%= recaptcha_tags %>
inside wizard form it is throwing JavaScript error like this:
Uncaught Error: ReCAPTCHA placeholder element must be empty
I have searched for the reason behind this error and found that this error occurs if the google recaptcha library is loading twice.
Is this happening because of jQuery.stepy.js ? How can I resolve this error?

This error occurs when recaptcha/api.js file gets loaded multiple times.
In your case you have used <%= recaptcha_tag %>, this tag inject api.js by default everytime it renders. Just check you haven't added the library file explicitly or using multiple recaptcha tags (each one will inject library script).
You can set :script param to false, by passing argument to method:
<%= recaptcha_tag :script => false %>

Related

Carrierwave for image upload with ruby on rails

I am trying to upload image using carrierwave. Everything seems to be correct, but I am repeatedly getting an error stating:
Can't resolve image into URL: undefined method `to_model' for #<PortfolioUploader:0x007f35a5cad3c0>
Did you mean? to_xml
CarrierWave with Rails 5.2 ActiveStorage may have some issues.
You could try to change your classic image_tag.
Assuming you're using something like this :
<%= image_tag(#user.image) %>
Change it to something like this, using .url
<%= image_tag(#user.image.url) %>

How make rails forms submit if using rails-api gem. Undefined method `protect_against_forgery?

I have a rails application which is api only i am using gem rails-api. Now I am trying to create admin panel on it because we realised later that we do need an admin panel. But i think rails-api doesnt have good support for views.
When I try to submit forms it says undefined method protect_against_forgery? maybe because it doesnt supports <%= csrf_meta_tags %>
I tried to define manually this method in my controller but no luck.
def protect_against_forgery?
true
end
How can i submit the forms without getting this error.
in your api controller put following line.
skip_before_filter :verify_authenticity_token
or try with
def protect_against_forgery?
false
end
this may be help you.
According to rails-api, try to set config.api_only = false to config/application.rb file
Or, since protect_from_forgery is a method that belongs to ActionController, try to add require "action_controller/railtie" to application.rb

Why is Sinatra web server giving a 500 error from this POST method?

I'm building a Ruby web app with Sinatra, and am using a form to pass parameters to a POST page, which should have rendered dynamic content based on those parameters.
However, when it came time to test the POST method and submitting the form, the following was displayed in the browser:
Internal Server Error
bad content body
When this happens, the error log shows:
Error: EOFError: bad content body
followed by a stacktrace of files I'm not working with and then:
"POST /list/AUser'sName" HTTP/1.1 500 294
Code for the form:
Note - This page is just used for redirecting to a different page, determining the correct form action= attribute.
<form action="/list/<%=name%>" method='post' enctype="multipart/form-data">
<input type='Submit'/>
</form>
POST method in the server.rb file:
post '/list/:name' do
"Hello World"
end
After removing the enctype= attribute, the form is now working as intended.
From what I've learned, the enctype attribute is only used when uploading a file that needs to be submitted with the form. I had it included here because of past issues on different forms where logic errors occurred when it was not included.
Thanks to all for the comments and suggestions.

Rails app routes not available in engine

I'm having the opposite problem of other related posts here. I'm using a gem that renders a page that uses the application's layout. That layout works fine in the application otherwise. In that layout is a named path, login_logout_path, that causes an undefined local or method error.
Here is a snippet of the view code:
<%= link_to( ApplicationName + ' Home', root_path ) %> |
<%= link_to('Sign Off', login_logout_path ) %> :
<%= get_session_login.iname %>
root_path works fine, logon_logout_path fails.
On complication might be that the layout above and the logon_logout_path is supplied by another gem that contains common code/layouts for the set of web sites.
Short Answer, change
login_logout_path
with:
main_app.login_logout_path
and add main_app to all your routes in the application.html.erb that go to your main app
Long answer:
The problem is that engines can have routes with the same name as a route in your main app and work without crashing, if you want to use a route of your main app inside your engine you have to specify it putting
main_app.
Before the route, and if you want to use a route of your engine in your main app you have to specify it with
engineName.example_path
The root_path works because your engine has defined a root_path, and it's send you to the root path of the engine, not the one in your app, you can fix it with adding main_app before it too.

Rails 3.1 link_to not showing confirmation or destroying properly

I've been plowing through the chapters at railstutorial.org and been using Rails 3.1.3 because I'm crazy and/or wanted a challenge. I managed to figure out most version problems easily but this one stumped me for a while.
In 10.4.2, Michael Hartl uses the following code to delete users:
<%= link_to "delete", user, :method => :delete, :confirm => "You sure?",
:title => "Delete #{user.name}" %>
It doesn't work properly if you test it in the browser (chrome) and instead sends you to that user page.
It is supposed to work if you include this:
<%= javascript_include_tag :defaults %>
but it fails with Rails 3.1 (it should work for Rails 3.0 though, or so I hear).
So for all of you pulling out your hair for using Rails 3.1, here's the solution.
<%= javascript_include_tag "application" %>
Using "application" instead of :defaults solves this problem, delete and confirm should work, now get back to coding!
Special thanks to George Shaw for this answer over on https://stackoverflow.com/a/8350158/1127011 .
And it case you were wondering, title is for mouseover only.
The a HTML tag will always fire the GET method. Rails uses the Javascript driver to replace the HTTP verb. The link_to method will fallback any method (post, put and delete) to get (corresponding to the show action) when :
Javascript has been disabled by the user
for some reason, Rails unobtrusive javascript driver is not handling the link properly
See http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html
I suspect the second reason to be the issue. Make sure the jquery.js, application.js, jquery_ujs.js file are included.
Instead of link_to, you could try to use button_to which creates a form allowing put, post and delete methods without Javascript enabled.

Resources