link_to does not work in production (Heroku) - ruby-on-rails

I don't have any problems using link_to locally however, as soon as I deploy to Heroku I get the following error:
users#show (ArgumentError) "arguments passed to url_for can't be handled. Please require routes or provide your own implementation"
app/views/users/show.html.erb:176:in `_app_views_users_show_html_erb__222687663100622833_69928454693640'
I am using ruby '2.2.0' and rails '4.2.0'
Any ideas on how to further debug this or replicate it locally?
Update 1. Here's the actual view code which displays pagination links. The resulting route should be /users/1?page=1 etc.
<div class="row text-center">
<%= will_paginate collection, renderer: BootstrapPagination::Rails %>
</div>

The issue was with url_helpers included in one of my models - everything worked after I removed the following include.
include Rails.application.routes.url_helpers
Using the correct url_for method in a Rails engine spec

I had the same issue with ActiveAdmin in production environment. In my case the issue was with ActionView::Helpers::FormTagHelper included at root level in one of my helpers.
I solved it moving the include statement inside a class defined in the same file.

Related

Rails: How to implement Action Text in Rails 5.2.1?

I'm experiencing difficulties in implementing the Action Text gem to my Rails 5.2.1 application.
I followed the installation guide and the rich text editor wouldn't show up in my _form view.
I then realised that it requires Rails 6 with webpacker and active storage. Changing my gemfile's Rails version and running rails upgrade didn't work, so I added the webpacker and active storage gems and bundled it with my current 5.2.1 version. That didn't work either.
I'd really like to have a Rich Text Editor in my app. It is not a must that it is the Trix editor, but since it will be native as of v6 I thought it was the obvious choice.
References of my source code
GitHub: https://github.com/Curting/mydanceplan
Heroku: https://mydanceplan.herokuapp.com/
The perfect choice ,tested on my new application using rails 5.2.3 .
please not action text require ruby 2.5.3 up.
first : Add webpacker
You can either add Webpacker during setup of a new Rails 5.1+ application using new --webpack option:
Available Rails 5.1+
rails new myapp --webpack
Or add it to your Gemfile:
Gemfile
gem 'webpacker', '~> 4.x'
OR if you prefer to use master
gem 'webpacker', git: 'https://github.com/rails/webpacker.git'
yarn add https://github.com/rails/webpacker.git
Now add Actiontext to your gem file with image magic:
gem'actiontext',github:'kobaltz/actiontext',branch:'archive',require:'action_text'
gem 'image_processing'
Finally, run the following to install Webpacker:
bundle
bundle exec rails webpacker:install
rails action_text:install
rails db:migrate
brew install imagemagick vips
add this to your view/layout/application in the head section
#layouts/application.html.erb
<%= javascript_pack_tag 'application' %>
In your model , it may be an article or what ever in your model
class Article < ApplicationRecord
has_rich_text :content
end
in your controller
#controllers/articles_controller.rb
def article_params
params.require(:article).permit(:name, :content)
end
in your form
#_form.html.erb
<div class="field">
<%= form.label :content %>
<%= form.rich_text_area :content %>
</div>
finaly to display the content in your view;
<h1><%= #article.name %></h1>
<p><%= #article.content %></p>
Optional: To fix "unmet peer dependency" warnings,
yarn upgrade
you can watch the full video here:
https://www.driftingruby.com/episodes/using-action-text-in-a-rails-5-2-application
I have a partial answer for you! I wanted to use Trix, Rails 5.2.2, and Webpacker, but couldn't find a clear answer. The trix gem didn't work since I don't use the asset pipeline at all. (I also use Turbolinks and Stimulus, and so far this is working fine alongside those.)
I strung a solution together using:
trix node package
a custom form tag helper, derived from this Stack post
I can now use a form helper that looks like this (using the Slim templating language):
= f.trix :personal_notes, class: 'some-css-class'
To get there, I did the following:
yarn add trix
In my Javascript pack, import 'trix';
In my CSS pack, #import '~trix/dist/trix';
Then I created the form helper. Here's what that looked like:
In app/helpers/application_helper.rb, I added the new form helper, based on that Stack post I linked above. My file now looks like this:
module ApplicationHelper
# ...
class ActionView::Helpers::FormBuilder
# include ActionView::Helpers::TagHelper # I didn't need this.
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::FormOptionsHelper
# include ActionView::Helpers::CaptureHelper # I didn't need this.
# include ActionView::Helpers::AssetTagHelper # I didn't need this.
# since these tag helpers just return strings, it is easy to make a pretty
# powerful helper. It is probably possible to use existing Rails form
# helpers to aid this as well.
def trix(method, options = {})
value = (#object[method].empty?) ? '' : "value='#{#object[method]}'"
return "<input id='#{field_id(method)}' type='hidden' name='#{field_name(method)}' #{value}><trix-editor input='#{field_id(method)}' class='trix-content #{options[:class]}'></trix-editor>".html_safe
end
def field_name(label,index=nil)
return #object_name + "[#{label}]"
end
def field_id(label,index=nil)
return #object_name + "_#{label}"
end
end
end
Restart Rails. Seems to work for me.

Accessing Views Within Ruby on Rails Application

I want to make a view for my website coded in RoR wherein all the available path are listed as links. Is there any way to access the views for various models from the program?
For example, something like:
<%model.views.each do |v| %>
<%= link_to v %>
<% end %>
You could use the sitemap_generator or dynamic_sitemaps gem to generate Sitemaps for your application.
You can use named routes, which allows you to create a link to a different part of your rails app, based on names you set in routes.rb. You can also include route parameters, too, which makes it easy to link to models.
In your routes.rb
get 'test_route/:id' => 'example#controller', as :test
In controllers/views:
link_to #example.name, test_path(id: #example.id)
Further reading on named routes
Im not sure why you want this :), but this will give you the routes
Rails.application.routes.routes
if you want to get all the paths as an array
Rails.application.routes.routes.collect {|r| r.path.spec.to_s }

Rails path-helpers doesn't work in js.coffee.erb

In my Rails 3.2 app (Ruby 1.9) I get following error when using path helpers in Coffeescript.
undefined local variable or method `new_user_session_path'
In my partial _usermenu.html.haml that works fine:
= link_to t('user.login'), new_user_session_path
In my app/assets/javascripts/metamenu.js.coffee.erb that throws above error:
$.get("<%= new_user_session_path %>")
Isn't it possible to use x_path and x_url helpers in coffeescript erb's?
This is because you are not within the view context inside of your assets. Adding an erb extension to the file doesn't change this, it simply allows you to evaluate embedded ruby.
If this is a one-off scenario, your best bet is to simply use the string itself.
$.get("/sign_in")
If you really wanted to you could create a partial that output a script tag that output your helper methods into js variables and access them that way.
# in your layout
<%= render 'url_helpers' %>
# in app/views/layouts/_url_helpers.html.erb
<script>
window.new_user_session_path = "<%= new_user_session_path %>";
# add more if necessary
</script>
# in your coffeescript
$.get(#new_user_session_path)
Also worth keeping in mind that this will obviously never work for member routes where your passing an instance of a model to the url helper as that is definitely not available to coffeescript. Remember, in production assets are precompiled so you can't use anything dynamic. For that you can only really rely on setting up actions in your controller to respond to JS calls.
Old post, but still accessible from Google.
In rails 4 (and certainly at least 3 too) you can use the route helpers to insert your js files easily:
assets/javascript/my_file.js.coffee.erb
<% self.class.include Rails.application.routes.url_helpers %>
window.index_route = '<%= index_path %>'

Is it possible to require a class in an erb template?

I have an erb template in which I need to use:
CGI.unescapeHTML(someEscapedHTML)
So I need to require 'cgi', however the following fails:
<% require 'cgi' %>
With the error:
can't dup NilClass
I would personally never put a require statement in a view, because 1) it's ugly and 2) what if another view needed that require?
A better place for this is in config/application.rb at the bottom, or in a file in config/initializers.
First of all do not require gems or libraries in ERB please. Then CGI is required by Rails itself already.
If you want to prevent Rails 3 from auto-escaping consider using
<%= data.html_safe %>
instead.

