active_scaffold routing error - ruby-on-rails

If you haven't seen my question yesterday, this is my second rails app. The first went nice and smooth, but this one keeps giving me one random error after another. I installed active_scaffold for this app as well as the last app (the first error, instead of using script/install plugin git://active_scaffold repository, I did script/install plugin http://active_scaffold repository.) I didn't want to spell out basic CRUD on minor models. After the install problems, (before I found the http solution from a windows user when I'm on Linux) I thought I'd try out Hobo. Well, Hobo updated actionmailer, actionpack, activerecord, activeresource, and installed rack. Rails isn't even using the updated versions. But as you can see at the bottom of the trace it's using rack. I have a feeling it has something to do with my futzing around with installing Hobo which I uninstalled. Thanks in advance.
[Edit]
I had asked the question over at the
ActiveScaffold Group
the answer (if you don't want to follow the link) was that this line needed to be added to routes:
map.resources :modelName, :active_scaffold => true
It doesn't entirely answer my question, since the documentation said nothing about changing routes. But, it works.
[/Edit]
ActionController::RoutingError in Departments#index
Showing vendor/plugins/active_scaffold/frontends/default/views/_list_header.html.erb where line #8 raised:
No route matches {:_method=>:get, :action=>"show_search", :controller=>"departments"}
Extracted source (around line #8):
5: <% next if controller.respond_to? link.security_method and !controller.send(link.security_method) -%>
6: <% next if link.action == 'new' && params[:nested].nil? && active_scaffold_config.list.always_show_create %>
7: <% next if link.action == 'show_search' && active_scaffold_config.list.always_show_search %>
8: <%= render_action_link(link, new_params) -%>
9: <% end -%>
10:
11: <%= loading_indicator_tag(:action => :table) %>
Trace of template inclusion: vendor/plugins/active_scaffold/frontends/default/views/list.html.erb
Full Trace It was taking forever to format it. I'm still not fully conversant in SO's formatting (sometimes the server is down. reboots are reinstalls. it's a play server)

Add this to routes:
map.resources :modelName, :active_scaffold => true
And, contrary to my edit, it is in the documentation. It's in the wiki at Github. My second question at SO, and I could have found the answer by RTFM'ing. <sigh>

For Rails 4+ at least, your entry in config/routes.rb should look like:
resources :models do # my model name in plural
as_routes
# for action links of type member, add the following
# see also [note, via the action-link API, you can change the HTTP verb. Adjust this route accordingly.
# https://github.com/activescaffold/active_scaffold/wiki/Adding-custom-actions
# get :my_custom_action, :on => :member
end
Note, getting the right version of the Gem requires the following config in your Gemfile:
gem 'active_scaffold', github: 'activescaffold/active_scaffold', :branch => "3-4-stable"
If you get an error that "as_routes" isn't found; that'd be the problem.
THIS IS RAILS 4 ONLY

For Rails 5.x you can set route as follow:
resources :model, concerns: :active_scaffold

Related

Rails gem Ratyrate showings stars only after refresh and not saving

(Saw many posts with these problems and got it working, so I will upload solution)
I'm using Ratyrate without Devise and the stars only appear after a web refresh and they do not save the previously assigned rate.
Fixed this issue with:
Specifying version in each Rating Migration (currently using ActiveRecord::Migration[5.1])
Changing Rate Controller "create" method, replacing "if user_signed_in?" with "if current_user"
Adding "post '/rate' => 'rater#create', :as => 'rate'" to routes.rb (gem should do this for you)
Adding
<%= javascript_include_tag 'ratyrate.js', "data-turbolinks-track": false %> at the bottom of show.html.erb (the view of the model you want to rate)
Hope this helps !

jquery_datepicker not working with Rails 4

See the following repo for example project: https://github.com/aarona/date_picker
At first I thought the problem was caused by the model being tableless but when associating it with the database through a migration I still get an error:
wrong number of arguments (1 for 0)
on this line:
<%= f.datepicker :start_date, :class => 'short' %>
I get an error about delete method not found if the model is tableless.
Turns out that the gem jquery_datepicker is incompatible with Rails 4. Time to use a different gem/control if I want to upgrade.
A solution is available for Rails 4+. You can find this solution here:
https://stackoverflow.com/a/43289610/43792

link_to_function in rails 3.1 with blocks throws wrong number of arguments error

I recently started working with rails 3. I'm trying to add a multi-modal form into my application. I'm following the steps mentioned at Handle multiple models in one form
When I try to add a link via following helper function, I get
wrong number of arguments (1 for 2)
Code block below.
def add_task_link(name)
link_to_function name do |page|
page.insert_html :bottom, :tasks, :partial => 'task' , :object => Task.new
end
en
Googling for solution didn't take me anywhere.
Thank you.
You should find another tutorial, RJS and prototype are deprecated in rails 3, they've been replaced with jQuery.
If you really insist on going forward, you can bring RJS and prototype back by adding the following to your Gemfile:
gem 'prototype-rails'
This will bring back the correct version of link_to_function.

Recommendations for a captcha on Ruby on Rails [closed]

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!

Has namedspaced routing changed in Rails 2.3?

I have an admin namespace which gives me the usual routes such as admin_projects and admin_project, however they are not behaving in the usual way. This is my first Rails 2.3 project so maybe related I can't find any info via Google however.
map.namespace(:admin) do |admin|
admin.resources :projects
end
The strange thing is for a given URL (eg. /admin/projects/1) I don't have to pass in an object to get URL's it somehow guesses them:
<%= admin_project_path %> # => /admin/projects/1
No worries, not really a problem just not noticed this before.
But if I try and pass an object as is usual:
<%= admin_project_path(#project) %> # => admin_project_url failed to generate from {:controller=>"admin/projects", :action=>"show", :id=>#<Project id: 1, name: "teamc...>
":id" seems to contain the entire object, so I try passing the id directly and it works:
<%= admin_project_path(#project.id) %> # => /admin/projects/1
This would not be so bad but when it comes to forms I usually use [:admin, #object], however:
<%= url_for [:admin, #project.id] %> # => undefined method `admin_fixnum_path'
So I can't pass in an id, as it needs an objects class to work out the correct route to use.
<%= url_for [:admin, #project] %> # => Unfortunately this yields the same error as passing a object to admin_project_path, which is what it is calling.
I can't spot any mistakes and this is pretty standard so I'm not sure what is going wrong...
Interesting. What happens when you define a to_param method on Project? For instance
class Project < ActiveRecord::Base
def to_param
self.id
end
end
This should be the default and this shouldnt be necessary. But what happens if you make it explicit? Try adding the above method then going back to your original approach of only passing around #project
I wish I could help you on this one. We have a large application with several namespaced sections. Our routes are defined in the exact method you have described and we are calling our path helper with objects. No where in the application are we accessing using the id.
Our application started on Rails 2.1 and has transitioned through 2.2 and 2.3 with no significant changes to the routing. Sorry I couldn't be more help.
Peer

Resources