I want to be able to get Sinatra views from sub-directories of ./views (such as ./views/admin). I know you can set the views like so:
set :views, Proc.new { File.join(root, "templates") }
But how would I be able to set this for only part of the file?
I'm not sure exactly what you're asking, but you can render a view in views/admin by doing this:
erb :"admin/report"
If you're asking how to automatically look in subdirectories of views when you call erb :report, I'm not sure how to do that, and I don't think you'd want to (what happens if two views in different dirs have the same name?).
This is supposed to be an oversized comment. The answer provided by Alex is the correct one, but to be sure, this is a quote from official documentation:
One important thing to remember is that you always have to reference templates with symbols, even if they’re in a subdirectory (in this case, use: :'subdir/template' or 'subdir/template'.to_sym). You must use a symbol because otherwise rendering methods will render any strings passed to them directly.
Related
I have a controller named: CustomAuthController. Now I want to create a folder for this controller. What should I choose for this folder name. I mean what name is the best practice for laravel. is custom_auth is best pick. Is there any rule?
View folders in Laravel are made to help the structure of the front end of the app. It has nothing to do with what Controller you use to provide functionality for it.
For instance, you can have the layouts folder where you put all your blades which can be used into another blade as a reusable section. The views under layouts folder might also use your CustomAuthController controller. Does that make sense?
Name your view folders in a way which helps you understand the front end structure of your website.
As for the naming convention, always try to keep it one word, all small case and usually a plural word as it holds a group of blade views.
If you need to make it as two words, mostly snake_case is used but there is no written rule about it.
I've got a small problem (maybe it isnt even a problem)
I am making an application in Ruby, and the folder/file structure goes something like this:
OrderSet/
..item.rb
..file.rb
..order_object.rb
OrderGet/
..item.rb
..files.rb
..order.rb
As you can see I got two item.rb files, they are both different in class structure. Now I need to create an OrderSet/item.rb object, how do I specify it needs to look in OrderSet and does not get the OrderGet one?
I have to make clear, all files are required in the main rb file.
I have tried doing OrderSet.Item (the class is called Item inside the item.rb) but it complains about an ininitialized constant OrderSet
Thanks in advance!
[edit]
I have also tried to make modules out of it, maybe I don't understand the concept correctly, but I have tried it with OrderSet.Item.new (OrderSet as module name)
You could use a module to create a namespace - that way each set of classes would be encapsulated to what they do (the folder name from your example). So classes in OrderSet would be wrapped in a module for example OrderSet/item.rb would become:
module OrderSet
class Item
# methods and properties
end
end
Then you could use it like
new_order_set = OrderSet::Item.new
RubyMonk has a lesson called Modules As Namespaces which has more details and examples you can run in your browser.
my question considers the partial handling in Rails. I just can't get the reason for some design decisions.
If I render a partial like this:
<%= render partial: 'foo/bar', object: #herbie %>
Where #herbie is an object of class Car. Why is the local variable called bar by default instead of car which - at least for me - would be more reasonable in most cases.
If various partials share a common layout I have to add the layout: ... each time I use render partial:. This seems odd to me! Isn't it more likely to have various partials wich share the same layout then the other way around? Wouldn't it be more reasonable to define the layout within the partial, or to add a to_layout_path method to the ActiveRecord Model?
Update:
I get the point that it might be better to use the partial name, as this is well known in the partial context. However the use of the tags :locals and :as screws up this argumentation.
In an actual case I want to use different partials depending on the state of an object (questions that could be open or closed). I moved the decision which partial to render to the Question.to_partial_path method which seems to be quite elegant. However I named the partials open_question and closed_question and this makes the partial code unreadable.
It gets even worse if I consider different types of questions.
I expect that within a partial we always make some presumptions about the model that is passed in. Thus in closed_question I am quit sure that I get a question - thus I would expect the variable to be called question. On the other hand I can imagine situations where we pass in something different that just behaves appropriated (Duck-typing).
My temporal meaning about partials is that within the partial it should be possible to define the name of the given parameter as well as a layout file that is used.
The parameter indeed can be realized by introducing a new variable ( question = open_question) which feels a little bit unclean to me.
When you pass in an argument to a method there's no way of knowing what the "name" of that variable is. The only thing the partial rendering method here has at its disposal is the foo/bar name, the object attribute, and some model.
That your model originated in a variable called #car is completely lost in the process of making the call.
It's convention in Rails that the variable used within the partial is the same as the name of the partial itself. You can override this if you want to have non-standard behaviour:
render(partial: 'foo/bar', locals: { car: #car })
As a note, though, going down the non-standard path is usually a bad idea. Whenever possible, just rename your partial to better match your expectations.
This is related: render partial :object vs :locals
The documentation is pretty good: http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html
The default is to name the object after the partial. If you want to be explicit, you can use :as or locals.
render partial: 'foo/bar', locals: {car: #car}
render partial: 'foo/bar', object: #car, as: 'car'
I am attempting to use the Exhibit pattern in a padrino application but to do so properly I need to access the rendering context. That is, I need to have some object context such that calling something along the lines of context.render('accounts/index') will produce identical output to when render 'accounts/index' is called from inside a controller (assuming that all variables are set the same and that app/views/accounts/index.erb is a template file).
I can't find anything that does this within the padrino public API, so I attempted to dig deeper. The existing render method in the controller wraps sinatra's render method, which is a private instance method. So it's not obviously not defined on the class MyApp (i.e. the Padrino::Application subclass). MyApp.new results in an instance of Rack::Session::Cookie rather than of itself, and none of the padrino methods that I can find return the existing instance of the app, which would be a suitable candidate for the context object if I called its private render method.
Is this something that's just inherently too difficult to bother with given sinatra's and padrino's designs, or is there something that I'm missing here?
I realise that the answer was in the Objects on Rails book if only I'd kept reading. I needed to pass the context explicitly from within the template: exhibit(object, self) inside the template causes the template's rendering context to be passed as the second argument. Of course.
I guess this really wasn't specific to padrino after all and I was looking for something complicated when the answer was very simple.
using CodeIgniter normally one has to specify the controllers in the config/routes.php file.
This is not to handy, so I would like to be able to do something like this in a controller.
get url parts and check if the first part is specified in an array
if so, load the specified controller, if not, load default controller.
It basically mimics the behavior of the routes file, but there is no need to specify the wildcards before. I am using a base controller I extend with every controller, but I would like to have this controller just load (or include) the needed controller.
Does anyone have any idea how I can do this in a good way?
Thanks in advance.
// Edit
Okay, so here is my scenario.
I have cms and users can choose to include modules (e.g. a gallery).
I need to inlcude all the gallery php scripts without having to have "gallery" in the url.
I figured it would work if I use a "main controller" which loads another controller depending on the modules chosen. I realize this might not be the best way, so if there is a "clean" way to do it, please tell me.
As far as I know models are just for database stuff, so putting a whole gallery in there is not right either. The Plugin itself will of course be a library, but there is going to be some code to load the libs depending on the demands, get the database data, etc.
Thanks
The way you're doing this is incorrect. You never should take over the routing function to do this. What you need is to use some kind of module functionality to include the required methods and models; a module doesn't require to have route-accessible methods, so it's basically a "library" with a model and views.
If I remember correctly there are several plugins that provide this, One was HMVC (google it).
The ideal form would be to load the "Module" on demand from your controller, like you do with the core CodeIgniter Libraries; so lets say you're inside the blog controller action and you want to include the comment module which is used on gallery and on images, you just include the module and call it's methods to get the data which you can then pass to the view as needed; you can even render partials and store them into a variable to pass to your master controller.
Hope this is enough to get you on the right track :)
I may be misunderstanding your question but you want to load your controllers if you go to them and if not you want to go to your default.
If I am understanding correctly you can do a couple things in your routes have a route that takes everything to your default controller.
In your controller have an array of all your controllers then implode the array to a regular expression
$array = [c1, c2, c3, c4];
$str = implode('|', $array);
$regex = "($str)"
now just add your regex to a route
now redirect as you see fit.
But this is really what the routes file is for you you are just dancing around something that should be used.