路由错误真的很简单,我错过了什么?

时间:2014-06-25 17:58:09

标签: ruby-on-rails routing

我觉得这是一个非常简单的错误,我只是没有看到......

请求控制器

def create
    if @request.save
        redirect_to 'success'
    else
        render 'new'
    end
end

def success
end

查看,requests/success.html.erb

<h1> Testing, this is the Success page that should be rendered </h1>

路线

get 'requests/new', to: 'requests#new', as: 'new_request'
post 'requests', to: 'requests#create' 
get 'requests/success', to: 'requests#success'

然而,现在,当我成功完成创建时,我不会渲染成功页面。相反,它似乎试图把我带到一个奇怪的地方。这是日志输出:

Redirected to http://localhost:3000success
Completed 302 Found in 10ms (ActiveRecord: 2.2ms)
[2014-06-25 10:49:24] ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000success (or bad hostname?)

请注意,如果在控制器中我将成功重定向更改为redirect_to root_path它确实有效......

2 个答案:

答案 0 :(得分:1)

将您的路线更改为

get 'requests/success', to: 'request#success', as: 'success'

然后在你的控制器内你可以

def create
  if @request.save
    redirect_to success_path
  else
    render 'new'
  end
end

同样在您的路线中,您有'requests#success''requests#create',您应将其更改为'request#success''request#create'以获取更多信息,请参阅here

答案 1 :(得分:1)

将您的控制器更改为此;

def create
    if @request.save
        redirect_to action: 'success'
    else
        render action: 'new'
    end
end

def success
end