Creating Custom Post Method - ruby-on-rails-3

I'm trying to create a a custom post method but i'm having trouble finding how to start.
What I want to do is to be able to read a CSV file, foreach entry, insert a new row into a database.
In the index file, I want to be able to hit 1 link or button to start a custom method.
This method would open my csv file (traverse each row and insert into database)
So essentially on my index.html.erb I would like to see something like:
<%= link_to "Load CSV to Database", :controller => MyController, :action => MyCustomAction %>
I believe I need to edit my routes.rb and this is where I'm stuck. How do I make it so that my routes know that MyCustomAction is a post.
My Rake Route:
use_database_csv_files POST /csv_files/use_database(.:format) csv_files#use_database
csv_files GET /csv_files(.:format) csv_files#index
POST /csv_files(.:format) csv_files#create
new_csv_file GET /csv_files/new(.:format) csv_files#new
edit_csv_file GET /csv_files/:id/edit(.:format) csv_files#edit
csv_file GET /csv_files/:id(.:format) csv_files#show
PUT /csv_files/:id(.:format) csv_files#update
DELETE /csv_files/:id(.:format) csv_files#destroy
Thanks

You can try:
resources :MyController do
collection do
post 'MyCustomAction'
end
end
This blog post may also help you if you want to do member instead of collection

Related

Rails 3 form actions and methods

