Destroy? Delete? What's going on here? Rails 2.3.5 - ruby-on-rails

I am new to rails. My rails version is 2.3.5. I found usage like:
In controller, a destroy method is defined and in view, you can use :action => "delete" to fire that method. Isn't the action name has to be the same as the method name? Why delete is mapped to destroy?
Again, in my controller, I define a method called destroy to delete a record. In a view, I have <%= link_to "remove", :action => 'destroy', :id => myrecord %>. But it never works in practice. Every time I press the remove link, it redirects me to the show view, showing the record's content. I am pretty sure that my destroy method is:
def destroy
#myobject = MyObject.find(params[:id])
#myobject.destroy
#redirect_to :action = 'index'
end
If I change the method name from destroy to something like remove_me and change the action name to remove_me in the view, everything works as expected.
In the above two weird problems, I am sure there is no tricky routing set in my configuration.
All in all, seems the destroy and delete are mysterious keywords in rails. Can anyone explain this to me?

You probably set MyObject as a resource in routes.rb. Resources get a couple of routes that don't directly match the name of the action. When you use an action name that does not match the routes defined by the resource, you'll get the default route which directly maps to the name of the action.
I found that this link explains rails' routing very well. Take a look at the "RESTful routing" section.

If you are using REST routing, destory only support delete method. you can change your code like this
link_to "remove", :action => 'destroy', :id => myrecord", :method => :delete
Adding :method => :delete
rails will add a hidden input with name "_method", value "delete"

Replace all :post => true with :method => :post

Related

how can I specify controller and method in a link using haml with rails?

