Using link_to_remote inside of a controller - ruby-on-rails

how can I use link_to_remote inside of a controller?
I have already included ActionView::Helpers::JavaScriptHelper, but still get the error:
undefined method `content_tag'
Thanks!

You'll need to discover all of the helpers that need to be included to make it work. For instance, content_tag is part of ActionView::Helpers::TagHelper.
http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M002139&name=content_tag
It may be time to consider an alternative design.

Related

Why can't I call an ActionView method from the Rails Console?

I'm trying to do a little testing of the number_to_currency method, but no matter how I try and call it in the console I'm out of luck it seems.
ActionView::Helpers::NumberHelper::number_to_currency 12321334543.00
# => NoMethodError: undefined method `number_to_currency' for ActionView::Helpers::NumberHelper:Module
This may qualify as a duplicate, I am not entirely sure. The other question is a lot broader in scope, and therefore has a lot more ways of getting things gone presented in the answers, and I also believe it may have some outdated answers.
ActionView::Helpers::NumberHelper is a module. Whenever you want to use it you should include it and all it's method could be used after that. So the first step is including ActionView::Helpers::NumberHelper module:
include ActionView::Helpers::NumberHelper
and then call the method you want:
number_to_currency 12321334543.00
Take a look at Ruby modules tutorial.
Edit:
Getting a console session bootstrapped with Rails’ helpers is also a pain, which helper can fix for you! You could also use it to play with building HTML tags, or any existing Rails helper that ActionView knows about (checkout this blog post).
helper.number_to_currency 12321334543.00
try helper.number_to_currency 12321334543.00

Ruby on Rails - undefined local variable or method - wice grid haml show_code

I am new to ruby on rails.
I am trying to get haml and wice_grid to work together. I am using this example as a model:
http://wicegrid.herokuapp.com/basics3
I get the error 'undefined local variable or method `show_code' for...'
In the file app/views/basics3/index.html.haml which you can see at the link above.
Am I missing a gem? In general, what is the best way to troubleshoot problems like this?
Thanks in advance-
Flex
EDIT: I found the definition for show_code. It's in a helper that I found in the unit tests for wice_grid.
https://github.com/leikind/wice_grid_testbed/blob/master/app/helpers/application_helper.rb
That said, I get more errors when I load it into my project. So the question becomes, how does the helper normally get included in my project?
show_code is a custom method created just for the example page you linked to. It just displays the code he has in his controller and his index and grid views. You don't need to call that method in your own application so just remove that line and you should be good.

Mountable Engine & Redefining views

I've developed a mountable engine that has partial at xxx/admin/shared/_menu. This partial is responsible for rendering a menu.
My application defines a few controllers, which are part of the backend, and, of course, I want to include them in the menu.
It seems to me that the easiest approach is to create a partial in the engine and then override it in the (containing) application. However, when I try to use a url helper inside of it, it gives me an error.
For example:
<%= admin_posts_url %>
Gives
undefined local variable or method `admin_posts_url ' for...
Is there way to fix it? May be there is another good way to do it?
Rails engine provides main_app helper to be used inside an engine by which you can refer to application's helpers.
Ref : http://guides.rubyonrails.org/engines.html#routes
Try using
main_app.admin_posts_url

Rails automatically call application helper methods any time form_for is called

In my application, any time there is a form, I call two helper methods to inject some javascript into the head of the web page. One method injects code to focus on the first input of the form and the second method hooks up some call backs that I use with the client_side_validation gem.
I will be calling these two methods on any page that has a form_for in the view. Is there a way to automatically invoke these methods any time form_for is called as opposed to adding the calls to my template files?
Using Rails 3.1.
You should wrap form_for with your own method that calls your helpers. Fortunately Rails provides an easy way of doing this in the form of alias_method_chain.
module ActionView::Helpers::FormHelper
def form_for_with_my_helpers (*args)
my_helpers
form_for_without_my_helpers (*args)
end
alias_method_chain :form_for :my_helpers
end
You can now call form_for_without_my_helpers with the normal form_for arguments if you don't want a form to use your helpers.
This can go somewhere that's in the load path. Or you can require the file holding this code.
You shouldn't be using Rails helper methods for these. You should be using jQuery. Add this to your application.js file:
$(document).ready(function() {
$('form>input:first-child').focus();
}
That's an example, use a similar method for the callbacks you're talking about.

Using send_file in helper

I'm trying to use send_file method in application_helper.rb and receiving the following error:
undefined method `send_file' for #<#<Class:0x00000003cc7ad8>:0x00000003cc4ea0>
Is there a way to get around this? I know that I can put my helper inside controller but I don't want to do that.
You can't send a file from a helper.
The send_file method is defined in ActionController::Streaming and is therefore not available in helpers. Helpers are for your code which helps with views. If you're writing a 'helper' for a controller, it shouldn't really be a helper. It should be a module in lib which you require and include in the controller.
You can't use send_file inside your helper.
Explain, why do you need to send file from your view helper and how should it work?

Resources