I have made a resource.
resources :dashboards
I have a partial file which contains a form and I want to use this partial (as the form elements won't change) to update and create. So here is what I have:
Controller
class DashboardsController < ApplicationController
def new
#dashboard = Dashboard.new
end
end
View
/dashboards/new.html.erb
<%= render :partial => "form", :locals => { :dashboard => #dashboard } %>
Partial Form
/dashboards/_form.html.erb
<%= form_for(#dashboard) do |form| %>
.....
<% end %>
Ruby Guide
The Ruby Guide states:
The Article model is directly available to users of the application, so — following the best practices for developing with Rails — you should declare it a resource. When dealing with RESTful resources, calls to form_for can get significantly easier if you rely on record identification. In short, you can just pass the model instance and have Rails figure out model name and the rest. For example:
## Creating a new article
# long-style:
form_for(#article, :url => articles_path)
# same thing, short-style (record identification gets used):
form_for(#article)
## Editing an existing article
# long-style:
form_for(#article, :url => article_path(#article), :html => { :method => "put" })
# short-style:
form_for(#article)
Result
I thought I have followed the Rails Guide correctly. Because I made #dashboard a resource. I could just pass it into the form and have it handle the action, method and the rest. Instead I'm getting this:
<form accept-charset="UTF-8" action="/dashboards" class="new_dashboard" id="new_dashboard_" method="post">
According to the docs. Shouldn't the action of my form now be "/dashboards/new" because we are on the new action? And should it be passing an extra field declaring the method to be put when I use the same code in the /edit action??
My result is always the same no matter what. The form never changes.
What am I doing wrong?
EDIT
Here is my router info from rake routes
GET /dashboards(.:format) dashboards#index
POST /dashboards(.:format) dashboards#create
GET /dashboards/new(.:format) dashboards#new
GET /dashboards/:id/edit(.:format) dashboards#edit
GET /dashboards/:id(.:format) dashboards#show
PUT /dashboards/:id(.:format) dashboards#update
DELETE /dashboards/:id(.:format) dashboards#destroy
You are correct that you should be able to "pass #dashboard into the form and have it handle the action, method and the rest." The issue here is what new is in the context of RESTful actions.
When you declare a set of resources with resources :dashboards, you are creating a set of routes which map requests to controller actions:
GET /dashboards index
GET /dashboards/new new
POST /dashboards create
GET /dashboards/:id show
GET /dashboards/:id/edit edit
PUT /dashboards/:id update
DELETE /dashboards/:id destroy
You can check this if you run rake routes.
The issue here is that the new action is defined as a GET request to the path /dashboards/new, i.e. this is the route for the form itself. The URL in the action attribute of the actual form is something else: this is where the form will post the data to with a POST request, which on the server (rails) side will map to the create controller action.
When you use the form helper with form_for(dashboard), a form is created with a route corresponding to what dashboard is: if it is a new record (i.e. it does not yet exist in the database), then the form action will be create (and point to /dashboards), whereas if it already exists it will point to the actual URL for the record (e.g. /dashboards/123). This is what makes the form helpers so useful.
So, to sum up, /dashboards is the correct URL, not for the new action but for the create action, which the form helper uses because dashboard is a new record. new is the route to the page where the form resides, i.e. /dashboards/new.
Hope that makes sense.
p.s. as a side note, you shouldn't be accessing #dashboard in the partial if you are passing it in as a local (:locals => { :dashboard => #dashboard }). Just use dashboard.

Rails _path helper generating path with format not id

In my routes.rb I have:
resources :aquariums do
resources :management_roles
resources :graphs
resources :animals
end
get 'aquarium', to: 'aquariums#show', :as => :aquarium
The reason for the last get is I have the notion of "current aquarium" in my app. If, say, current_aquarium is set to 1, then in my controller's 'show' action '/aquarium' gets the same rendering as '/aquariums/1' with code like
#aquarium_id = params[:id] || current_aquarium.id
Now, and I'm assuming this is thanks to this different routing, this code:
<%= link_to aquarium.name, aquarium %>
or
<%= link_to aquarium.name, aquarium_path(aquarium) %>
Generates paths like this:
/aquarium.1
where I'd typically expect:
/aquariums/1
Right?
Per request... here's what rake routes | grep aquar yields:
(I'm assuming it's that very last route that is messing things up, but I would have thought that it would process these in order. And, just FYI, I originally had that route at the top. Moved it to the bottom assuming it would fix).
aquarium_management_roles GET /aquariums/:aquarium_id/management_roles(.:format) management_roles#index
POST /aquariums/:aquarium_id/management_roles(.:format) management_roles#create
new_aquarium_management_role GET /aquariums/:aquarium_id/management_roles/new(.:format) management_roles#new
edit_aquarium_management_role GET /aquariums/:aquarium_id/management_roles/:id/edit(.:format) management_roles#edit
aquarium_management_role GET /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#show
PUT /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#update
DELETE /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#destroy
aquarium_graphs GET /aquariums/:aquarium_id/graphs(.:format) graphs#index
POST /aquariums/:aquarium_id/graphs(.:format) graphs#create
new_aquarium_graph GET /aquariums/:aquarium_id/graphs/new(.:format) graphs#new
edit_aquarium_graph GET /aquariums/:aquarium_id/graphs/:id/edit(.:format) graphs#edit
aquarium_graph GET /aquariums/:aquarium_id/graphs/:id(.:format) graphs#show
PUT /aquariums/:aquarium_id/graphs/:id(.:format) graphs#update
DELETE /aquariums/:aquarium_id/graphs/:id(.:format) graphs#destroy
aquarium_animals GET /aquariums/:aquarium_id/animals(.:format) animals#index
POST /aquariums/:aquarium_id/animals(.:format) animals#create
new_aquarium_animal GET /aquariums/:aquarium_id/animals/new(.:format) animals#new
edit_aquarium_animal GET /aquariums/:aquarium_id/animals/:id/edit(.:format) animals#edit
aquarium_animal GET /aquariums/:aquarium_id/animals/:id(.:format) animals#show
PUT /aquariums/:aquarium_id/animals/:id(.:format) animals#update
DELETE /aquariums/:aquarium_id/animals/:id(.:format) animals#destroy
aquariums GET /aquariums(.:format) aquariums#index
POST /aquariums(.:format) aquariums#create
new_aquarium GET /aquariums/new(.:format) aquariums#new
edit_aquarium GET /aquariums/:id/edit(.:format) aquariums#edit
aquarium GET /aquariums/:id(.:format) aquariums#show
PUT /aquariums/:id(.:format) aquariums#update
DELETE /aquariums/:id(.:format) aquariums#destroy
aquarium GET /aquarium(.:format) aquariums#show
Thanks in advance!
Greg
Change that last route from this:
get 'aquarium', to: 'aquariums#show', :as => :aquarium
to this:
get 'aquarium', to: 'aquariums#show', :as => :current_aquarium
The problem is that you have to routes named the same thing:
aquarium GET /aquariums/:id(.:format) aquariums#show
aquarium GET /aquarium(.:format) aquariums#show
If you make the change above then that second route will not match when you make those link to calls... as it stands now, the second one is matching and like the route says is using your argument as the :format.
If you do make this change, you may need to tweak some things if you are intentionally linking to 'current_aquarium'.

Defining a route for a method in rails 3

I'm new to Rails and currently using Rails 3, so please bear with me. I have a basic app, with a basic scaffolded controller/model e.g Contacts.
Amongst the methods for Show/Edit etc.. i have added a method called newcontacts (i have also added a newcontacts.html.erb), which will eventually show the last 5 contacts imported , but at the moment i am using the same code one would find in the basic Index method of a controller (i intend to filter the data at a later point), the method in the controller is -
def newcontacts
#contacts = Contact.all
respond_to do |format|
format.html # index.html.erb
end
end
I can access localhost:3000/contacts which displays the index method action from the contact controller, but when i try and access this method (newcontacts) using localhost:3000/contacts/newcontacts it returns the error
Couldn't find Contact with id=newcontacts
I have looked at the routes.rb file as i believe this is what needs editing, and have added the following line to routes.rb
match 'newcontacts', :to => 'contacts#newcontacts'
but this only works when i call localhost:3000/newcontacts.
So my question is, how do i get the url localhost:3000/contacts/newcontacts to work?
Any help would be great.
I think what you're trying to do is add another RESTful action.
resources :contacts do
# This will map to /contacts/newcontacts
get 'newcontacts', :on => :collection # Or (not and; use only one of these)...
# This will map to /contacts/:id/newcontacts
get 'newcontacts', :on => :member # ... if you want to pass in a contact id.
end
Try this in your routes.rb file:
resources :contacts do
member do
put 'newcontacts'
end
end
That will add in a new action for the contacts controller.

REST path for "new from copy"

For certain models, I wish to provide functionality that allows a user to create a new record with default attributes based on copy of an existing record.
I'm wondering what would be the correct restful route for this.
My initial thinking is that it could be a parameter to the new action. I.e. to borrow from the the Rails Guides examples, instead of just:
GET : /photos/new
Also allow:
GET : /photos/new/:id
...where :id is the id of the record to use as a template. The response would be a new/edit form, same as with a plain old new but the values would be pre-filled with data from the existing record. The parameter (or absense of it) could be easily handled by the new controller method.
The alternative seems to be to create a new controller method, for example copy which would also accept an id of an existing record and response with the new form as above. This seems a little 'incorrect' to me, as the record is not actually being copied until the user saves the new record (after probably editig it somewhat).
TIA...
UPDATE: my question is not "how do I do this in rails?", it's "is it RESTful?"
my question is not "how do I do this in rails?", it's "is it RESTful?"
No, it isn't. For that matter, neither is GET /photos/new. Rails seems to be hopelessly mired in the past, where it was considered haute programme for a GET on a URI to return an HTML form which would then POST x-www-form-urlencoded data back to that same URI. The opacity of that POST forces them to invent new verbs-as-URI's like /photos/new, when you could be using PUT instead, or at least POST with the same media type.
The simplest way to make a copy of an HTTP resource RESTfully is:
GET /photos/{id}/ -> [representation of a photo resource]
...make modifications to that representation as desired...
POST /photos/ <- [modified representation]
If you're implementing this for browsers, you should be able to perform those actions via Ajax quite easily, using an HTML page sitting perhaps at /photos/manager.html/ to drive the interaction with the user.
You can try to use nested resources. I'm not exactly sure about structure of you application, but in general using nested photos will look somehow like this:
routes.rb
resources :photos do
resources :photos
end
photos_controller.rb
before_filter :find_parent_photo, :only => [:new, :create]
def create
#photo = Photo.new params[:photo]
if #parent_photo.present?
# fill some #photo fields from #parent_photo
end
#photo.save
respond_with #photo
end
def find_parent_photo
#parent_photo = Photo.find(params[:photo_id]) if params[:photo_id].present?
end
new.html.haml
= form_for [#parent_photo, #photo] do |f|
-# your form code
previously when you wanted to add a link to photo creation you wrote something like that
= link_to "new photo", [:new, :photo]
now if you want to add a link to photo creation based on foto #photo1
= link_to "new photo based on other one", [:new, #photo1, :photo]
You should be able to match a route like so:
match 'photos/new/:photo_id' => 'photos#new
or you could just pass a :photo_id parameter in the url and handle it in the controller:
'/photos/new?photo_id=17'
Example using helper method: new_photo_path(:photo_id => 17)
Edit: I don't know if this conforms to REST
It may be over the top, but you could do something like this:
class PhotoCopiesController < ApplicationController
def new
#photo = Photo.find(params[:photo_id]).dup
end
def create
end
end
and
resources :photo_copies, :only => [:new, :create]
and
= link_to 'Copy', photo_copy_path(:photo_id => #photo.id)

Beginner struggling with update_attribute command

I am in the process of trying to use the update_attribute command, but struggling to get it working (at all) and hoped someone could point me in the right direction?
I have previously posted a question about this issue, it was very useful in terms of giving a feel for the mechanics of what is going on, but unfortunately it didn't actually get it working.
I have a database of items (Items), which among other things contains ':item_name', ':click_count' and ':external_url'.
Currently I have a view (Showselecteditems) in which there is a list of all the items, when a user clicks on an item name, they are directed to the appropriate external url. This works using the code:
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url %>
I would now like to add the ability to count the number of times a particular item name has been clicked on (i.e. in total for all users, not individual users) and therefore the number of times each external url has been visited in order to work out which is most popular.
Reading around, I believe i need to modify the code above to something of the form:
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
And need to define this function somewhere - it seems to only be found if located in 'application_helper'?
def clickcountplusone
clickeditem = Items.find(params[:identifier])
clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
rescue ActiveRecord::RecordNotFound # to avoid error if no identifier value
end
Needless to say, I cannot get this to work... My question is therefore, how can I set things up correctly so that when the link is clicked on the count is incremented? The other common problem people seem to report is that the number will be incremented each time the page is refreshed, which I would like to avod if possible.
Previously people have suggested adding to the 'show' section of the 'Items' controller, however, i don't know how this would work as the links are being clicked on the Showselecteditems view page, not the database itself where you get the show, edit, destroy commands. Any advice greatly appreciated.
This
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
will not point user to the some_controller#clickcountplusone, because you already specified an external link.
The easiest way to do this job is to modify your link_to like:
<%= link_to selecteditem.item_name.to_s, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
And then to modify your actions source:
def clickcountplusone
clickeditem = Items.find(params[:identifier])
redirect_to clickeditem.external_url if clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
rescue ActiveRecord::RecordNotFound # to avoid error if no identifier value
end