Confused about Ruby on Rails REST Routing - ruby-on-rails

I am really confused about Ruby on Rails REST routing. Even though I have specified that after the success it should go to the confirm action it goes to the show action and pass the ID=confirm.
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
format.html { redirect_to :action => "confirm" }
else
format.html { render :action => "new" }
end
end
end
The error I get is the following:
ActiveRecord::RecordNotFound in ArticlesController#show
Couldn't find Article with ID=confirm
Rails.root: /Projects/highoncoding
Application Trace | Framework Trace | Full Trace
app/controllers/articles_controller.rb:31:in `show'
UPDATE 1:
Here is my Route.rb file:
resources :articles
get "articles/confirm"

# config/routes.rb
MyApp::Application.routes.draw do
resources :articles do
member do
get 'confirm'
end
end
end
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
# use a named route here
format.html { redirect_to confirm_article_url(#article) }
else
format.html { render :action => "new" }
end
end
end
end

you'll need to add the route so it looks like
match 'articles/confirm/', :controller => 'article', :action => 'confirm'
resources :articles
you need to have the :id in there or it will think that confirm is an id which is why you are seeing the error ID=confirm. make sure also that this is the first route. (at least before the resources for the articles controller.

You should probably add the confirm route directly in your routes file.
match 'articles/confirm' => 'articles#confirm'
resources only work for create/update/destroy/etc.

Related

Rails issue with "route matching"

I'm getting the following error when loading this URL: localhost:3000/groups/5/post/new
No route matches [GET] "/groups/5/post/new"
I am trying to create a new "post" for a specific group. Here is my Post controllers "new" action:
def new
#group = Group.find(params[:group_id])
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #post }
end
end
I have my routes orginized as so:
resources :groups do
resources :posts do
resources :comments
end
end
Does anyone see anything that may be causing this?
Thank you.
localhost:3000/groups/5/post/new
Should be
localhost:3000/groups/5/posts/new
Using that nested resource, shouldn't the path be?: /groups/5/posts/new (note the plural posts)

send errors to "/login/errors" when logging fails instead of going to the path "/user_sessions"

I am building an app with ruby on rails 3.1.
I have a login form correctly displayed at the url "/login". When an error occurs, I would like it to go to "login/errors" instead of "/user_sessions".
For information I am using authlogic
The model used is called "user_session".
in route.rb:
resources :user_sessions, :only => [:create, :destroy]
match 'login' => 'user_sessions#new'
root :to => redirect("/login")
in user_sessions_controller.rb:
def new
#user_session = UserSession.new
respond_to do |format|
format.html # new.html.erb
end
end
def create
#user_session = UserSession.new(params[:user_session])
respond_to do |format|
if #user_session.save
user = User.first(:conditions => {:email=> #user_session.email})
format.html { redirect_to :controller => 'teams', :action => 'show', :id => user.team_id }
else
format.html { render :action => "new" }
end
end
end
I have tried different things without success.
Thanks for your help.
simple solution:
match 'login' => 'user_sessions#new', as: :login_page
redirect_to login_page_path if saving was fail.

Rails 3.1/Mongoid - How to find model by name rather than id in routes

Edit: i am using Mongoid/MongoDB for my database, meaning I don't get the normal Active Record tools I think.
I have a simple Rails 3.1 app with a model Page. I would like to match '/:customURL' to the Page#show action for the Page with the relevant :customURL. How should I change the controller and routes? Keep in mind that there are a few routes from '/SOMETHING' that I want to keep. For instance '/pages' should still go to my Page#index action not try to find a page with customURL of 'pages'.
current controller:
def show
#page = Page.find(params[:id])
#title = #page.title
respond_to do |format|
format.html # show.html.erb
format.json { render json: #page }
end
end
routes:
resources :pages do
resources :feeds
end
get "pages/index"
get "pages/show"
get "pages/new"
root :to => "pages#index"
Thanks a million.
Assuming that your Page has a customURL attribute from its database table. In your controller:
def show
#page = Page.first(:conditions => {:customURL => params[:customURL]})
#title = #page.title
respond_to do |format|
format.html # show.html.erb
format.json { render json: #page }
end
end
In your routes
resources :pages, :except => :show do
resources :feeds
end
# Anything you want to match before the custom URLs needs to go above the next route definition
get "/:customURL" => "pages#show"
root :to => "pages#index"

Rails 3 nested resources route error

I'm not being able to redirect the user to the page I want after he creates a new "service" resource.
Here's the routes.rb:
resources :wsps do
resources :services
end
The html form:
<%= form_for([#wsp,#service]) do |f| %>
Services_controller.rb:
def new
#wsp = current_wsp
#service = #wsp.services.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #service }
end
end
def create
#wsp = current_wsp
#service = #wsp.services.build(params[:service])
if #service.save
redirect_to wsp_service_path
end
end
The wsp_service_path goes to /wsps/1/services and the error:
No route matches {:action=>"destroy", :controller=>"services"}
What am I doing wrong? Why cant't I use "wsp_service_path"?
Thank you.
You can use wsp_service_path (while you should be use wsp_service_url since you are in a controller). All you are missing is arguments. wsp_service_path (or _url) are going to expect two arguments: a wsp and a service. Once you provide those two, it works.
redirect_to wsp_service_url(#wsp, #service)

Routing to new action (Rails)

I have an action in my PostsController named 'tagged', which I want to return all posts tagged with whatever term.
In my routes.rb I have the following (at the top):
map.connect 'posts/tagged/:tag', { :controller => 'posts', :action => 'tagged', :tag => /[a-z\-]+/ }
Yet navigating to posts/tagged/yes returns a RecordNotFound error:
Couldn't find Post without an ID
In my tagged.html.erb file, I'll eventually be using the find_tagged_with method from acts_as_taggable_on_steroids, but for now I've put a simple Post.find(:all) to eliminate the possibility of error.
It seems like my map.connect is being overridden, and the same error occurs even if I comment the whole of the routes.rb file out except my new line.
Ok, because you can comment out the default routes that means your problem is not in your routes at all. It's that your tagged action in the posts controller probably has something like this.
def tagged
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
Or perhaps if you spent a little more time it looks like this:
def tagged
#post = Post.find(params[:tagged])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
Where as what you want is this:
def tagged
#post = Post.find(:all, :conditions => {:tagged => params[:tagged]})
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
Anyway, you should be writing functional tests for this stuff and not testing in the browser.
Why not add a RESTful route for the "tagged" action?
map.resources :posts, :member => { :tagged => :put }

Resources