【问题标题】:Redirecting and routing Ruby on Rails重定向和路由 Ruby on Rails
【发布时间】:2010-08-09 08:54:21
【问题描述】:

我还是 Ruby on Rails 的新手,目前遇到路由和重定向问题。

我有两个控制器“候选人”和“调查”。我目前正在查看“canidate/editEmail.html”

http://www.otoplusvn.com/TherapistSurvey/candidates/editEmail/2

一旦点击提交按钮,就会执行候选人更新邮件的方法:

  def updateEmail
    @candidate = Candidate.find(params[:id])

    respond_to do |format|
      if @candidate.update_attributes(params[:candidate])
        flash[:notice] = 'Candidate email was successfully updated.'
        format.html { redirect_to :controller => "survey", :action => "end" }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @candidate.errors, :status => :unprocessable_entity }
      end
    end
  end

此代码将更新数据库中候选人的电子邮件并将其重定向。我想将它重定向到位于 view/Survey/end.html.erb 下的名为 end.html.erb 的 html 页面。但是,使用目前的代码,它会给我一个

Unknown action

No action responded to show. Actions: download and lastPage

其中 download 和 lastPage 是 Survey 控制器中的唯一方法。 “end”不是survey_controller.rb中定义的方法,但我认为它会自动跳转到位于views/survey下的end.html.erb

在我的本地机器上,我可以让它与这个更新的代码一起工作:

format.html { redirect_to :controller => "survey/end" }

但这在我加载 ruby​​ on rail 应用程序的网站上不起作用。无论哪种情况,我认为可能有一种更简单的方法可以做到这一点,但我做错了。

当您将本地 ruby​​ on rail 应用程序移植到主机时,我认为它必须处理我不知道的 route.rb 文件或权限。

通常我会认为如果我直接进入文件

http://www.otoplusvn.com/TherapistSurvey/survey/end

我可以像在本地计算机上一样查看该页面,但我无法使用我托管的内容。

一般来说,我只是希望能够重定向(跳转)到另一个 html 页面,表明电子邮件更新已正确完成并且调查已结束。这一切都是通过点击提交按钮触发的,该按钮导致对数据库的更新被称为重定向到另一个页面。问题是如何正确进行重定向。

顺便说一句:在我的 routes.rb 我有

 map.resources :survey

任何建议表示赞赏。谢谢! D

【问题讨论】:

    标签: ruby-on-rails redirect routing


    【解决方案1】:

    您需要添加所有其他您想要的操作。

    map.resources 仅添加资源浏览所需的路线。

    所以在你的情况下添加你的方法结束的收集选项

    map.resources :survey, :collection => {:end => :get}
    

    【讨论】:

    • 我添加了它,但我仍然得到未知的操作。我做了一个 rake routes 来向你展示路径。 PUT /candidates/:id(.:format) {:controller=>"candidates", :action=>"update"} DELETE /candidates/:id(.:format) {:controller=>"candidates", :action =>"destroy"} end_survey GET /survey/end(.:format) {:controller=>"survey", :action=>"end"}
    • “end”是否必须实际定义为一个动作或将其作为 end.html.erb 在视图/调查中足够多?
    • 我还将“end”更新为“lastPage”,这样它就不会与关键字 end 混淆,单击 otoplusvn.com/TherapistSurvey/candidates/editEmail/2,您将获得一个页面 otoplusvn。 com/TherapistSurvey/survey/lastPage –
    • 其实我想通了。在我拥有之前: map.resources :survey map.resources :survey, :collection => {:lastPage => :get} 当我移动订单时,它解决了这个问题: map.resources :survey, :collection => {:lastPage => :get} map.resources :survey 这背后有什么原因吗?
    【解决方案2】:

    将此添加到 routes.rb

    map.resources :survey, :collection => {:end => :get}
    

    在应用程序的路由中从终端运行 rake 路由以查看您的路由。

    使用命名路由:

    redirect_to survey_end_path
    

    【讨论】:

    • survey_end_path 是如何定义的。我更新了代码以反映这一点,但它给了我未定义的局部变量或方法 `survey_end_path' 用于 #<0xb6bfd9dc>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2011-04-17
    相关资源
    最近更新 更多