Jump to content

The ultimate community for Ruby on Rails developers.


Photo

redirect_to previous page

routing

  • Please log in to reply
3 replies to this topic

#1 crogamer

crogamer

    Passenger

  • Members
  • 2 posts

Posted 07 January 2014 - 08:21 PM

Hi guys,

 

i have a question. I'm developing an App to manage User-Ideas.

 

The current status:

 

controller: post -> action: create, onsuccess: redirect_to posts_overview_path

 

welcome: index -> calls the post#create action -> the user will be  redirected to the posts_overview_path

 

dashboard: index -> calls the post#create action -> the user will be  redirected to the posts_overview_path

 

 

Now I want to call the post#create action but onsuccess i want to redirect to a specific URL.

 

Of course it's possible somehow, but I don't know.

 

Can anyone help me?

 

Thanks Markus



#2 Rails_Driver

Rails_Driver

    Passenger

  • Members
  • 5 posts
  • LocationSan Antonio, Texas

Posted 07 January 2014 - 11:30 PM

What you might consider is in your PostsController setting a condition (success = true) when you call .save on @post, else, success = false.

 

Then you can do something like the following:

 

  if success
    
redirect_to 'http://www.somewhere.com
  else
    redirect_to some_rails_path
  end

 

Also, the syntax for the redirect_to a non-Rails URL varies depending on Rails version.



#3 crogamer

crogamer

    Passenger

  • Members
  • 2 posts

Posted 08 January 2014 - 06:52 PM

Hmm,

 

i think you don't understand me because i explained it to low.

 

I hope i solved the problem with this code snipped:

    respond_to do |format|
      if @comment.save
        format.html { redirect_to(:back)}

        format.json { render action: 'show', status: :created, location: @comment }
        else
        format.html { redirect_to project_dashboard_path, notice: 'Can't save'}
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end

That's the solution:

 

redirect_to(:back)

 

But if i take this solution i can't send a notice with this request.

format.html { redirect_to(:back), notice: 'Wohohh'}

That doesn't work. Got anyone a solution for this problem?



#4 himanshu339

himanshu339

    Passenger

  • Members
  • 9 posts

Posted 17 March 2014 - 08:45 AM

Make sure of two things:

  1. format.html { redirect_to(:back, notice: 'Wohohh.') }. notice parameter should be part of redirect_to
  2. <p id="notice"><%= notice %></p> should be present in the view.

Hope this helps!







Also tagged with one or more of these keywords: routing

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users