Title says it all. It's pretty easy from controllers - just using "view_context". But it doesn't work with models.
E.g ApplicationController.new.view_context.url_for yields in: "undefined method 'host' for nil:NilClass"
ActionView::Base.new(Rails.configuration.paths["app/views"].first).url_for doesn't work as well.
Can this be done without so much pain ?
P.S I need it for my Prawn gem! I wanted to generate the PDF from the model. And apart from generating the links inside of it - it is very successful!
Hope for your help! Thanks in advance!
Rails 5
include Rails.application.routes.url_helpers
url_for(myModelObject)
It might depend on Rails version, but generally this method is now defined in Rails.application.routes. It expects you to provide hash with url_options, with :host key among others.
Related
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
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.
I'm a total noob to rails and I'm trying to understand a project I'm working on. I've found a method named cv_member_url in one of the views, but I can't for the life of me figure out where it is defined... One thing I do know about rails is that it is a very flexible language so it could be some sort of gem creating this method.
Any ideas where this method may have come from? (or better yet, how I can add others)
Thank you!
Do you have a model called CvMember? If so, the method is probably a named route for that model. See here for more info:
http://guides.rubyonrails.org/routing.html#paths-and-urls
To see all your named routes, you can run
rake routes
Those are named routes which are automatically defined based on what you have in routes.rb. *_url should be used in the controller, and *_path should be used in the views. Here's some more info from the official rails guide.
Assuming you can run this in development: You should put a breakpoint on it and step inside. Most likely it is dynamically defined or maybe in plugin.
I'm using Rails 3 beta 4 and trying to include ActionController::UrlWriter in my model, which is the correct way to go about it as far as i can tell, but i get "Uninitialized Constant ActionController::UrlWriter".
Any idea why that would be? Did it move in rails 3?
First I agree with zed. This shouldn't be done in a model. Your models should be unaware of any http url.
I do the same in a resque job. Here's what I do :
include ActionDispatch::Routing::UrlFor
include ActionController::PolymorphicRoutes
include Rails.application.routes.url_helpers
default_url_options[:host] = 'example.com'
Then you can call any usual url generator.
url_for(object)
page_url(object)
It'll create the link on the host defined as default_url_options[:host]
Answers to Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models? are pretty good
or see
http://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html
http://siddharth-ravichandran.com/2010/11/26/including-url-helpers-in-models/
Basically, you can do something like this in a model:
def url
Rails.application.routes.url_helpers.download_attachment_path(self)
end
It is worth considering whether this is the right layer, though. In my case it's for file attachments and I want to do attachment.url instead of writing out a helper a lot.
I'm new to rails and can't figure out this issue...
I have a controller
Admin::Blog::EntriesController
defined in app/controllers/admin/blog/entries_controller.rb
And I have a model called
Blog::Entry
defined in app/model/blog/entry.rb
When I try to access my model from the controller, I get a "uninitialized constant Admin::Blog::EntriesController::Blog" from this line:
#blog_entries = Blog::Entry.find(:all)
Clearly it is not finding the namespace correctly which is odd because according to what I have read, I have placed my model in the correct folder with the correct syntax.
Any ideas on how I can fix this?
Thanks
Try:
#blog_entries = ::Blog::Entry.find(:all)
It's currently looking for the wrong class. Using :: before Blog will force it to look from the top level.
It is now 2011 and we are in Rails 3.1 territory, but this issue still arises. I just ran into it with a namespaced controller referencing a non-namespaced model, but only when there were no rows for that model in the database!
Prefixing the model name with :: fixes the problem.
You can achieve a custom table name by using
set_table_name('foo')
at the top of your model.
As for multiple namespaces, you might be able to get away with using
polymorphic_path(#the_object)
to generate your urls as it does more basic inference (in my experience at least, maybe form_for uses it under the hood).
Yeah, from looking at the code form_for uses polymorphic_path under the hood.