Defining a non-resourceful route in rails - ruby-on-rails

I want to setup a non-resourceful route in rails but I dont know how. Rails api says the structure has to be like this. post 'post/:id' => 'posts#create_comment' however, I'm not sure what I should exatly write.
I want it to post to the method "addbank" which is in the bankacctscontroller
I will be on the page localhost:3000/bankaccts/new
def addbank
if (params['customer_uri'])
current_user.customer_uri = (params['customer_uri'])
end
if current_user.save
redirect_to root_url, :notice => "bank account added"
else
render json: {error: "Payment account could not be configured properly"}, status: 401
end
end

There are many formats for defining custom routes. The most elaborate one is:
<METHOD> 'PATH' => 'Controller#Action', :as => path_helper_name (:as is optional)
So for your problem it would be :
post '/bankaccts/:id' => 'bankaccts#addbank'

If you use rails4.0,it will be written like this:
get "/bankaccts/new", to: "bankaccts#new", as: :new_post
I suggest you should learn rails routing first via the website "http://guides.rubyonrails.org/routing.html"

Related

Rails routing prob: on failing "create", re-renders the form (as it should) but not at the URL in my routes

RESTful resource, default type routes. Creating an event is supposed to work as follows:
def create
#event = current_user.events.build(params[:event])
if #event.save
redirect_to #event, :flash => { :success => "Event created!" }
else
render :action => "new" # new_event_path
end
end
When invalid data is entered, it does render the "new" view/form again, but it renders this view at the "localhost:3000/events" URL, where the "index" action/view should be on.
My event routes seem like they ought to be pretty predictable:
resources :events
I just updated to Capybara 2, began using DatabaseCleaner, and set transactional_fixtures to false in preparation for testing some JS-enabled functionality but can't think of any other way I might have stuffed this up.
Is there some simple thing I'm missing that could cause a weird routing muck up like this?
Ideas, anyone, on where to start troubleshooting it?
This is the correct behavior. What is happening is that it is using the POST method for that URL when issuing the create action. Using a GET at the URL would be the index action. Also note that rendering a different template does not change the URL (that would require a redirect).
Check out section 2.2 in the Rails Routing documentation:
http://guides.rubyonrails.org/routing.html
When you create event from submitting form you using post method to /events route. And when data becomes invalid rails render events/new for your /events(POST) request at /events address.
But you can
redirect_to action: "new", event: #event.attributes
and add to new action
#item = Item.new(params[:item].except('protected attributes','created_at', 'updated_at', 'id'))

Passing Params to a 301 redirect in rails 2.3

I'm stuck on a Rails 2.3 app trying to redirect some old URLs. I'm new to RoR and know 3+ way better, so I'm having trouble getting this done.
So: http://siteurl.com/foo/:foo_id goes to http://siteurl.com/foo/:foo_id/foo_items
I think I'm close because the end product goes to: http://siteurl.com/foo/foo_id/foo_items but doesn't pass the :foo_id params it actually writes it out!
Any help, or pointers are much appreciated.
In my routes.rb
map.connect '/foo/:foo_id',
:controller => 'redirect',
:action => :redirectoldfoo,
:monkey_tag => ':foo_id'
In my redirect controller:
def redirectoldfoo
redirect_to "/auctions/#{params[:monkey_tag]}/catalog_items"
end
You should not need the :monkey_tag at all, since :foo_id is all you're using anyway.
map.connect '/foo/:foo_id',
:controller => 'redirect',
:action => :redirectoldfoo
def redirectoldfoo
redirect_to "/auctions/#{params[:foo_id]}/catalog_items"
end
Note that in your example you indicated that you were trying to redirect to both /foo/:foo_id/foo_items and then /auctions/:foo_id/catalog_items. I assumed that the second one was closer to the real code, so I left that one in there.

How do I maintain the same controller & action in a page URL upon re-rendering an action in Rails?

