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
Related
What would be the best way to implement routes like github uses?
Ex:
github.com/about
github.com/37signals
github.com/javan
I'm guessing /about is a real controller, but the second and third probably load a user controller. What is the best way to do this?
Write a route like match '/:id/' => 'user#show' for a user like javan and have the show action in the user controller look up the user by username.
So in the user controller:
#user = User.find_by_username(params[:id])
You can learn more about routes here.
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.
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/
Greetings all, newbie to Rails here. I'm currently having issues routing /profile to the current user's profile.
My route is as follows:
match "profile" => "users#show"
However, this is hitting me with the "Couldn't find User without an ID" error. I know it has to do with my show method in the Users Controller. That code is simply:
def show
#user = User.find(params[:id])
end
Now, I could add another method in my Users controller with "#user = current_user" and it works fine. However, it seems a bit redundant and would also require a copy of the show view page. From what I've gathered with Rails, it's all about keeping things neat and tidy.
I would appreciate it if someone could point me in the right direction. Thank you.
RailsGuides states:
Because you might want to use the same controller for a singular route (/account) and a plural route (/accounts/45), singular resources map to plural controllers.
So I think you want to change your code to be the following
def show
#user = !params[:id].nil? ? User.find(params[:id]) : current_user
end
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