Destroy-like functionality using a custom controller method in Rails 3 - ruby-on-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.

Related

Content_for availability in application_controller after_filter

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.

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.

how to post :create from the default update url, rails 3

I'm sure this is pretty basic, but I'm somewhat new to rails and struggling to find a solution via search.
I'm implementing a message model to enable private messaging on a forum. I have the models resource nested within a users resource.
Currently the model works, but I want to enable a user to reply to a private message directly on the message show page. I.e users/1/messages/16 instead of users/1/messages/new. Currently this is the default route for 'update' within the MessagesController. Is there anyway to make the form on this page hit the 'create' action within the controller, instead of the 'update'?
Thanks.
Sure, I would try something like this:
On your show page just add a new form.
<%= form_for :message, :url => new_user_message_path do |f| %>
...
<% end %>
You can check the routes of your application using this command:
bundle exec rake routes
I suggest you to read the rails guide: http://guides.rubyonrails.org/

Rails app - Wanting something more than CRUD

I have a rails app where a User controller has all the CRUD methods new create destroy update however, I want an action now that does something more.
When the user clicks a button, I want to perform some logic and then forward them to their accounts page. The logic will go in the new method I make inside the controller. However, I cant find out how to make a form that submits to this new method I make in the User controller.
I'm trying something like this:
form_tag "/users"
but how do I make this form execute the new method I've made in the User controller
def some_logic
....
end
Update
After some reading, Is this the best way to go?
routes.rb
match '/download' => "users#some_logic"
view
= form_tag "/download"
This will execute the some_logic method
I tend to do in the view:
form_tag :action => :some_logic do
and in the routes you can have:
post "/download", :to => "users#some_logic"
That should make a form which sends the user off to the some_logic action within
UsersController
Take a look at the docs for further restful actions. You can find it here: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

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