Nerian
Nerian

Reputation: 16197

Rails3 – refactor redirect call

Refactor this:

redirect_to "http://#{@school.id}" + '.' + request.domain + request.port_string + '/'

That is inside the School controller:

def create
    @school = School.new(params[:school]) 

    if @school.save
      redirect_to "http://#{@school.id}" + '.' + request.domain + request.port_string + '/'
    else
      render "new", :layout => nil
    end
end

So that it redirect to:

subdomain.domain.dom/

Routes:

resources :schools root :to => "schools#show"

Things tried:

redirect_to(:host=>@school.id + '.' + request.domain + request.port_string)

Fail because it redirect to http://subdomain.domain.dom/schools

And How do I pass a :notice?

I have this in the view:

<% if flash[:notice] -%>
    <div id="info_panel">
    <%= flash[:notice] -%>
    </div>
<% end -%>

And this in the controller:

flash[:notice] = "School created"
redirect_to(school_base(@school.id))                  

Upvotes: 0

Views: 169

Answers (1)

Brian Rose
Brian Rose

Reputation: 1725

redirect_to "http://#{@school.id}.#{request.domain}#{request.port_string}/"

If you will use this regularly, you might want to consider adding a method to ApplicationController along the lines of:

def url_for_school(school)
  "http://#{school.id}.#{request.domain}#{request.port_string}/"
end
helper_method :url_for_school

Upvotes: 1

Related Questions