How can I render a specific template? - ruby-on-rails

Rails 2.3.9.
I have this action in my ProcurementsController...
def view_downloads
#downloads = Procurement.find(params[:id]).downloads
end
I want to render this view: views/procurements/view_downloads.html.haml.
When I just leave it like that what gets rendered is the Procurements index page. (I thought is was supposed to render the template with the same name as the action by default.)
I looked in the docs for the render method and found this...
Method deprecated
This method is deprecated on the latest stable version of Rails. The last existing version (v2.3.8) is shown here.
Zero info about what to use instead, or where to get further information.
Does that mean it's deprecated in Rails 3 but I can still use it if I'm using 2.x?
Or is there now some totally new way to indicate what template you want to render now? If so which versions of Rails does it apply to?

Related

Rails - `flash.discard` vs `flash.delete`

As title, I wonder what's the difference between Rails flash.discard and flash.delete.
#SteveTurczyn is right.
However, flash.delete is only possible to do in versions of rails previous to 3.1.
As of 3.1, flash.delete doesn't work anymore, an alternative might be flash.clear (check out flash.delete(:notice) not working in Rails 3.1 RC?).
You might also want to check: https://apidock.com/rails/v3.0.0/ActionDispatch/Flash/FlashHash/discard
flash.delete is immediate. flash.discard will remove the flash at the end of the current action.
So using flash.discard you can still examine the flash contents in the current action.

Ruby on Rails: partial view inheritance

I want to get next thing...
# For ArticlesController > ApplicationController
# in view
render 'articles/edit/form'
# tries 'app/views/articles/edit/_form.html.erb'
# then tries 'app/views/articles/_form.html.erb'
# then what it wants
Or maybe render with array partial option:
# For ArticlesController > ApplicationController
# in view
render_exists ['articles/edit/form', 'articles/new/form']
# tries 'app/views/articles/edit/_form.html.erb'
# then tries 'app/views/articles/new/_form.html.erb'
# then what it wants
This isn't realized, is this? But maybe some gems for 3.2 or monkeypatches... And don't you know pull requests to rails about it? Thanks!
UPD That's isn't controller-based view inheritance. This should work for (at the same page):
render `articles/edit/form`
render `comments/edit/form`
I'm using the mechanism I described in an article in the rails forum
It works a treat for me though I hear there is now some built in support in the latest versions or at least effort is under way to do add such a feature.
That already exists, and it's very similar to the controller inheritance.
You must follow a conventional strategy, however. You would put your global partial in app/views/application, then you can put a more specific one at each inherited level, like app/views/articles.
Take a look at the following railscast for more details: #269 Template Inheritance

Rails dynamic select menu for 3.1

How would this be updated for Rails 3.1?
http://railscasts.com/episodes/88-dynamic-select-menus
I just can't figure out how to call the js.erb file and have it run the code to generate the javascript dynamically.
Might be something: in Rails 3.1, you're most likely using jQuery instead of Prototype. The example code on the Railscasts site is using good old Prototype instead of the new hotness that is jQuery (default javascript library in Rails 3.1).
Once all your jquery pipes are connected, having rails respond to and render your js.erb is the same as always. In your controller:
def country_selected
// whatever you need to do
respond_to do |format|
format.js
end
end
Then in your view directory, you have a country_selected.js.erb that you can put in whatever javascript you want to update the second select menu. (Remember you have to escape your shiz for it to work correctly) e.g.
<%= escape_javascript(params[:country]) %>
By the way, I think .rjs was moved out of Rails proper and into it's own Gem. Something else to keep in mind regarding Rails 3.1 vs. javascript.

Rails views inconsistently rendered

I like how Rails gives me flexibility in naming view files, ie index.erb or index.html.erb essentially do the same thing.
The problem is that I've merged two separate projects, where one uses filename.erb for all its views and the other uses filename.html.erb.
It seems that Rails expects only one naming scheme as I keep getting missing template errors for the views with only the .erb extension.
Can I get around this? Should I even want to get around this? Or should I bite the bullet and manually rename half of my view files?
To me it sounds like there may be a problem with the naming conventions you're using.
See what happens when you choose an action that isn't working and then explicitly try and render a template with:
render :template => 'products/show'
Where 'products/show' is the path to your layout in the views directory.
If that doesn't work it might help locate the issue.
Another thing to try is to use the format declaration from within your action:
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #products }
end
The docs here are also very explicit about how the conventions by which docs are found.
http://guides.rubyonrails.org/layouts_and_rendering.html
Hope that helps,
David
You should stick with the more modern rails convention of *.html.erb.
Are you using different versions of Rails? Rails versions below 2.0 wouldn't support the .html.erb format.

Why am I suddenly getting Missing Template errors with edge Rails (2.3)?

After freezing edge rails, all my controller examples are failing with
MissingTemplate errors.
e.g., "Missing template attachments/create.erb in view path app/views"
Trying to actually render the views gives me the same error.
I noticed I can fix most of them by using respond_to but I usually
never use it. I almost always only need to respond to one format in
one action so I omit respond_to and let Rails figure out which file to
render.
Does Rails suddenly require respond_to blocks in every action as of 2.3?
Just found this, which answers my question:
http://rails.lighthouseapp.com/projects/8994/tickets/1590-xhrs-require-explicit-respond_to

Resources