views:

47

answers:

2

I'm still new to ruby on rails and I'm currently having a issue with routing and redirecting.

I have two controllers "candidate" and "survey". I'm currently viewing "canidate/editEmail.html"

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. 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. "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

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.

Normally I would think that if i went directly to the file

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. 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

 map.resources :survey

Any Advice Appreciated. Thanks! D

+1  A: 

Add this to routes.rb

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

Run rake routes from terminal in the route of your application to see your routes.

Use named routes:

redirect_to survey_end_path
mark
how is survey_end_path defined. I updated the code to reflect that, but its giving me undefined local variable or method `survey_end_path' for #<CandidatesController:0xb6bfd9dc>
I tried "end_survey_path" as well since that's what is printed out when you do "rake routes." It prints out "end_survey." but still no go
I also updated "end" to "lastPage" so it doesn't confused with the key word end click http://www.otoplusvn.com/TherapistSurvey/candidates/editEmail/2and you'll get an page http://www.otoplusvn.com/TherapistSurvey/survey/lastPage
Actaully I figured it out. Before I had: map.resources :survey map.resources :survey, :collection => {:lastPage => :get} When I moved the ordering it addressed the issue: map.resources :survey, :collection => {:lastPage => :get} map.resources :survey Any reasoning behind this?
You only need to define it once. By defining a route resource you're generating a whole bunch of default routes, the collection is adding to it. I highly recommend you read the rails guide on this. It will take you 30 minutes but it will be a worthwhile investment of time. This approach of guessing your way along really isn't productive. :)http://guides.rubyonrails.org/routing.html
mark
+1  A: 

You need add all other action you want.

map.resources add only route need by ressources browsing.

So add in collection option your method end in your case

map.resources :survey, :collection => {:end => :get}
shingara
I added it, but I still get the unknown action.I did a rake routes to show you the paths. 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"}
does "end" have to actually be defined as an action or having it as a end.html.erb sufficient enough under view/survey?
I also updated "end" to "lastPage" so it doesn't confused with the key word end click otoplusvn.com/TherapistSurvey/candidates/editEmail/2 and you'll get an page otoplusvn.com/TherapistSurvey/survey/lastPage –
Actaully I figured it out. Before I had: map.resources :survey map.resources :survey, :collection => {:lastPage => :get}When I moved the ordering it addressed the issue: map.resources :survey, :collection => {:lastPage => :get} map.resources :surveyAny reasoning behind this?