Carrierwave for image upload with ruby on rails - ruby

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) %>

Related

Ruby on Raills disabling the Assets-Pipeline

I try to update an old rails application.
It was original written with rails 2.3 and works now with rails 5.0.7
It does not use the asset pipeline, as there is not much css and js, and think it would make updating even more complex.
When trying to update to Rails 5.1, it seem now to wanting to use the asset pipeline, and throws errors:
<%= stylesheet_link_tag 'stylesheetfile.css' %>
ActionView::Template::Error (The asset "stylesheetfile.css" is not present in the asset pipeline.
):
I have already tried the following in application.rb
config.assets.enabled = false

ReCAPTCHA throwing error when used inside jQuery Stepy Wizard

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 %>

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

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.

Generate URL for file in /public in Rails 2 ERB view

In my rails (v2.3.8) app I have a static resource file which I've put at /public/myfile.kml No need for any special routes.rb setting right?
It serves up just fine at http://localhost:3000/myfile.kml
When I deploy (to passenger) it appears at http://myserver/myappname/myfile.kml
All is well so far...
I have a view (an erb file) which spews out javascript which needs to reference this file. The output needs to be '/myfile.kml' on localhost, and '/myappname/myfile.kml' in production, or maybe the full URLs as above, or maybe a relative url involving a bit of '../../../' (awkward with RESTful URLs).
Should I be able to do something like <%=url_for 'myfile.kml'%> ?
or '<%=ROOT_URL%>/myfile.kml'
I know there's an insanely easy answer to this question, but honestly I've had no luck finding it. Quite a few people talking about 'root_url' but what is that? A variable I can reference in a view? It's undefined.
I'm not sure about Rails 2.3.8, but in Rails 3 this value defaults to false.
edit config/environments/production.rb and set:
config.serve_static_assets = true
Also, here's a blog post that shows a helper for linking to a static resource (favicon)
http://ilconnettivo.wordpress.com/2008/07/28/favicon-on-rails/
'<%= ENV["RAILS_RELATIVE_URL_ROOT"] %>/myfile.kml'
<%= RAILS_ROOT + "/public/myfile.kml" %>
Inspection of rake routes reveals the helper root_path for use in views. For example <%= root_path + 'myfile.kml' %> By default will map to files under public/ in a rails application.
The latest (>2.3.6) is Rails.root, see:
http://joneslee85.wordpress.com/2010/05/27/the-dilemma-of-rails-root-vs-rails_root-complex/
Why not just replicate your production environment locally? A webserver is not very resource hungry and it can help resolve some ecosystem configuration issues like you're seeing here.

Resources