redirect_to导致与请求不同的操作

时间:2013-08-05 13:20:42

标签: ruby-on-rails

我是Ruby on Rails的新手,这是我在stackoverflow上的第一篇文章。

我有这个页面,用户可以选择他想要的功能。由于他选择了这些特征,因此该动作将他带到一个页面,在该页面中,他可以看到所选特征的属性,并且对于每个所选特征,都有一个用于编辑特征的链接。 我可以完美地编辑和更新该功能,但我希望操作更新能够重定向到包含所选功能的页面,以便用户不必再次选择它们。

问题在于,当我执行redirect_to select_multiple_features_path时,它不会导致select_multiple操作。我收到以下错误:

<小时/> Features>中的 ActiveRecord :: RecordNotFound#show

无法找到id = select_multiple

的功能

服务器输出:http://img138.imageshack.us/img138/1382/sfp4.jpg


控制器:

  def edit
    @feature = Feature.find(params[:id])
  end

  def update
    @feature = Feature.find(params[:id])    
    respond_to do |format|
      if @feature.update_attributes(params[:feature])
        format.html { redirect_to select_multiple_features_path, notice: 'Feature was successfully updated.' }
      else
        format.html { render action: "edit" }
        format.json { render json: @feature.errors, status: :unprocessable_entity }
      end
    end
  end

  def select_multiple
    if params[:features_ids].nil?
      @features = Feature.find(session[:features_ids])
    else
      @features = Feature.find(params[:features_ids])
      session[:features_ids] = params[:features_ids]
    end
  end

路线:

resources :attached_assets

resources :modifications

resources :maps

resources :coordinates

resources :results do
    collection do
      post 'select_number_of_groups'
    end
end


resources :features do
    collection do
      post 'select_multiple'
    end
end

resources :home

devise_for :users, :path => "auth", :path_names => { :sign_in => 'login', 
                                                     :sign_out => 'logout', 
                                                     :password => 'secret', 
                                                     :confirmation => 'verification', 
                                                     :unlock => 'unblock', 
                                                     :registration => 'register', 
                                                     :sign_up => 'cmon_let_me_in' 
                                                    }

devise_scope :user do
  root to: "devise/sessions#new"
end

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在您的路线中,您要定义post行动,但未定义get行动。 你必须定义:

get 'features/select_multiple' => 'features#select_multiple'

否则它会假设您默认尝试使用#show操作。

您可以在控制台中使用rake routes来显示所有路线和匹配操作