pseudo-complex routes in rails 3 - ruby-on-rails

I'm trying to make some nice simple routes in Rails 3 with 2 custom matchers which assume the root :id will be a city and :city_id/:id will be a place... it seems to work fine except when trying to edit.
Ie.
root_url/countries/france
root_url/paris/some_place
root_url/paris
Here's my code to be more precise.
resources :countries do
resources :cities
end
resources :cities do
resources :places, :reviews
end
match ':id' => 'cities#show', :as => :city, :method => :get
match ':city_id/:id' => 'places#show', :as => :city_place, :method => :get
That seems to work perfectly accept when I try to edit records. The html is as below:
<% form_for #city do |f| %>
<% end %>
Produces:
<form accept-charset="UTF-8" action="/kiev" class="edit_city" id="edit_city_3581" method="post">
Which would only work if it were:
<form accept-charset="UTF-8" action="/cities/kiev" class="edit_city" id="edit_city_3581" method="post">
I know I could simply provide a more advanced form_for route explicitly to get around this, but I'm wondering if there's something better to do in my routes.rb to make my life easier rather than patching?
Thanks

How about if you renamed your custom routes like this and let normal routes handle edit etc.
get ':id' => 'cities#show', :as => :city_shortcut
get ':city_id/:id' => 'places#show', :as => :city_place_shortcut

Related

Routing in Rails forms

I've used a form successfully in embedded Ruby that looks like this:
<%= form_for :comment, url: user_comments_path do |c| %>
<%= c.text_field :location %>
<%= c.text_field :title %>
<%= c.text_field :body %>
<%= c.submit ("Submit") %>
<% end %>
However when I tried to put this into Bootstrap I came up against problems. I then tried to make a simpler HTML form like this:
<form accept-charset="UTF-8" action="user/comments" method="post">
<input id="comment_location" name="comment[location]" type="text"/>
<input id="comment_title" name="comment[title]" type="text"/>
<input id="comment_body" name="comment[body]" type="text"/>
<input name="commit" type="submit" value="Create"/>
</form>
...but I came up against the same error message: No route matches [POST] "/users/1/comments/new/user/comments". I am using nested resources, and Rails routes in the terminal tells me that the user_comments path 'POST' equates to user/user_id/comments and the Create controller action.
In my HTML form if I enter action="user/comments" then the resulting path is "/users/1/comments/new/user/comments", if I enter action="comments" the path is "/comments", and if I enter "/" the path is "/". The path that I'm trying to reach is "user/comments" but there seems to be no way there through my HTML form.
I've racked my brains but I can't understand why this might be. Any help would be much appreciated! Thanks
ps. Routes look like this:
Rails.application.routes.draw do
get 'sessions/new'
get 'welcome/index'
root 'welcome#index'
get 'user' => 'users#show'
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :users do
resources :comments
end
resources :sessions
end
update routes.rb use collection instead of resources to get /users/comments like path
resources :users do
collection do
get 'comments'
end
end

Undefined method with "_path" while using rails form_for