I am new in ralis, and I am trying to create a link in what calls an specific method of a specific controller, in this case car and method add_to_cart sending a parameters, unfortunately I dont know how to do it
For example, I want something this, but even if I scape characters
= link_to "Add to cart", {:controller => "car", :action => "add_to_cart", :car => car.id }
If I paste it just like that I get an error saying No route matches , any way to make it? or maybe in the old format (I dont understand it) I guess should be something like
=link_to "car", add_to_cart_car_path(#car.id), class: "btn"
Also shows me an error,
Any idea of this?
Assuming you have a route setup for the resource called car:
resources :cars
You can add a member action by changing it to:
resources :cars do
get :add_to_cart, :on => :member
end
You can now write:
= link_to "Add to Cart", add_to_cart_car_path(#car), class: 'btn'
You can add the route to routes.rb:
match 'add_to_cart' => 'cars#add_to_cart'
Remember to restart your rails server for the new route to be recognized.

Rails - custom action in controller - how to reference in view?

I'm using a custom action to get the id of a project into the session, so that only relevant info for that project is shown in other areas. I've made a custom action in the projects controller, and am having trouble getting a link to work in the view to call that action. I just get an error saying "Couldn't find project without ID". I'm new to rails - I know it's probably an easy question, but help would be much appreciated, thanks!
View Code:
<%= link_to 'Select Project', :action => :select_project %>
Controller Code:
def select_project
#project = Project.find(params[:id])
session[:project_id] = #project.id
end
Routes:
resources :projects do
collection do
get :select_project
end
end
Alternative routes code:
resources :projects do
put 'select_project', on: :member
end
This is untested but I believe it is what you are looking for:
Routes:
resources :projects do
member do
post :set_current
end
end
this should create the following:
Endpoint: /projects/:id/set_current POST
Helper: set_current_project_path
Controller
def set_current
project = Project.find(params[:id])
session[:project_id] = project.id
redirect_to projects_path, :notice => "Current project set to #{project.name}"
end
Views
# index / erb tags excluded for simplicity
#projects.each do |project|
link_to 'Select Project', set_current_project_path(project), :method => :post
end
# show
<%= link_to 'Select Project', set_current_project_path(#project), :method => :post %>
See:
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
Note also the use of 'post' instead of 'get', since we are changing the state of an object (session)
it is preferred to use a post not a get, otherwise users might pull up an old get request in the address bar
of their browser and set their session to a project unknowingly.
like varatis said - use rake routes or CONTROLLER=projects rake routes to help with determining what your route/path helpers look like and what http verbs they are expecting
And is there a reason why it's project not #project in the controller
The #project creates an instance variable; in a rails controller instance variables are made available to the views. This set_current action will never render a view, so no reason to make an instance variable out of it.
How come you have to set it to member and not collection in the routes
any action where you want to reference params[:id] should be a member route, an alternative would be to leave it as a collection route and pass params[:project_id] and pass that in all of your link_to calls, but in this case member makes more sense.
I believe resources :projects is a short cut for this break down
member do
get :show
get :edit
put :update
delete :destroy
end
collection do
get :index
get :new
post :create
end
hopefully that clarifies your questions some?
I think the route generated would be select_project_projects_path.
Link:
<%= link_to 'Select Project', select_project_projects_path %>
For future reference, run rake routes to see the automatic route helpers generated by Rails.

Force current_user path

Currently users can access their "profile" through many paths.
localhost:3000/users/current_user
localhost:3000/users/current
localhost:3000/users/id#
How can I make it that they can only get to their "profile" through localhost:3000/users/current_user
One suggestion on the 'what' of your question: instead of the ideal url being localhost:3000/users/current_user I suggest localhost:3000/user or something even more descriptive such as localhost:3000/profile or localhost:3000/account.
Could you include the entries in your routes.rb? Even if Authlogic, etc. add routes to your app, they should do it in routes.rb. If you have the entry:
map.resource :users
then that's where the /users/123 route is coming from. I agree with Matchu that even if you don't use /users/123 you should keep it and route other requests to it.
An Additional Idea
If you don't want to get into the (kinda complicated, and not pretty) business of preserving model validation errors across redirects, here's another way. I'm assuming from here that you already have map.resource :users, so that you have the 7 default actions on your UsersController (index, new, create, show, edit, update, destroy).
In your routes.rb:
map.profile 'profile', :controller => 'users', :action => 'show'
map.edit_profile 'profile/edit', :controller => 'users', :action => 'edit', :conditions => { :method => :get }
map.update_profile 'profile/edit', :controller => 'users', :action => 'update', :conditions => { :method => :put }
You will need to update your form_for tag slightly:
<% form_for #user, :url => update_profile_path do |f| %> ...
Now, assuming you start on /profile, and click an edit link that takes you to /profile/edit (should show the form), if you fill out the form such that it fails validation then you should end up back on /profile/edit with the correct errors in the f.error_messages output.
Your controller code for edit should stay the same, and your code for update should be:
def update
#user = current_user || User.find(params[:id])
if #user.update_attributes(params[:user])
flash[:notice] = "Successfully updated user."
redirect_to #user
else
render :action => 'edit'
end
end
The render (rather than a redirect) preserves the state of the #user model (including errors) and just renders the edit template again. Since you directed it at the update_profile_path the url viewed by the user is still /profile/edit.
Umm, first, remove the /users/current route that you must have in your routes.rb somewhere. (Although I prefer /users/current to /users/current_users, since the latter is rather redundant.)
As for /users/123, in your controller, you can check if the current user's ID matches 123 or whatever, and, if so, redirect.
But I really prefer the opposite effect. Pushing /users/current to /users/123 makes more sense in my brain, since it keeps the routes consistent for all users while still allowing you to cache links to /users/current.

Pretty URL in Rails when linking

Let's say I have a Ruby on Rails blogging application with a Post model. By default you would be able to read posts by http://.../post/id. I've added a route
map.connect ':title', :controller => 'posts', :action => 'show'
that will accept http://.../title (titles are unique) and the controller will do a query for the title and display the page. However when I am now calling <%= link_to h(post.title), post %> in a view, Rails still gives me links of the type post/id.
Is it possible to get Rails to automatically create the pretty links for me in this case?
If you are willing to accept: http:/.../1234-title-text you can just do:
def to_param
[id, title.parameterize].join("-")
end
AR::Base.find ignores the bit after the id, so it "just works".
To make the /title go away, try naming your route:
map.post ':id', :controller => 'posts', :action => 'show', :conditions => {:id => /[0-9]+-.*/ }
Ensure this route appears after any map.resources :posts call.
You can override ActiveRecord's to_param method and make it return the title. By doing so, you don't need to make its own route for it. Just remember to URL encode it.
What might be a better solution is to take a look at what The Ruby Toolbox has to offer when it comes to permalinks. I think using one of these will be better than to fixing it yourself via to_param.
I would use a permalink database column, a route, and I normally skip using link_to in favor of faster html anchor tags.
Setting your route like:
map.connect '/post/:permalink', :controller => 'post', :action => 'show'
then in posts_controller's show:
link = params[:permalink]
#post = Post.find_by_permalink(link)
You link would then be
Link
then in your create method, before save, for generating the permalink
#post = Post.new(params[:post])
#post.permalink = #post.subject.parameterize
if #post.save
#ect
There is a Gem for you to get this done perfectly
https://github.com/rsl/stringex

Ruby on Rails: Using a "complete_tasks_controller" for RESTful Rails

I'm having troubling completing a task the RESTful way. I have a "tasks" controller, and also a "complete_tasks" controller.
I have this in the complete_tasks_controller create action:
def create
#task = Task.find(params[:id])
#task.completed_at = Time.now
#task.save
end
I tried calling this:
<%=link_to "Complete task", new_task_complete_task_path(#task), :method => :post %>
..but I'm getting errors on that mentioning that "Only get, put, and delete requests are allowed."
Do you know what I'm doing wrong?
It would make more sense to move this into an action called complete in your controller:
def complete
#task = Task.find(params[:id])
#task.complete!
end
To access this action using RESTful routing you'll need to define a new member route like this in config/routes.rb:
map.resources :tasks, :member => { :complete => :put }
Adding :member => { :complete => :put } to the end of any pre-existing map.resources :tasks will do the trick also, you should only ever have one map.resources :tasks line, unless it's nested. The routing guide explains this better than I ever could.
To get to it from the view:
link_to "Complete this task", complete_task_path(#task), :method => :put
The method complete! would then be defined in your model like so:
def complete!
self.completed_at = Time.now
save!
end
The reason for this is that it puts the model logic where it belongs: in the model.
Each map.resources statement routes.rb creates a common RESTful routes for use with the specified resource. The appeal of REST is that is uses the request type and url to determine which action to take. Out of the four verbs associated with HTTP, each one has a specific use.
POST => Create
GET => Retrieve
PUT => Update
DELETE => Destroy
The reason you're getting an error about only get, put, and delete requests being allowed, is that you're using a post request. Essentially you're telling Rails you want to create a task with an id of one. However you cannot create an item that already exists. Which is why posts are not allowed. Instead you want to use put, because you're updating an existing record.
You can do it by changing post, to put in your link_to call.
<%=link_to "Complete task", new_task_complete_task_path(#task), :method => :put %>
Have a read through the routing guide and the resources documentation, it will help you understand the difference between HTTP requests, as well as provide some insight into how Rails handles those requests.

Resources