我在Rails中的方法调用后重定向时遇到问题

时间:2015-04-25 20:32:20

标签: ruby-on-rails devise

我终于让我的自定义def方法工作并更新我的数据库中的表,但知道我有问题重定向回我的帐户索引页面。它只会转到我的update.html.erb页面。问题的来源可能在我的路线和/或cancancan / devise的验证问题?也许我的before_filter:authenticate_user!但当我改为redirect_to accounts_url时,我得到了这个,   在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,每次操作最多一次。另请注意,重定向和呈现都不会终止执行操作,因此如果要在重定向后退出操作,则需要执行类似“redirect_to(...)并返回”的操作。

我的控制器,顶部的声明和我的更新方法

 class AccountsController < ApplicationController
 before_action :set_account, only: [:show, :edit, :deposit, 
 :withdrawl,:update, :destroy]
  # before_filter :set_account, only: [:show, :edit, :deposit, :withdrawl, 
 :update, :destroy]
  before_filter :authenticate_user!
  respond_to :js, :html
  #respond_to :html

  def update
    @account = Account.find(params[:id]) 
    @account.update(account_params)
    respond_with(@account)
    return accounts_url 
end

我的路线

   Rails.application.routes.draw do


  # get 'accounts/update'

  #get 'accounts/new'

  # get 'accounts/create'

  # get 'accounts/edit'

  # get 'accounts/destroy'

  # get 'accounts/index'

  # get 'accounts/show'

  # get 'accounts/deposit'

  # get 'accounts/withdrawl'

  #get 'account/new'

  # get 'account/create'

  # get 'account/edit'

  # get 'account/destroy'

  # get 'account/index'

  #  get 'account/show'

  #  get 'account/deposit'

  # get 'account/withdrawl'

  devise_for :users
  get 'admin' => 'admin#index'
  get 'users/index'
  get 'accounts/index'
  get 'accounts/show'

  #get 'accounts/show'


  # resources :accounts

 resources :students

 #controller :sessions do
 #  get 'login' => :new
 #  post 'login' => :create
 #  delete 'logout' => :destroy
  #end

 root 'store#index', as: 'store'


  #get 'sessions/create'

   #get 'sessions/destroy'

   #resources :users

   resources :orders

   resources :line_items

   resources :carts

   get 'store/index'

   resources :menus
   resources :users

   resources :accounts do
     collection do
      post 'deposit', :action => :deposit
      post 'withdrawl', :action => :withdrawl
    end
   end
   # The priority is based upon order of creation: first created -> highest
    priority.
   end

3 个答案:

答案 0 :(得分:2)

您应该将return accounts_url更改为redirect_to accounts_url

这样您就可以设置响应。行动的回归并不重要。

答案 1 :(得分:0)

更改update方法,

return accounts_url 

redirect_to accounts_path 

并删除respond_with(@account)行。

答案 2 :(得分:0)

您需要移除对respond_with的调用:该方法已在进行重定向(对于您的模型的show动作)

然后用

替换你的return语句
redirect_to accounts_path

虽然返回当然会停止进一步执行操作,但不使用返回值。