I am using AuthLogic to authenticate users in my rails app. That part is set up and workign properly.
I have the following route defined:
map.login '/account/login', :controller => :user_sessions, :action => :new
Calling rake routes returns what I expect:
login /account/login {:controller=>"user_sessions", :action=>"new"}
When someone submits a login, it calls UserSessionsController.create:
def create
#user_session = UserSession.new(params[:user_session])
if #user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default account_url
else
render :action => :new
end
end
If #user_session.save fails, the appropriate error messages appear on the screen. However, the browser URL also changes to "http://localhost:3000/user_session" instead of staying on "http://localhost:3000/account/login".
I assume the problem is what I am feeding to the render method. What should I be feeding it?
This is actually the intended behavior for this process. In a standard scaffolded RESTful controller, a validation error in the create and update actions will simply render the original template without redirecting. This results in what you are seeing – the new template will be displayed with the create action's URL in the URL bar. The reason for this is that in order to display information to the user about what errors occurred, the view must have access to the invalid model object, which is #user_session in your case.
You can use redirect_to instead of render if you want to force a redirect to the original URL, but this will cause you to lose information about the errors. You would need to manually persist the errors in the session, which would be messy. My advice is not to worry about the fact that the URL doesn't match that of the original as this is pretty standard in all Rails apps.
Just adding solution for Rails 4 (based on Shaun's answer here):
Add new route to routes file:
post '/carts/new' => 'carts#create', as: :create_post
Add url: create_post_path to form tag
Done.
After further digging, I found the solution in another StackOverflow question: Use custom route upon model validation failure
I simply modified my routes to add a new one for posing to '/account/login':
map.login '/account/login', :controller => :user_sessions, :action => :new, :conditions => {:method => :get}
map.login_post '/account/login', :controller => :user_sessions, :action => :create, :conditions => {:method => :post}
Then, I updated my view to utilize the new route:
<% form_for #user_session, :url => login_post_path do |f| %>
This works perfectly. A failed login gives the appropriate error messages and maintains the '/account/login' URL.

Rails inherited resources usage

I'm using Inherited Resources for my Rails 2.3 web service app.
It's a great library which is part of Rails 3.
I'm trying to figure out the best practice for outputting the result.
class Api::ItemsController < InheritedResources::Base
respond_to :xml, :json
def create
#error = nil
#error = not_authorized if !#user
#error = not_enough_data("item") if params[:item].nil?
#item = Item.new(params[:item])
#item.user_id = #user.id
if !#item.save
#error = validation_error(#item.errors)
end
if !#error.nil?
respond_with(#error)
else
respond_with(#swarm)
end
end
end
It works well when the request is successful. However, when there's any error, I get a "Template is missing" error. #error is basically a hash of message and status, e.g. {:message => "Not authorized", :status => 401}. It seems respond_with only calls to_xml or to_json with the particular model the controller is associated with.
What is an elegant way to handle this?
I want to avoid creating a template file for each action and each format (create.xml.erb and create.json.erb in this case)
Basically I want:
/create.json [POST] => {"name": "my name", "id":1} # when successful
/create.json [POST] => {"message" => "Not authorized", "status" => 401} # when not authorized
Thanks in advance.
Few things before we start:
First off. This is Ruby. You know there's an unless command. You can stop doing if !
Also, you don't have to do the double negative of if !*.nil? – Do if *.present?
You do not need to initiate a variable by making it nil. Unless you are setting it in a before_chain, which you would just be overwriting it in future calls anyway.
What you will want to do is use the render :json method. Check the API but it looks something like this:
render :json => { :success => true, :user => #user.to_json(:only => [:name]) }
authorization should be implemented as callback (before_filter), and rest of code should be removed and used as inherited. Only output should be parametrized.Too many custom code here...

Ruby on Rails Scaffold - Modify Show Method

I am a beginner when it comes to Ruby on Rails, so I need a little bit of help. I started reading a basic tutorial recently, which was taught using Scaffolding. I made a "Clients" model: script/generate scaffold clients name:string ip_address:string speed:integer ... Inside the clients_controller.rb file, there is a method called show:
# GET /clients/1
# GET /clients/1.xml
def show
#client = Client.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #client }
end
end
For queries, I'd go to localhost:3000/clients/{Enter the ID here}. Instead of searching with the ID are the argument, I'd like to search with another value, like ip_address or speed, so I thought all I would have to do is change :id to :ip_address in "#client = Client.find(params[:id])". However, that does not work, so would someone please tell me how I can achieve a search with another parameter. Thanks!
This doesn't work because of the way things are routed
When you do something like
map.resources :client (See config/routes.rb)
This happens automatically when you use scaffold.
It sets up routes based on the assumption you're using an id.
One of these routes is something like
map.connect 'clients/:id', :controller => 'client', :action => 'show'
So :id is passed as a parameter as part of the URL.
You shouldn't have the IP be the primary identifier unless they're distinct - and even then it kind of messes with the RESTful routing.
If you want to have the ability to search by IP, modify your index action for the clients
def index
if params[:ip].present?
#clients = Client.find_by_ip_address(params[:ip]);
else
#clients = Client.all
end
end
Then you can search by ip by going to clients?ip=###.###.###
This line in your routes.rb file
map.connect 'clients/:id', :controller => 'client', :action => 'show'
implies that when the dispatcher receives an URI in the format "clients/abcdxyz' with GET Method, it will redirect it to show method with the value "abcdxyz" available in params hash with key :id.
EDIT
Since you have used scaffold, the clients resource will be RESTful. That means that when you send a GET request to "/clients/:id' URI, you will be redirected to show page of that particular client.
In your controller code you can access it as
params[:id] # which will be "abcdxyz"
The find method that is generated by scaffold searches on the primary key i.e 'id' column. You need to change that statement to either
#client = Client.find_by_ip_address(params[:id]) #find_by_column_name
OR
#client = Client.find(:first, :conditions => [":ip_address = ?", params[:id]])
:-)

Resources