Using reCaptcha with Rails 2.3.12

So I'm trying to get reCaptcha to render on a partial form view that uses HAML. I have tried using the :ruby filter and then adding <%= recaptcha_tags %> but that didn't work, neither has anything else that I've found. Is there a way to implement this?
*Revision
Ahem, more specifically, can anyone tell me what I need to have for the <%= recaptcha_tags %> helper? Every thing I find on this subject just says "Add <%= recaptcha_tags %> wherever you want it to appear!" and absolutely nothing on what the helper should contain.
*Another Revision
I am indeed trying to use Ambethia. I tried using just = recaptcha_tags but that didn't work, I got an error saying it was an undefined variable or method. I installed the Ambethia/reCaptcha as a plugin using script/plugin install git://github.com/ambethia/recaptcha.git and I put config.gem "ambethia-recaptcha", :lib => "recaptcha/rails", :source => "http://gems.github.com" in environment.rb along with my public/private keys.
*Started Over
Okay, got rid of everything I had done initially. Can anyone help me with this? I follow all of the tutorials I can find on it, but none of them explain how to implement/create the helpers for <%= recaptcha_tags %> or <%= verify_recaptcha %>. I'm obviously new to RoR and implementing reCaptcha of any kind, so I'm sorry I'm asking for my hand to be held but I am honestly lost and am not finding any guidance anywhere! Thanks so much anyone and everyone.
did you try simply:
= recaptcha_tags
You don't mention the plugin you're using. I'm assuming this one. If that's the case, the recaptcha_tags helper will return the HTML for the captcha, and you'd insert it into whichever forms you wanted the captcha to appear on.
The <%= %> around recaptcha_helper aren't part of the helper, but rather the way you insert content into erb templates (and other templating languages resembling erb). In Haml you don't need the surrounding tag. It's just =.
I had the same problem and I finally solved it realizing that my form was called asynchronously.
I added:
= recaptcha_tags :ajax => true
and captcha appeared.
Hope this could help the original question.

Resources