ROR link_to method problem - ruby-on-rails

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 %>

Related

Ruby on Rails: Linking issue between pages

I'm fairly new to Ruby and Rails and this has been an issue I can't resolve on my own:
I'm working on an existing website, running on an older version of Rails and Ruby 1.9.3.
I'm trying to change the details for a certain user, but sometimes in the linking I don't get http://127.0.0.1:3000/users/firstname.lastname but http://127.0.0.1:3000/users?id=firstname.lastname.
This is the code for the links I have:
<li><%= link_to "Remove this user", user_path(#person[:uid]), :confirm => 'Are you sure you want to delete ' + #person[:uid] + '?',:method => :delete ,:title => "Delete user from LDAP" %></li>
<li><%= link_to "Edit this user", edit_userdetails_path(id:#username[:UserID]) %></li>
<li><%= link_to "Add to group", group_path %></li>
<li><%= link_to "Change password", change_pwd_path(#person[:uid]) %></li>
The first and 3rd link work like they should, the 2nd and 4th link have the behavior as described with the ?id= in the url.
This is what's in my routes file:
resources :posts
resources :timesheets
resources :personal_bugs, :only => [:new, :create, :destroy]
resources :sessions, :only => [:new, :create, :destroy]
resources :passwords, :only => [:new, :create]
resources :users, :constraints => { :id => /[0-9A-Za-z\-\.]+/ }
resources :computers
resources :userdetails, :constraints => { :id => /[0-9A-Za-z\-\.]+/ }
resources :bz2refs, :only => [:index, :update]
resources :bz2ref_activities
resources :sessions do
collection do
get :reset_password
get :recovery
end
end
resources :bz2ref_resources do
collection do
get :remove
get :restore
end
end
get "userdetails" => "userdetails#index", :as => "userdetails"
get "edit_userdetails" => "userdetails#edit", :as => "edit_userdetails"
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "change_pwd" => "passwords#new", :as => "change_pwd"
post "search" => "pages#search", :as => "search"
get "search" => "pages#search", :as => "search"
get "help" => "pages#help", :as => "help"
get "admin_urls" => "pages#admin_urls", :as => "admin_urls"
get "report" => "timesheets#report", :as => "report"
post "report" => "timesheets#report", :as => "report"
get "incomplete" => "timesheets#incomplete", :as => "incomplete"
post "incomplete" => "timesheets#incomplete", :as => "incomplete"
get "check" => "timesheets#check", :as => "check"
post "check" => "timesheets#check", :as => "check"
get "export" => "timesheets#export", :as => "export"
get "group" => "users#group", :as => "group"
get "group_show" => "users#group_show", :as => "group_show"
post "group_add" => "users#group_add", :as => "group_add"
get "computer_wake" => "computers#wake", :as => "computer_wake"
get "mirror_users" => "users#mirror", :as => "mirror_users"
get "kiosk" => "posts#latest", :as=> "kiosk"
get "lookupbug" => "timesheets#lookupbug", :as =>"lookupbug"
I call upon userdetails controller from users and then after editing I want to go back from userdetails --> users.
That is because you give an id parameter in the path.
So for example:
user_path(#person[:uid]) => localhost/users/firstname.lastname
user_path(id: #person[:uid]) => localhost/users?id=firstname.lastname
To fix the second link you need to change from edit_userdetails_path(id:#username[:UserID]) to edit_userdetails_path(#username[:UserID]), without the id.
I solved the issue by creating the hyperlink this way:
<li><%= link_to "Edit this user", controller: "userdetails", action: "edit", id: #username[:UserID] %></li>
This way it works!

Rails form_tag post not working

My users_controller has these methods
def follow_code
#user = current_user
end
def followsubmit
redirect_to root_path
end
My route file has
match "follow_code" => "users#follow_code", :as => "follow_code"
match "follow_code" => 'users#followsubmit', :as => "follow_code", :via => 'post'
My follow_code.html.erb view has
<%= form_tag(follow_code_path, :method => 'post') do %>
<%= submit_tag("Submit") %>
<% end %>
Yet for some reason when I click submit on my view I am never redirected to my root_path and instead the follow_code view is re-rendered.
What am I doing wrong? Thanks.
I'm also curious about this. I ran into it today, using match as well, and my solution was to rename the post action:
match "follow_code" => "users#follow_code", :as => "follow_code"
match "save_follow_code" => 'users#followsubmit', :as => "save_follow_code", :via => 'post'
However, I was using the condition attribute to specify the method. In your case, you may just need to specify the first one as a get.
match "follow_code" => "users#follow_code", :as => "follow_code", :via => 'get'
match "follow_code" => 'users#followsubmit', :as => "follow_code", :via => 'post'

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.

Ruby routes and link_to, custom :action route problem

I'm trying to get this link:
<%= link_to('Edit', :action => 'manage', :id => user) %>
even tried explicitly <%= link_to('Edit', {:controller => 'users', :action => 'manage', :id => user}, :method => :get) %>
to show the link in HTML as
'/users/manage/1' or '/users/1/manage'
but it shows up as
'/users/manage?id=1'
I can see in my routes:
manage_users GET /users/manage(.:format) {:action=>"manage", :controller=>"users"}
...
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
so I added a map.connect, but it was added to users
users GET /users/manage/:id(.:format) {:action=>"manage", :controller=>"users"}
but without any success. The link is still '/users/manage?id=1'
This doesn't work anymore than the above.
GET /users/:id/manage(.:format) {:action=>"manage", :controller=>"users"}
Now, when I put the :action in link_to, to 'edit' i.e.
<%= link_to('Edit', :action => 'edit', :id => user) %>
The routes.rb edit_user GET /users/:id/edit/(.:format) works, with a link showing up of
'/users/1/edit'
How do I get my link_to to show the same link when it is 'manage' instead of 'edit', i.e. showing a link of 'users/1/manage' instead of '/users/manage?id=1'??? Is it because my above map.connect is being added to users, when it should be added to 'manage_users'?
Thank for the help. I'll be trying to figure it out.
Peace.
BTW, /users/manage?id=1 works, I just want the proper rewrite link to click on.
EDIT routes.rb
map.resources :users, :collection => {:manage => :get}
#map.manage_user '/users/:id/manage', :controller => :users, :action => :manage
#map.resources :users, :member => { :manage => :get }
#map.connect 'users/manage/:id(.:format)', :controller => 'users', :action => 'manage', :conditions => { :method => :get }
map.resources :categories
map.resources :posts
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
so I added a map.connect, but it was added to users
I suspect you added map.connect after other definitions, which would give it lowest priority. Try putting it in the beginning of routes.rb file.
You can also use named routes to avoid confusion:
map.manage_user '/users/:id/manage', :controller => :users, :action => :manage
and then refer it as
link_to 'Manage', manage_user_path(:id => user)
edit
If that doesn't work, please show your routes.rb file.
You should change collection to member in your routes.rb when defining map.resources :users. Then you'll get nice /users/1/manage link.
Also, when creating a link try this:
<%= link_to 'Manage', manage_user_path(user) %>

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