I'm running into a (I think) routing error while using the Rails form_for helper. I have been searching around and looked at this question, but the plural for "static_event" with pluralize is "static_events" so I am at a loss. Any help would be apprecited. Here are the details....
ActionView::Template::Error (undefined method `static_events_path' for #<#<Class:0x007f9fcc48a918>:0x007f9fcc46fa78>):
My Model:
class StaticEvent < ActiveRecord::Base
attr_accessible :content, :title, :discount, :location, :day_of_week, :start_time
My Controller:
class StaticEventsController < ApplicationController
before_filter :authenticate, :only => [:create, :destroy]
before_filter :authorized_user, :only => [:destroy]
def new
#title = "Share An Event"
#static_event = StaticEvent.new
end
def create
#static_event = current_user.static_events.build(params[:event])
if #static_event.save
flash[:success] = "Event Shared"
redirect_to #static_event #this was the old version
else
render :new
end
end
The route:
match '/static-events/new', :to => 'static_events#new'
match '/static-events/', :to => 'static_events#index'
match '/static-events/:id', :to => 'static_events#show'
The view
<%= form_for (#static_event) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= text_field "static_event", "title", "size" => 48 %>
<%= time_select "static_event", "start_time", {:ampm => true, :minute_step => 15} %>
<%= text_area "static_event", "content", "cols" => 42, "rows" => 5 %>
<%= text_field "static_event", "discount", "size" => 48 %>
<%= text_field "static_event", "location", "size" => 48 %>
<%= text_field "static_event", "day_of_week", "size" => 48 %>
<input name="" type="submit" class="button" value="share on chalkboard" />
<% end %>
Only routes created using the resources method are automatically named.
If you want to name your routes, use the :as option:
match '/static-events/new', :to => 'static_events#new', :as => :new_static_event
match '/static-events/', :to => 'static_events#index', :as => :static_events
match '/static-events/:id', :to => 'static_events#show', :as => :static_event
However, it's better to use the resources method. You must pass the "true" name of your model as the first parameter, then override the path if you want:
resources :static_events, :path => 'static-events'
First of all, you should define your routes this way:
resources 'static-events', :only => [:new, :create]
This will create a route for new and create methods.
Because when you use a new ActiveRecord object as an argument to form for, it will looks for *s_path like static_events_path in your routes file with the POST verb.
I think the way you have defined your routes doesn't create the static_events_path with POST verb (you can check that by using rake routes as megas said). So don't use match anymore, use resources or get/post/... instead of match in your Rails 3 projects.
EDIT
I did not notice yesterday, but there is no route for create method. Add the route below before static_events#index or remove all your routes and do like I said above.
post '/static-events/', :to => 'static_events#create'
Run rake routes and you'll see the list of your routes. Then you can fix the route file to have appropriate route path.
This happened to me when i was using a nested resource, but forgot to actually initialize the parent resource using load_and_authorize_resource in cancan. Therefore, the parent resource was null and it threw this error.
I fixed it by declaring load_and_authorize_resource on the parent in the controller.

How to custom edit_user_path with nice URL

I'm adding a nav menu in application.html.erb which navigate users in authlogic. All routers like: :logout, :register and :login seems working with custom paths.
match 'account/login' => 'user_sessions#new', :as => :login
match 'account/logout' => 'user_sessions#destroy', :as => :logout
match 'register' => 'users#new', :as => :register
Here is nav menu:
<% if current_user %>
<%= link_to "Edit Profile", edit_user_path(current_user.id)%> <%=h current_user.firstname %>
<%= link_to "Logout", :logout %>
<% else %>
<%= link_to "Register", :register %> |
<%= link_to "Login", :login %>
<% end %>
But edit_user_path is transferring me to /users/:id/edit. How do I make the nice URL for this path. I would like to make it /account/edit. It also need to disable this path /users/:id/edit to prevent user from requesting another user ID which doesnt belong to him/her. It should throw a 404 page is perfect.
Current paths in the nav menu:
Logout: /account/logout
Login: /account/login
Register: /register
I would love to have another path for edit_user_path:
Edit profile: /account/edit
Is there any way I can simply use edit_user_path(current_user.id) and the path automatically transfers me to /account/edit and disable id request.
Any help would be much appreciated! Thanks!
[Updated] This is my /config/router.rb
Appcatous::Application.routes.draw do
resources :users, :user_sessions
match "account" => "users#show", :as => :account
match 'account/login' => 'user_sessions#new', :as => :login
match 'account/logout' => 'user_sessions#destroy', :as => :logout
end
users.rb model is very simple:
class User < ActiveRecord::Base
acts_as_authentic
validates :firstname, :presence => true
validates :lastname, :presence => true
end
It would be helpful if you included all the routes in question - which means also the routes for the users. Did you create those with:
resources :users
In case you did, you'll have to disable that command and recreate just the routes you need manually with (example!):
match 'account/edit' => 'users#edit', :as => :edit_user
or whatever your controller is named. That way you'll get your "/account/edit" - however, you'll also have no way to transfer the user id that way. That one you'll have to transfer by other means, like through a variable stored in a cookie or a session.
The latter method is the usual way of remembering user authentification anyway.

ROR link_to method problem

I'm trying to pass some values through a link and I want them to be invisible. These are the options I've tried:
<%= link_to 'Add comment', :controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
<%= link_to 'Add comment',{ :controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id}, :method => :post %>
<%= link_to 'Add comment', :controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id, %>
<%= link_to 'Add comment', new_comment_path, :idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
First option - treats method as a parameter:
http://localhost:2000/comments/new?idea_id=1&method=post&user_id=1
Second option - goes like this: http://localhost:2000/comments/new?idea_id=1&user_id=1
and also causes routing error: "Routing Error No route matches "/comments/new"
Third option - loads the form, but of course is like: http://localhost:2000/comments/new?idea_id=1&user_id=1
Fourth option - looks good (http://localhost:2000/comments/new) but the same routing error like the second one.
What am I doing wrong?
Thanks in advance.
PS
I was asked to give my routes, so here they are:
resources :rights
resources :comments
resources :ideas
resources :users
resources :sessions, :only => [:new, :create, :destroy]
root :to => 'main#home'
#match '/comments/new' => "comments#new" # this doesn't help
match '/home', :to => 'main#home'
match '/contact', :to => 'main#contact'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/signup', :to => 'users#new'
If you have RESTful routes
<%= link_to 'Add comment', new_comment_path,
:idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
should be
<%= link_to 'Add comment', comments_path,
:idea_id => #idea.id, :user_id => #idea.user.id, :method => :post %>
As others have said, it sounds like you have a problem in your routes file. Either check if you have the resource :comments defined or post your routes file here and we'll help you out. It's possible that it's not working because you are trying to POST...
If you want 'invisible' variables (I assume you mean that you do not want the variables to appear in the URL), you'll have to POST to the page rather than just link to it. In this case, your second example is the best bet. It goes against convention to POST to /new so that could be what is causing the 'no routes' error if you're using resource :comments
Give this a try in your routes.rb:
match '/comments/new' => "comments#new"
Give that a try, it should load the correct page and give you access to the variables you passed through to it through params.
Please note, that this goes wildly against convention. Is there a reason why you don't want those variables to appear in the URL? It's likely there's a better way of doing what you're thinking and if you explain we can advise you as best as we can.
Have you properly defined the routes?
Can you show how they are?
You should have something like this for that to work: resource :comments
Also, in general /new works with a GET, and a POST is sent when creating...
The method is part of html_options, you need to separate the two hashes like so:
<%= link_to 'Add comment', {:controller => :comments, :action => :new, :idea_id => #idea.id, :user_id => #idea.user.id}, :method => :post %>

Problem with link_to_remote in rails

I'm having a consistency problem using link_to_remote in rails.
I have 2 use cases of link_to_remote, and they generate different ajax. I can't figure out why, and it is driving me crazy.
Here is use case one...
<%= link_to_remote "section-", :update => "sections", :url => {:action => :destroy, :controller => "sections", :id => #section.id } %>
This generates the appropriate ajax (as below) and works as I expect. Note that it picks up the :action param from the call and inserts it correctly in the ajax.
section-
I also have another instance where I use link_to_remote, but it generates incorrect ajax. The use case is nearly identical, except the controller is different. Either way, I wouldn't expect that to result in different ajax.
The call...
<%= link_to_remote "question-", :update =>"questions-1", :url => {:action => :destroy, :controller => "questions", :id => #question.id} %>
The resulting ajax...
question-
The obvious difference here is in the second arg to Ajax.Updater. The :action param is missing from that path. Why? This results in broken code for me, but I can't understand why this is happening. The link_to_remote calls are nearly identical.
Please point me in the right direction. Thanks.
Below is my routes.rb file...
ActionController::Routing::Routes.draw do |map|
map.resources :questions, :has_one => :section, :collection => { :sort => :post }
map.resources :sections, :has_many => :questions, :has_one => :form, :collection => { :sort => :post }
map.resources :forms, :has_many => :sections
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
map.root :controller => "forms"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
What do you get if you copy this into your view?
<%= url_for :action => :destroy, :controller => :questions, :id => #question.id %>
I suspect the problem's not really with link_to_remote, but with routing. Once url_for is returning the URL you expect, then try link_to_remote again.
Simply adding a :method => :delete to your link_to_remote call may be the simplest fix for you:
<%= link_to_remote "question-", :update =>"questions-1", :url => {:action => :destroy, :controller => "questions", :id => #question.id}, :method => :delete %>
This should force the call to /questions/:id to use the HTTP DELETE method. If you want the above call to generate the url /questions/destroy/:id instead I believe you would need a manual change to your routes, as the default map.resources doesn't seem to be achieving that result.

Resources