2015-05-04 58 views
0

我遇到了PATCH请求的问题。Rails使用PATCH请求渲染或redirect_to

我想PATCH "/activities/7/mans"update一些价值
但如果更新值无效,那么我怎么会重定向到:new

这里的铁轨服务器日志

Started PATCH "/activities/7/mans" for 127.0.0.1 at 2015-05-04 12:55:58 +0800 
Processing by MansController#new as HTML 
Parameters: {"utf8"=>"✓","authenticity_token"=>"Qnn/N9yWMtxAwA9N+br5r+mMvPdoY4fBJaI3sCYnObY=","activity_id"=>"7"} 

当我使用更新
我总是得到{GET} not {PATCH}

Started GET "/activities/7/mans" for 127.0.0.1 at 2015-05-04 12:58:56 +0800 
Processing by MansController#show as HTML 
Parameters: {"activity_id"=>"7"} 

这里后呈现新的route.rb设置

resources :activities do 
    resources :mans, :only => [:index ,:create] do 
    collection do 
     patch :new 
    end 
    end 
end 

这里是我的控制器操作

def new 
    @activity = Activity.find_by_id(params[:activity_id]) 
    @man= Man.new 
    end 

    def create 
    @man = Man.new(man_params) 
    if @man.age <20 
     redirect_to :new 
    end 
    end 
+1

你能告诉你的控制器行动?这很可能与您如何进行重定向有关。 – jphager2

回答

0

当有它的共同不是重定向,但与一些HTML的响应一次验证错误:

def create 
    @man = Man.new(man_params) 
    if @man.save # validation for age < 20 should be in the model 
     redirect_to my_redirect_path 
    else 
     render :new 
    end 
    end 

完全一样为update行动,你没有在这个问题提供:

def update 
    @man = Man.find(man_params[:id]) 
    if @man.update_attributes(man_params) # validation for age < 20 should be in the model 
     redirect_to my_redirect_path 
    else 
     render :edit 
    end 
    end 

这样你就可以打印使用@man.errors视图中的任何验证错误。

为了这个工作,你应该有edit(当然update)控制器动作,并修改路线如下(如果你不需要showdestroy动作):

resources :activities do 
    resources :mans, :except => [:show, :destroy] 
end