简体   繁体   English

重定向和路由Ruby on Rails

[英]Redirecting and routing Ruby on Rails

I'm still new to Ruby on Rails and I'm currently having a issue with routing and redirecting. 我还是Ruby on Rails的新手,目前在路由和重定向方面遇到问题。

I have two controllers "candidate" and "survey". 我有两个控制器“候选人”和“调查”。 I'm currently viewing "canidate/editEmail.html" 我目前正在查看“ canidate / editEmail.html”

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

Once the submit button is clicked, the candidate's method for updating the email is executed: 单击提交按钮后,将执行候选人更新电子邮件的方法:

  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

This code will update the Candidate's email in the database and redirect it. 此代码将更新数据库中候选人的电子邮件并重定向它。 I want to redirect it to an html page called end.html.erb that is located under the view/Survey/end.html.erb. 我想将其重定向到位于view / Survey / end.html.erb下的一个名为end.html.erb的html页面。 However, With the present code it will give me a 但是,使用当前代码,它将给我一个

Unknown action

No action responded to show. Actions: download and lastPage

where download and lastPage are the only methods in the Survey controller. 其中download和lastPage是Survey控制器中唯一的方法。 "end" is not a method defined in the survey_controller.rb, but I assumed that it would automatically jump to the end.html.erb located under views/survey “ end”不是在survey_controller.rb中定义的方法,但是我认为它会自动跳转到位于views / survey下的end.html.erb。

On my local machine, I'm able to get this to work with this updated code: 在本地计算机上,我可以将其与更新后的代码一起使用:

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

But this doesn't work on the site that I loaded the ruby on rail application on. 但这在我将红宝石加载到铁路应用上的站点上不起作用。 In either case, I think there is probably an easier way to do this and i'm doing this incorrectly. 无论哪种情况,我都认为可能有一种更简单的方法可以做到,而我做错了。

I think it has to deal with the route.rb file or permission that I'm unaware of when you port a local ruby on rail application to a host. 我认为它必须处理在将铁路应用程序上的本地红宝石移植到主机时我不知道的route.rb文件或权限。

Normally I would think that if i went directly to the file 通常我会认为,如果我直接进入文件

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

I would be able to see the page like I was able on my local machine, but I'm not able to with what I have hosted. 我将能够像在本地计算机上一样看到该页面,但无法使用托管的内容。

In general, I just want to be able to redirect (jump) to another html page indicating that the email update was done correctly and that the survey is over. 通常,我只希望能够重定向(跳转)到另一个html页面,指示电子邮件更新正确完成并且调查已结束。 This is all triggered by hitting a submit button that result in an update to the database being called an a redirect to another page. 这全部是通过单击“提交”按钮触发的,该按钮导致对数据库的更新称为“重定向到另一个页面”。 The problem being how to do the redirect correctly. 问题是如何正确进行重定向。

BTW: in my routes.rb I have 顺便说一句:在我的routes.rb中,我有

 map.resources :survey

Any Advice Appreciated. 任何建议表示赞赏。 Thanks! 谢谢! D d

Add this to routes.rb 将此添加到routes.rb

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

Run rake routes from terminal in the route of your application to see your routes. 在应用程序的路由中从终端运行rake路由以查看您的路由。

Use named routes: 使用命名路线:

redirect_to survey_end_path

You need add all other action you want. 您需要添加所有其他所需的操作。

map.resources add only route need by ressources browsing. map.resources仅通过资源浏览添加路线需求。

So add in collection option your method end in your case 因此,在您的情况下,在集合选项中添加方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM