helpers

Rails: Preserving GET query string parameters in link_to

Deadly 提交于 2019-11-27 04:03:59
问题 I have a typical search facility in my app which returns a list of results that can be paginated, sorted, viewed with a different records_per_page value, etc. Each of these options is controlled by parameters in the query string. A simplified example: /search?q=test&page=2 Now say I need to display a set of links that set records_per_page value to 10, 20, 30. Each link must include the existing query parameters, which can be a very long set, plus a new per_page parameter. /search?q=test&page

Change protocol to https in all rails helpers

大城市里の小女人 提交于 2019-11-27 03:56:15
问题 Rails 3.1+ I want my url helpers to use the https protocol without having to specify it in every helper I call. After searching around I've found various ways but none work, for example: ROUTES_PROTOCOL = (ENV["RAILS_ENV"] =~ /development/ ? 'http://' : 'https://') scope :protocol => ROUTES_PROTOCOL, :path => "/app" do How can this be done? 回答1: So you want it mainly for links in emails? I think this will work in your production.rb, development.rb or any other environment. config.action

Why are all Rails helpers available to all views, all the time? Is there a way to disable this?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 02:45:22
Why can I access helper methods for one controller in the views for a different controller? Is there a way to disable this without hacking/patching Rails? @George Schreiber's method doesn't work as of Rails 3.1; the code has changed significantly. However, there's now an even better way to disable this feature in Rails 3.1 (and hopefully later). In your config/application.rb, add this line: config.action_controller.include_all_helpers = false This will prevent ApplicationController from loading all of the helpers . (For anyone who is interested, here's the pull request where the feature was

Override rails helpers with access to original

那年仲夏 提交于 2019-11-27 02:34:25
问题 I want to use rails' familiar helpers, but with slightly altered functionality. The way I see it, I want to be able to do something like: module AwesomeHelper #... create alias of stylesheet_link_tag to old_stylesheet_link_tag def stylesheet_link_tag(*args) if @be_awesome awesome_stylesheet_link_tag *args else old_stylesheet_link_tag *args end end end The way I see it, I have three options: Monkey patching: Reopening the rails helper module. If the rails team ever change the name of their

Custom form helpers

a 夏天 提交于 2019-11-27 00:29:42
问题 Is there a way that I can create a custom form helper so that instead of: special_field_tag :object, :method I can achieve something like: form.special_field :method 回答1: Yes, you can add to the FormBuilder class and get access to the object passed into the form_for. I've done this for a lot of things: dates, times, measurements, etc. Heres an example: class ActionView::Helpers::FormBuilder include ActionView::Helpers::TagHelper include ActionView::Helpers::FormTagHelper include ActionView:

Rails 3: @template variable inside controllers is nil

跟風遠走 提交于 2019-11-26 21:37:18
问题 i've started using rails 3 beta3 and I have see that the @template variable inside controllers is nil. How I can call helpers methods inside a controller? Thx 回答1: Use the view_context method instead of @template . This is because in Rails 3 the new AbstractController was introduced. You can read more here: http://apidock.com/rails/AbstractController/Rendering/view_context 来源: https://stackoverflow.com/questions/3300582/rails-3-template-variable-inside-controllers-is-nil

Rails- nested content_tag

我只是一个虾纸丫 提交于 2019-11-26 15:18:11
问题 I'm trying to nest content tags into a custom helper, to create something like this: <div class="field"> <label>A Label</label> <input class="medium new_value" size="20" type="text" name="value_name" /> </div> Note that the input is not associated with a form, it will be saved via javascript. Here is the helper (it will do more then just display the html): module InputHelper def editable_input(label,name) content_tag :div, :class => "field" do content_tag :label,label text_field_tag name,'',

Why are all Rails helpers available to all views, all the time? Is there a way to disable this?

孤街醉人 提交于 2019-11-26 10:15:22
问题 Why can I access helper methods for one controller in the views for a different controller? Is there a way to disable this without hacking/patching Rails? 回答1: @George Schreiber's method doesn't work as of Rails 3.1; the code has changed significantly. However, there's now an even better way to disable this feature in Rails 3.1 (and hopefully later). In your config/application.rb, add this line: config.action_controller.include_all_helpers = false This will prevent ApplicationController from