I have resource bio and in views and link for add new bio is:
= link_to "Add new bio", [:new, :admin, :bio]
If I put resource :bio to scope like this:
namespace :admin do
scope "/:bio_type", :defaults => {:bio_type => "company"} do
resources :bios
end
end
This doesn't work
= link_to "Add new bio", [:new, :admin, :bio, { bio_type: params[:bio_type] }]
My question is how can I add scoped param to url_for helper? And can rails do this by default?
p.s. new_admin_bio_path({bio_type: params[:bio_type]}) works fine, but it's just curious
I believe you cannot make this with array params to link_to. You have to use polymorphic_path or new_admin_bio_path({bio_type: params[:bio_type]})
The reason is that link_to calls url_for with [:new, :admin, :bio, { bio_type: params[:bio_type] }], which calls polymorphic_path with these params.
Check the source code for url_for and for polymorphic_url.
Notice, that polymorphic_url takes 2 params - record_or_hash_or_array and options, but url_for calls it with one parameter only.
def url_for(options = {})
options ||= {}
case options
when String
options
when Hash
options = options.symbolize_keys.reverse_merge!(:only_path => options[:host].nil?)
super
when :back
controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
else
polymorphic_path(options)
end
end
def polymorphic_path(record_or_hash_or_array, options = {})
polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path))
end
def polymorphic_url(record_or_hash_or_array, options = {})
if record_or_hash_or_array.kind_of?(Array)
record_or_hash_or_array = record_or_hash_or_array.compact
if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy)
proxy = record_or_hash_or_array.shift
end
record_or_hash_or_array = record_or_hash_or_array[0] if record_or_hash_or_array.size == 1
end
record = extract_record(record_or_hash_or_array)
record = convert_to_model(record)
args = Array === record_or_hash_or_array ?
record_or_hash_or_array.dup :
[ record_or_hash_or_array ]
inflection = if options[:action] && options[:action].to_s == "new"
args.pop
:singular
elsif (record.respond_to?(:persisted?) && !record.persisted?)
args.pop
:plural
elsif record.is_a?(Class)
args.pop
:plural
else
:singular
end
args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)}
named_route = build_named_route_call(record_or_hash_or_array, inflection, options)
url_options = options.except(:action, :routing_type)
unless url_options.empty?
args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options
end
args.collect! { |a| convert_to_model(a) }
(proxy || self).send(named_route, *args)
end
So, correct call with the scope option should sound like
polymorphic_path([:new, :admin, :bio], bio_type: params[:bio_type])
Related
I get this error and for the life of me I can't figure out why. Help would be appreciated. :
error 3: error displayed after changes
error 4: after User.all.each do |user|
Error: Undefined method 'each' for nil: nilClass
my ruby/haml code is as follows
viewer code:
-# This file is app/views/projects/index.html.haml
%h1 All Project Tasks
= form_tag projects_path, :method => :get do
Include:
- #all_users.each do |user|
= user
= check_box_tag "users[#{user}]", 1, ("checked" if #filtered_users.find_index(user))
= submit_tag 'Refresh', :id => "users_submit"
%table#projects
%thead
%tr
%th{:class => ("hilite" if params[:sort] == "title")}= link_to "Title", {:controller => "projects", :sort => "title", :filter => #filtered_users.to_s}, :id => "title_header"
%th Task Details
%th Assigned Usertimot
%th{:class => ("hilite" if params[:sort] == "due_date")}= link_to "Due Date", {:controller => "projects", :sort => "due_date", :filter => #filtered_users.to_s}, :id => "due_date_header"
%tbody
- #projects.each do |project|
%tr
%td= project.title
%td= link_to "More about #{project.title}", project_path(project)
%td= project.user
%td= project.due_date.to_formatted_s(:long)
= link_to 'Add new project task', new_project_path
controller code:
class ProjectsController < ApplicationController
def show
id = params[:id] # retrieve project task ID from URI route
#project = Project.find(id) # look up project task by unique ID
# will render app/views/projects/show.<extension> by default
def index
#projects_users = Project.all_users
# remembered settings
if (params[:filter] == nil and params[:users] == nil and params[:sort] == nil and
(session[:filter] != nil or session[:users] != nil or session[:sort] != nil))
if (params[:filter] == nil and session[:filter] != nil)
params[:filter] = session[:filter]
end
if (params[:sort] == nil and session[:sort] != nil)
params[:sort] = session[:sort]
end
redirect_to projects_path(:filter => params[:filter], :sort => params[:sort], :users => params[:users])
else
if (params[:filter] != nil and params[:filter] != "[]")
#filtered_users = params[:filter].scan(/[\w-]+/)
session[:filter] = params[:filter]
else
#filtered_users = params[:users] ? params[:users].keys : []
session[:filter] = params[:users] ? params[:users].keys.to_s : nil
end
end
session[:sort] = params[:sort]
session[:users] = params[:users]
if (params[:sort] == "title")
if ( params[:users]or params[:filter] )
#projects = Project.find(:all, :order => "title")
end
end
if (params[:sort] == "due_date")
if ( params[:users]or params[:filter] )
#projects = Project.find(:all, :order => "due_date")
end
if (params[:sort] == nill)
if(params[:users] or params[:filter])
#projects = Project.all
end
end
end
end
def new
# default: render 'new' template
end
def create
#project = Project.create!(project_params)
flash[:notice] = "#{#project.title} was successfully created."
redirect_to projects_path
end
def edit
#project = Project.find params[:id]
end
def update
#project = Project.find params[:id]
#project.update_attributes!(project_params)
flash[:notice] = "#{#project.title} was successfully updated."
redirect_to project_path(#project)
end
def destroy
#project = Project.find(params[:id])
#project.destroy
flash[:notice] = "Project '#{#project.title}' deleted."
redirect_to projects_path
end
private
def project_params
params.require(:project).permit(:title, :description, :extended_description, :user, :due_date)
end
end
end
i understand that the spacing for haml may be a little off, just the nature of trying to format the code block thanks in advance!
viewer code:
class Project < ActiveRecord::Base
def self.all_users
allUsers = []
Project.all.each do |project|
if (allUsers.find_index(project.user) == nil)
allUsers.push(project.user)
end
end
return allUsers
end
end
You are probably getting the error on this line in your view:
#all_users.each do |user|
The reason for the error as I see it is that you don't have #all_users instantiated anywhere in your controller's index action method.
First switch #all_users to #projects_users. Also it appears that your all_users method in project.rb is overly complex and is returning nil. Try modifying project.rb to the following:
class Project < ActiveRecord::Base
def self.all_users
all.includes(:user).map(&:user).uniq
end
end
Undefined method 'each' for nil: nilClass
This error basically means you don't have any data in your variable.
In other languages, it would mean you've not delcared the variable. Because Ruby is object orientated, it will populate the variable with the nilClass class.
Many new Ruby devs are thrown by the "undefined method" exception message; it's the nilClass you have to look out for.
--
To explain the error properly, because Ruby is object orientated, every variable is actually a data object, represented by a class. In Rails, you can define these classes as models (User.find etc).
Unlike other languages, Ruby treats these objects as is -- it uses methods on them. Other languages fit data into functions, E.G PHP's each function:
#PHP
<$ each($people) $>
#Ruby
<% #people.each do |person| %>
Thus, the "no method" error basically means that Ruby cannot find the method you're calling on the nilClass. It throws developers because they think that "I have the x method on the User class", not realizing that the variable has been populated by the nilClass instead.
The short of it is that you have to either make your calls conditional, or populate the variable properly.
The error appears to be here:
#app/views/project/index.html.haml
#all_users.each do |user|
#app/controllers/projects_controller.rb
class ProjectsController < ApplicationController
def index
#projects_users = Project.all_users
end
end
You're not assigning #all_users at all
You're using an inefficient way to get "all users"
Here's what I'd do:
#app/controllers/projects_controller.rb
class ProjectsController < ApplicationController
def index
#users = Project.all_users
end
end
#app/models/project.rb
class Project < ActiveRecord::Base
scope :all_users, -> { joins(:users) } #-> this needs to be tested
end
#app/views/projects/index.haml
- #users.each do |user|
= user.name
I am pretty inexperienced with pure SQL, you'll be best referring to the joins documentation for a clearer perspective.
I'm learning Ruby on Rails and got curious how the params method works. I understand what it does, but how?
Is there a built-in method that takes a hash string like so
"cat[name]"
and translates it to
{ :cat => { :name => <assigned_value> } }
?
I have attempted to write the params method myself but am not sure how to write this functionality in ruby.
The GET parameters are set from ActionDispatch::Request#GET, which extends Rack::Request#GET, which uses Rack::QueryParser#parse_nested_query.
The POST parameters are set from ActionDispatch::Request#POST, which extends Rack::Request#POST, which uses Rack::Multipart#parse_multipart. That splays through several more files in lib/rack/multipart.
Here is a reproduction of the functionality of the method (note: this is NOT how the method works). Helper methods of interest: #array_to_hash and #handle_nested_hash_array
require 'uri'
class Params
def initialize(req, route_params = {})
#params = {}
route_params.keys.each do |key|
handle_nested_hash_array([{key => route_params[key]}])
end
parse_www_encoded_form(req.query_string) if req.query_string
parse_www_encoded_form(req.body) if req.body
end
def [](key)
#params[key.to_sym] || #params[key.to_s]
end
def to_s
#params.to_s
end
class AttributeNotFoundError < ArgumentError; end;
private
def parse_www_encoded_form(www_encoded_form)
params_array = URI::decode_www_form(www_encoded_form).map do |k, v|
[parse_key(k), v]
end
params_array.map! do |sub_array|
array_to_hash(sub_array.flatten)
end
handle_nested_hash_array(params_array)
end
def handle_nested_hash_array(params_array)
params_array.each do |working_hash|
params = #params
while true
if params.keys.include?(working_hash.keys[0])
params = params[working_hash.keys[0]]
working_hash = working_hash[working_hash.keys[0]]
else
break
end
break if !working_hash.values[0].is_a?(Hash)
break if !params.values[0].is_a?(Hash)
end
params.merge!(working_hash)
end
end
def array_to_hash(params_array)
return params_array.join if params_array.length == 1
hash = {}
hash[params_array[0]] = array_to_hash(params_array.drop(1))
hash
end
def parse_key(key)
key.split(/\]\[|\[|\]/)
end
end
I'm making a script in rails 4.1 that is reading all the ActiveAdmin resource classes along with their member_action methods, but how can I get the additional defined methods inside the class ?
The goal of that script is to get all the resources with the methods defined in the class in order to set the admin user permissions in the database
def get_permissions
skip_resources = [ 'dashboard.rb' ];
default_actions = [ 'create', 'read', 'update', 'delete' ];
resources = Dir.new("#{Rails.root}/app/admin").entries
all_resources = {}
resources.each do |resource|
if resource =~ /.rb/ && (not skip_resources.include? resource)
resource_class = resource.gsub(".rb","")
# resource_obj = resource_class.camelize.constantize.new
all_resources[resource_class] = default_actions #TODO: merge with the defined in class methods
end
end
all_resources
end
This rake task outputs all defined actions for both resource and pages, but it does require loading Rails and the ActiveAdmin configuration.
Also available as a gist: active_admin_actions.rake
task :active_admin_actions => :environment do
skip_resources = [ 'Dashboard' ]
namespace = ActiveAdmin.application.namespace(:admin)
pages = namespace.resources.select { |r| r.is_a? ActiveAdmin::Page }
resources = namespace.resources.select { |r| r.respond_to? :resource_class }
resource_actions =
resources.each_with_object({}) do |resource, actions|
resource_name = resource.resource_class.name
if !skip_resources.include? resource_name
actions[resource_name] = resource.defined_actions
actions[resource_name].concat resource.member_actions.map { |action| action.name }
actions[resource_name].concat resource.collection_actions.map { |action| action.name }
end
end
puts resource_actions.inspect
page_actions =
pages.each_with_object({}) do |page, actions|
page_name = page.name
if !skip_resources.include? page_name
actions[page_name] = page.page_actions + [:index]
end
end
puts page_actions.inspect
end
I need a helper that generates a link wrapped in a <li> including an active class.
Without supporting blocks this is easy:
def nav_item(*args, &block)
url = args[1]
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
link_to(*args)
end
end
But like link_to I want my helper to support blocks for defining content. With link_to I can do:
So how do I support the same in my helper?
All I need to do is pass the block through to link_to. My current attempt
def nav_item(*args, &block)
url = if block_given?
args.first
else
args[1]
end
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
if block_given?
# What goes here?
else
link_to(*args)
end
end
end
You can just pass the block to link_to as the last arg. Like this:
def nav_item(*args, &block)
url = if block_given?
args.first
else
args[1]
end
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
if block
link_to(*args, &block)
else
link_to(*args)
end
end
end
I need to convert a rails 2.3 site so that all external URLs open in a new window. I could go though every call to link_to and add :target => '_blank', but I'd like to do it in one step for all links, present and future. Is there a way I can monkey patch link_to to get the desired behaviour?
You should not have to change your server-side code for this view problem.
You should use Unobscursive javascript.
This example will only make external links showing up in a new window :
// jQuery
//
$(document).ready(function() {
$("a").click(function() {
link_host = this.href.split("/")[2];
document_host = document.location.href.split("/")[2];
if (link_host != document_host) {
window.open(this.href);
return false;
}
});
});
In the end I went with this, in an initialiser:
module ExternalLinksInNewTabs
def new_tab_link_to *args, &block
if block_given?
options = args.first || {}
html_options = args[1] || {}
if options.is_a? String
if ExternalLinksInNewTabs.is_external_link? #controller.request.host, options
html_options[:target] = '_BLANK'
end
end
same_tab_link_to options, html_options, &block
else
name = args.first
options = args[1] || {}
html_options = args[2] || {}
if options.is_a? String
if ExternalLinksInNewTabs.is_external_link? #controller.request.host, options
html_options[:target] = '_BLANK'
end
end
same_tab_link_to name, options, html_options
end
end
def self.is_external_link? host, url
host.sub! /^www\./, ''
url =~ /^http/i && url !~ /^http:\/\/(www\.)?#{host}/i
end
end
module ActionView
module Helpers
module UrlHelper
include ExternalLinksInNewTabs
alias_method :same_tab_link_to, :link_to
alias_method :link_to, :new_tab_link_to
end
end
end
You just add an helper to add this options in your link_to
If you want add it on each link_to to can add on ApplicationHelper
def link_to(*args, &block)
if block_given?
args = [(args.first || {}), (args.second || {}).merge(:target => '_blank')]
else
args = [(args.first || {}), (args.second || {}), (args.third || {}).merge(:target => '_blank')]
end
super(args, block)
end
Or you can create your own link_to helper
def link_to_blank(*args, &block)
if block_given?
args = [(args.first || {}), (args.second || {}).merge(:target => '_blank')]
else
args = [(args.first || {}), (args.second || {}), (args.third || {}).merge(:target => '_blank')]
end
link_to(args, block)
end
In rails 3.2+, it has been added as an option, just add
= link_to 'facebook', 'http://www.facebook.com/fb-page', target: '_blank'
and it'll open the link in a new tab.