User /settings route? - ruby-on-rails-3

Simple question:
I have added the following to my routes. rb
match 'settings' => 'users#edit'
I'm trying to make a user settings page at /settings (just with the edit form and the update button).
I have created the edit/update methods in my User controller, and added the edit view and _form.
For some reason when I go to /settings I am getting:
No route matches {:action=>"show", :controller=>"users"}
(Even though users#show exists)
Strangely, I can get it to work fine to route to users#show if I change my routes.rb - but that's not what I want!
I guess I'm missing something obvious - can someone point me in the right direction?

Does this issue go away when you restart your dev server? Routes are funny when created after the server is running.

Turns out commenting out my link_to fixed it..

Related

Creating an action inside a controller, after it has been generated

I am working on a rails app, and have generated a Controller via
rails g controller Pics index upload
but now I would like to add another action
delete
do I do this by manually adding a delete method in the Pics controller?
Or do I need to run another generation. My concern is that by adding manually something may not get included (not sure what it would be, but something under the hood.)
Is this the best way of adding a new action to a generated controller?
If you add manually, just make sure you have the right route on your routes.rb.
Let's say you create your delete action inside your Pics controller.
def delete
# do stuff
end
On your routes.rb, you need to append the route to your resource like this, remembering to analyse if it is a resource that acts upon a member of your resource, or a collection. (More about this you can read on this guide http://guides.rubyonrails.org/routing.html#adding-more-restful-actions).
resource :pics do
collection do
post :delete
end
end
Or
resource :pics do
member do
post :delete
end
end
Remember that all RESTFUL actions are handled by default by the rails router, again, try to read the guide i showed earlier for precise information about the topic. Hope it helps.

rails routes gives an error - wrong controller seems to be called

I can't figure out what's wrong with my routes. I'm very new to rails (and programming) so it could be something very simple.
Clicking on my home page gives the following error -
Routing Error
No route matches {:action=>"show", :controller=>"bets"}
Try running rake routes for more information on available routes.
This is my routes.rb
root :to => 'pages#home'
resources :bets
resources :bettingevents
get '/bet' => 'bets#index'
get '/bettingevent' => 'bettingevents#index'
The show function in my bets controller has
def show
redirect_to(bets_path)
end
localhost:3000/bets is fine, with create, update, delete, edit routes working correctly. My pages controller has been working perfectly till I added the bets routes.
If i remove resources :bets the homepage works all right, but none of the bets routes work.
What am I doing wrong? Thanks

Yii-User and Yii-eauth integration

I am trying to put together an application using yii-user and yii-eauth extensions but I am coming up short. When I create a new webapp and install eauth I can get it to work fine so I know that I am not doing anything wrong on that end. I think the problem lies with my urls. This is a demo of what it is supposed to be like: http://nodge.ru/yii-eauth/demo/login. When someone clicks on say google it should bring you to the google sign in page but on my application I am getting a 404 error that states "The system is unable to find the requested action "login"." The url for this is user/user/login/service/google_oauth whereas the url should read user/login/service/google_oauth. Upon typing this into the browser manually I am redirected to the right page.
So I took a look into the EAuthWidget.php class to see if I could find out where it was getting that extra "user" from but I could not figure it out. I think it may have something to do with the fact that I have this in the user module which is in the /modules/user directory in my webapp. I also tried to use the URLManager to point to the right address but with no luck.
Does anyone have any similar experiences setting this up? Or any suggestions?
You just need to change the widget initialization code in your view(namely change the action property of the widget), somewhat like this:
<h2>Do you already have an account on one of these sites? Click the logo to log in with it here:</h2>
<?php
$this->widget('ext.eauth.EAuthWidget', array('action' => 'login'));
?>
Just to add, keep in mind that this action depends on which view you are including this widget, if the view is protected/views/site/login.php (which is yii's default site login view) then action should be able to go to the yii-user module's login action, so it'll be : 'action'=>'/user/login' , however if you are including this widget in yii-user's protected/modules/user/views/user/login.php then the action will be 'login' as already mentioned.

rails 3 No route matches error

I am newbe to rails3
I have 20 controllers in my rails app..redirecting from one controller to another one my rails app gives No Route matches
should I add resources :controllername entry for each controller in routes.rb file?
or any other general method to do this?
When you run rake:routes is your route there? If it is not you'll get 'No route matches'. It is much better to use your routes.rb config then to continuously redirect from one controller to another

Rails 3 - Routing to a user profile

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