Content_for availability in application_controller after_filter - ruby-on-rails-3

As some context, I use this to build my breadcrumb. I print the breadcrumb to the page built from session state. In an after_filter I need to add to the session for the next page view.
In Rails 2 I was able to setup a view helper that used content_for, setting title and subtitle. In the after_filter in the application_controller I could use a helper to get at the content_for to add to the session
In Rails 3.2.x content_for accessed through the view_context helper in the after_filter is nil once. It's populated at nearly every other point however.
The challenge is finding a means to pass data from a view through to the controller where the session is available.
Does any one know how I can continue down this route or a decent way to pivot?
I still need to set the title and subtitle from the view and then be able to add them to the session.

Related

Creating an action inside a controller, after it has been generated

I am working on a rails app, and have generated a Controller via
rails g controller Pics index upload
but now I would like to add another action
delete
do I do this by manually adding a delete method in the Pics controller?
Or do I need to run another generation. My concern is that by adding manually something may not get included (not sure what it would be, but something under the hood.)
Is this the best way of adding a new action to a generated controller?
If you add manually, just make sure you have the right route on your routes.rb.
Let's say you create your delete action inside your Pics controller.
def delete
# do stuff
end
On your routes.rb, you need to append the route to your resource like this, remembering to analyse if it is a resource that acts upon a member of your resource, or a collection. (More about this you can read on this guide http://guides.rubyonrails.org/routing.html#adding-more-restful-actions).
resource :pics do
collection do
post :delete
end
end
Or
resource :pics do
member do
post :delete
end
end
Remember that all RESTFUL actions are handled by default by the rails router, again, try to read the guide i showed earlier for precise information about the topic. Hope it helps.

Render layout without a controller

I'm writing a Rails 3.2 app with backbone, and since I only need rails to render one page, I have no need for a controller to back the index page.
Is there a way to render the layout (application.html.erb) without a controller? I imagine it would be a configuration in the routes.rb file?
My first thought was to move it to index.html in the /public directory, but I need to take advantage of erb for javascript includes and CSRF helpers, etc.
I get that you don't need the controller to do anything, but Rails is "opinionated" software; it expects a controller and a view, because that is the way it was designed, and trying to work around that is going to give you a lot of trouble.
Just
create an empty controller class in /app/controllers/main_controller.rb
create an empty view file /app/views/main/index.html.erb
set up a route like :root => 'main#index'
Easy peasy.

Destroy-like functionality using a custom controller method in Rails 3

I have a resource called patient_admissions that has all the RESTful routes. It is nested under another resource called patients. I want to add another method to my patient_admissions controller called discharge that updates a field in the model called :discharge_date (with Date.now) and saves that value in the table.
I would like this to work like the destroy method, in that if I have a bunch of patient_admission objects listed in a table in my index view, I could just click on the Discharge link and a confirmation box would appear, I would click 'ok' and then the value would be updated without having to first go to another view and deal with forms.
How can I do this without resorting to something like javascript? Many thanks!
In the rails guide on Routing, there's a section on adding additional restful actions:
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
The example there would translate to something like:
resources :patient_admissions do
member do
put 'discharge'
end
end
This will recognize /patient_admissions/1/discharge with PUT, and route to the discharge action of PatientAdmissionsController.
This will at least allow you to get the routing set up for the action.
You could do this by using the link_to or button_to helpers in conjunction with a custom member route for your controller. Here is an example:
#routes.rb
resources :patient_adminssions do
put :discharge, :on => :member
end
Notice that I used PUT to add the custom route because the record will not be deleted, just modified. So according to the REST standards, I think put is the most appropriate.
# in your view
<%= button_to "Discharge", discharge_patient_admission_path(#patient_admission), :method => :put,
:confirm => "Are you sure you want to discharge this patient?" %>
This will create a button in a hidden form that when clicked will display the confirmation message and if it is confirmed then it will send a request to your controller action where you can set the appropriate discharge date like you suggested.

Can a Rails Template Determine Which Controller Called It?

In my Rails app, I want to update the contents of a header element in my application wide layout and have that content depend on which controller is handling the request. For example, if UserController is handling the request, then the header element content could be "User Page", but if the PhotoController is handling the request then the header element content could be "Photo Page". The solutions I've came up with (using content_for or setting instance variables) all seem to require code duplication and I'm looking for a DRY implementation. Is there a Rails variable that I can use in my view that reflects the current controller?
You can use the params[:controller] variable to determine this, or if you want something longer then controller.controller_name.

Rails app doesn't see my views

I've on a while on rails now and here's the problem I've been having on and on:
When I create a controller through:
"rails generate controller ControllerName ViewName"
I get everything working as I want but if for some reason I create the controller through:
"rails generate controller ControllerName"
and then just add ViewName.html.erb to the folder inside views that has the same name as my controller things would go wrong.
So the concrete case is me writing:
rails generate controller Subjects list show.
Which creates for me:
1.controllers>subjects_controller.rb
2.views>subjects>list.html.erb
3.views>subjects>show.html.erb
So this whole thing works fine.But as I already said if I need another view; let's say "new" I just add "new.html.erb" next to the other *.html.erb files and an action:
def new
end
to my subjects_controller.rb then it won't work.
The two previous views would keep working but any other "*html.erb" created outside the command line wouldn't.
Is there anywhere else where info about views is being stored?.
I'm a Windows 7 user (32 bit).Rails version=3.0.3. WebServer=WEBrick.
Text editor = E-TextEditor
This is most likely caused by your routes not being correctly configured. So it would be helpful to see the content of your routes.rb
In your case I think the best way to configure the routes is to use the resources mapping:
resources :subjects
This will by default create routing for the standard RESTful actions :index, :show, :edit, :update, :new, :create and :destroy.
For more detailed information about the routing, I would recommend Rails Routing from the Outside In