I don`t understand this from rails 4:
<td colspan="12" id="compatibility_activities-add" >
<%= job.link_to_add t('shared.add'), :activities,:class => "right small button radius success", :data => { :target => "#activities#{job.object.person_type}_#{job.index}" } %>
</td>
where does the "link_to_add" comes from? there is no references inside the project and cant find anything on the net, there is another "link_to_remove"
this is on a list that creates new rows or deletes them dynamicaly
My second question is, does rails 4 have the way, like laters versions to do rails routes | grep some_route ??
The helper methods you mentioned seem to be provided by the gem Nested Form or some fork of it.
Check your Gemfile for the specific gem name and you can get the official documentation on GitHub or on its website.
In rails 4, you could try using rake routes or bundle exec rake routes to get the list of the routes.
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.
I am reading code(written by someone else) that uses the ransack gem to build a filter. In the config file there is a route that is as follows:
get '/app_hosts/ransack_filter_builder' => 'app_hosts#ransack_filter_builder', :format => :js
I can't find a method called ransack_filter_builder anywhere. Application_helper has one called filterBuilder and application_controller has a filter_builder_controller. so, what is it(#ransack_filter_builder) referring to? Is it something created by rails?
Is there a gem in the Gemfile called ransack? If so, then this method comes from that gem. Here is the gem documentation.
I use the meta_tags gems to handle all the SEO meta tags.
However, when I use in my application.html.haml view, it's showing on the page itself so that the user sees it as well.
= set_meta_tags title: "app title, keywords...."
Any idea why that happens?
This is a behaviour of HAML rather than the meta_tags gem.
-# In a HAML file, ...
= '= will evaluate the ruby code and put the result on the page'
- '- will evaluate the ruby code but not put the result on the page'
Reference: http://haml.info/docs/yardoc/file.REFERENCE.html#ruby_evaluation
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I'd like to implement a captcha in a Rails project for a form submission, but I'm not sure what to go with. I'm inclining towards simplicity of implemention, and reliability when in use, over it being too sophisticated, as my application doesn't require too high a level of security.
Anyone have any recommendations?
The easiest way to add a CAPTCHA to your Rails application is using Ambethia reCAPTCHA:
1. Installation:
config.gem "ambethia-recaptcha", :lib => "recaptcha/rails",
:source => "http://gems.github.com"
You can install it as a plugin, too, if you like.
2. Get a reCAPTCHA account:
You have to create a reCAPTCHA key. You can do it on the reCAPTCHA site.
3. Usage:
Use recaptcha_tags to output the necessary HTML code and then verify the input with verify_recaptcha.
4. Further reading:
Ambethia reCAPTCHA
reCAPTCHA documentation
Installation
Add the following to your GEMFILE
gem "galetahub-simple_captcha", :require => "simple_captcha"
or
gem 'galetahub-simple_captcha', :require => 'simple_captcha',
:git => 'git://github.com/galetahub/simple-captcha.git'
Then run bundle install if you're using Bundler (or use the package manager for your Rails configuration)
Setup
After installation, follow these simple steps to setup the plugin. The setup will depend on the version of rails your application is using.
rails generate simple_captcha
rake db:migrate
Usage
Controller Based
Add the following line in the file “app/controllers/application.rb”
ApplicationController < ActionController::Base
include SimpleCaptcha::ControllerHelpers
end
In the view file within the form tags add this code
<%= show_simple_captcha %>
and in the controller’s action authenticate it as
if simple_captcha_valid?
do this
else
do that
end
Model Based
In the view file within the form tags add this code
<%= show_simple_captcha(:object=>"user") %>
and in the model class add this code
class User < ActiveRecord::Base
apply_simple_captcha
end
FormBuilder helper
<%= form_for #user do |form| -%>
...
<%= form.simple_captcha :label => "Enter numbers.." %>
...
<% end -%>
Validating with captcha
NOTE: #user.valid? will still work as it should, it will not validate the captcha code.
#user.valid_with_captcha?
Saving with captcha
NOTE: #user.save will still work as it should, it will not validate the captcha code.
#user.save_with_captcha
Formtastic integration
SimpleCaptcha detects if your use Formtastic and appends
“SimpleCaptcha::CustomFormBuilder”.
<%= form.input :captcha, :as => :simple_captcha %>
Options & Examples
View Options
*label* - provides the custom text b/w the image and the text field, the default is “type the code from the image”
*object* - the name of the object of the model class, to implement the model based captcha.
*code_type* - return numeric only if set to ‘numeric’
Global options
image_style - provides the specific image style for the captcha image.
There are eight different styles available with the plugin as…
simply_blue
simply_red
simply_green
charcoal_grey
embosed_silver
all_black
distorted_black
almost_invisible
Default style is ‘simply_blue’. You can also specify ‘random’ to select the random image style.
distortion - handles the complexity of the image. The :distortion can be set to ‘low’, ‘medium’ or ‘high’. Default is ‘low’.
*Create “rails_root/config/initializers/simple_captcha.rb”*
SimpleCaptcha.setup do |sc|
# default: 100x28
sc.image_size = '120x40'
# default: 5
sc.length = 6
# default: simply_blue
# possible values:
# 'embosed_silver',
# 'simply_red',
# 'simply_green',
# 'simply_blue',
# 'distorted_black',
# 'all_black',
# 'charcoal_grey',
# 'almost_invisible'
# 'random'
sc.image_style = 'simply_green'
# default: low
# possible values: 'low', 'medium', 'high', 'random'
sc.distortion = 'medium'
end
You can add your own style:
SimpleCaptcha.setup do |sc|
sc.image_style = 'mycaptha'
sc.add_image_style('mycaptha', [
"-background '#F4F7F8'",
"-fill '#86818B'",
"-border 1",
"-bordercolor '#E0E2E3'"])
end
You can provide the path where image_magick is installed as well:
SimpleCaptcha.setup do |sc|
sc.image_magick_path = '/usr/bin' # you can check this from console by running: which convert
end
You can provide the path where should be stored tmp files. It’s usefull when you dont have acces to /tmp (default directory)
SimpleCaptcha.setup do |sc|
sc.tmp_path = '/tmp' # or somewhere in project eg. Rails.root.join('tmp/simple_captcha').to_s, make shure directory exists
end
How to change the CSS for SimpleCaptcha DOM elements?
You can change the CSS of the SimpleCaptcha DOM elements as per your need in this file.
*/app/views/simple_captcha/_simple_captcha.erb*
View’s Examples
Controller Based Example
<%= show_simple_captcha %>
<%= show_simple_captcha(:label => "human authentication") %>
Model Based Example
<%= show_simple_captcha(:object => 'user', :label => "human authentication") %>
Model Options
message - provides the custom message on failure of captcha authentication the default is “Secret Code did not match with the Image”
add_to_base - if set to true, appends the error message to the base.
Model’s Example
class User < ActiveRecord::Base
apply_simple_captcha
end
class User < ActiveRecord::Base
apply_simple_captcha :message => "The secret Image and code were different", :add_to_base => true
end
Well, ReCaptcha will do the job and there are many tutorials on it online.
However, you have to write a correct "def create" (create method) in your controller that will pass whatever is in your form plus validate Recaptcha at the same time. Then it will work nicely.
There was one little problem with it. After I inserted ReCaptcha into my form, the form validation stopped working. However, it can be fixed with an easy code inserted into the model file:
after_validation :on => :create
(:create = is the "def create" method in your controller). It will force the form to validate the form first and then validate Recaptcha.
I've used Recaptcha in one of my PHP project. http://recaptcha.net/
According to the site, it also has plugins for Ruby (http://recaptcha.net/resources.html). Although first ruby link didn't work, next link still works. http://svn.ambethia.com/pub/rails/plugins/recaptcha/
Check it.
I've used ambethia recapchat for rails application. it most easy than other
reCAPTCHA for rails is great, in terms of functionality. However, if you require XHTML validation, RUN AWAY! This plugin does not (and probably never will) validate. I find it embarrassing that only one page on my entire site does not validate - it is the page with reCAPTCHA. If there was ANY other choice, I would take it.
if you're after a CAPTCHA that validates, and is (almost) as easy to use as reCAPTCHA, please give my SlideCAPTCHA a try. (Wrote it a few days ago, needs some tests in real-life usage.) I based its deployment process on the reCAPTCHA plugin, but you can style it with CSS.
It does require Ruby/GD, however, so if you don't have GD already, I can't promise that GD is easy to install and use!