helpers

Body class for controller in Rails app

喜你入骨 提交于 2019-12-03 17:31:05
Currently I have this in my layout: <body class="<%= controller.controller_name %>"> I want to add an additional class that will be the same for all actions in any controller where it's set, something like: class SomeController < ApplicationController body_class 'page' ... end class AnotherController < ApplicationController body_class 'page' ... end Which will result in: <body class="some page"> <body class="another page"> What would be the easiest way to achieve this? Can I use controller class variables for this? Stop! Stop! Use this pattern: <body data-controller="#{controller.controller

Finding all by Polymorphic Type in Rails?

倖福魔咒の 提交于 2019-12-03 17:19:44
问题 Is there a way to find all Polymorphic models of a specific polymorphic type in Rails? So if I have Group, Event, and Project all with a declaration like: has_many :assignments, :as => :assignable Can I do something like: Assignable.all ...or BuiltInRailsPolymorphicHelper.all("assignable") That would be nice. Edit: ... such that Assignable.all returns [Event, Group, Product] (array of classes) 回答1: There is no direct method for this. I wrote this monkey patch for ActiveRecord::Base . This

Rails 3 nested resources short name?

风流意气都作罢 提交于 2019-12-03 13:49:32
I'm in the process of upgrading a Rails 2.3 app to Rails 3. In the Rails 2.3 router, it was possible to set a :name_prefix of nil on nested resources to get a shorter name. The actual URL would still be fully qualified, but the code could use a shorter name. E.g.,: map.resources :sites do |site| site.resources :groups, :as => :groups, :controller => :url_groups, :name_prefix => nil, :member => { :clone => :post } do |group| group.resources :tests, :as => :tests, :controller => :test_runs, :name_prefix => nil, :collection => { :latest => :get } end end would allow one to use latest_tests_path .

Rendering templates within helpers in handlebars

≡放荡痞女 提交于 2019-12-03 12:31:07
Hey guys! Because there seem to be no answer on this: Passing variables through handlebars partial yet, I'm currently working on a little workaround to get this work. So, the idea is to register a helper function which renders a specific template with possible values. A bit code makes it better to understand. This is how a I'd invoke my helper: <div> {{myHelper}} </div> This helper is registered with this little code: hbs.registerHelper(name, function (args) { args = args || {}; var template = hbs.compile(fs.readFileSync(__dirname + '/' + file, 'utf8')); return template(args); }); I put this

asp.net mvc. Routing and Url building

无人久伴 提交于 2019-12-03 09:00:24
Does any body knows how to hide some parameters in url? For example you have a url parameter "culture". It can be "en, fr, it". By default your site renders in "en" culture and we don't wont to show default culture in the URL but in cases of other cultures parameter "culture" must appear in URL. http://myrendersite.net/barbikueue http://myrendersite.net/fr/barbikueue This is same pages in different cultures. How to do what basing on default asp.net mvc routing system? Cristi Pufu This will help: ASP.NET mvc, localized routes and the default language for the user asp.net mvc localization Set

Finding all by Polymorphic Type in Rails?

情到浓时终转凉″ 提交于 2019-12-03 07:22:20
Is there a way to find all Polymorphic models of a specific polymorphic type in Rails? So if I have Group, Event, and Project all with a declaration like: has_many :assignments, :as => :assignable Can I do something like: Assignable.all ...or BuiltInRailsPolymorphicHelper.all("assignable") That would be nice. Edit: ... such that Assignable.all returns [Event, Group, Product] (array of classes) There is no direct method for this. I wrote this monkey patch for ActiveRecord::Base . This will work for any class. class ActiveRecord::Base def self.all_polymorphic_types(name) @poly_hash ||= {}.tap do

How to implement a helper in Symfony 1.4?

ⅰ亾dé卋堺 提交于 2019-12-03 05:51:01
问题 I'd like to create my own helper but can't find any help on Google for Symfony 1.4/Doctrine. I guess it has something to do with creating a myClassHelper.class.php in lib/helpers/ or something, but I don't know what to implement, or if specific methods have to be overridden. Any help would be appreciated! 回答1: I don't think that anything changed regarding the helpers in 1.4. From the documentation(although 1.2): Helper functions (regular PHP functions returning HTML code) should be saved in a

How can I modify the input type of the Rails datetime_select helper?

非 Y 不嫁゛ 提交于 2019-12-03 05:09:17
I am using the Rails helper datetime_select in one of my forms. I currently have a requirement to change the dropdowns for day , year , hour , and minute to be textboxes with validation. Is there a way I can specify that I want textboxes for these fields? Or possibly a different helper that will do what I need? here is my usage: datetime_select(:campaign, :start_date, :order => [:day, :month, :year, :hour, :minute]) how about rolling your own simple helper, like def datetime_text_fields(object_name, method) html = text_field_tag("#{object_name}[#{method}(3i)]", Date.today.day.to_s, :length =>

How do I test helpers in Rails?

折月煮酒 提交于 2019-12-02 21:44:13
I'm trying to build some unit tests for testing my Rails helpers, but I can never remember how to access them. Annoying. Suggestions? Eugene Bolshakov In rails 3 you can do this (and in fact it's what the generator creates): require 'test_helper' class YourHelperTest < ActionView::TestCase test "should work" do assert_equal "result", your_helper_method end end And of course the rspec variant by Matt Darby works in rails 3 too You can do the same in RSpec as: require File.dirname(__FILE__) + '/../spec_helper' describe FoosHelper do it "should do something" do helper.some_helper_method.should ==

Dynamic Paths in Helper

大兔子大兔子 提交于 2019-12-02 17:33:33
I'm trying to create a helper method for my admin links. In quite a few views I have the code <% if current_user %> <%= link_to "Edit", edit_model_path(model) %> <%= link_to "New", new_model_path %> <%= link_to "Delete", model, :confirm => "You're a Noob", :method => :delete %> <% end %> that only display these when logged in. I would like to do something like this in their place <%= admin_links(model) %> and pass the current item into the application helper method def admin_links(m) if current_user a = "#{link_to "edit" edit_m_path(m)}" a << "#{link_to "new" new_m_path}" a << "#{link_to