lkahtz
lkahtz

Reputation: 4796

how to handle parameters in a raw submit form in rails 3.2.3

How can I handle a parameter passed in from a raw HTML form?

in /views/feeders/index.html.erb:

<form action="/feeders/extract/" method="post" accept-charset="utf-8">
    <input type="text" name="url" value="" id="url">    
    <p><input type="submit" value="submit"></p>
</form>

in /controllers/feeders_controller.rb

class FeedersController < ApplicationController
  def index
    @url=params[:url]
  end

  def show

  end

  def extract
    @url=params[:url]
    redirect_to :controller => 'feeders', :action => 'show'
  end
end

in routes.rb:

 root :to => 'feeders#index'
  match '/feeders/extract/:url' => 'feeders#extract', :via => :post
  match '/feeders/show/' => 'feeders#show'

I keep getting

Routing Error

No route matches [POST] "/feeders/extract"

Upvotes: 1

Views: 274

Answers (2)

Amol Pujari
Amol Pujari

Reputation: 2347

point #1

make your route as

post "feeders/extract" => "feeders#extract"

your route match '/feeders/extract/:url' => 'feeders#extract', :via => :post

will allow urls like

POST /feeders/extract/somevalue => params[:url] => 'somevalue'
POST /feeders/extract/123       => params[:url] => '123'

Try "rake routes" to know more

point #2: after you have followed #1 above

As I am trying to understand your needs from your comments, what you need is as below

in your routes.rb

post "feeders/extract" => "feeders#show"
match '/feeders/show/' => 'feeders#show'

and then perhaps you wont need extract action in your FeedersController.rb, show action would get called for extract call with params[:url]

point #3(alternative to #2): after you have followed #1 above

simply edit your controller as

render :show # redirect_to :controller => 'feeders', :action => 'show'

instead of redirect there

Hence you can go with either #1,#2 or #1,#3 as per your needs.

You should go with #1,#2 if "extract" action/process has more meaning there, I mean to end user.

Hope I understood your scenario :)

Upvotes: 1

Kelly
Kelly

Reputation: 81

The post request is trying to hit /feeders/extract/, but you don't have any routes that match that (the closest I see is /feeder/extract/:url).

Changing your route to match '/feeders/extract/' => 'feeders#extract', :via => :post should do the trick.

Once the request is routed correctly to the extract action, you will still have to params[:url].

Upvotes: 0

Related Questions