Ruby Rails page jumps (render and redirect_to) _ruby topics

Source: Internet
Author: User
Ruby Code
Copy Code code as follows:

If @user. Update_attributes (:p assword => params[:user][:p assword])
Flash[:notice] = ' Password modification complete '
Redirect_to:action => ' index '
Else
Redirect_to:action => ' Change_pass ',: ID => @user
End

Then casually changed the next 5th line, the redirect_to changed to render, incredibly OK. On the internet found that redirect_to and render still have a lot of difference, I used to have no attention, sweat.

REDIRECT_TO implementation is the action method of the jump, to launch a new request to the browser, the specific use of the following methods:
Copy Code code as follows:

Redirect_to:action => ' edit ',: ID => 7
Redirect_to "http://wiisola.javaeye.com/"
Redirect_to "/images/1.jpg"
Redirect_to:back

The 4th line is to go back to the page that you visited last time.
Render can be translated as "render", that is, render only renders a new template without executing the appropriate action. The use of render is as follows:
Copy Code code as follows:

Render (: Text => string)
Render (: Inline => string, [: Type => "rhtml" | " Rxml "])
Render (: Action => action_name)
Render (: File => path, [: Use_full_path => True|false])
Render (: template => name)
Render (:p artial => name)
Render (: Nothing=>true)
Render ()

Line 1th: render the text directly
Line 2nd: Render the incoming string as a template (rhtml or rxml)
Line 3rd: A template that invokes an action directly, equivalent to forward to a view
Line 4th: Using a template file render, you can pass in a relative path when the Use_full_path argument is true
Line 5th: Use template name render,e.x.: Render (: Template => "Blog/short_list")
Line 6th: Rendering with local templates
Line 7th: Nothing is output, including layout
Line 8th: Default render, equivalent to render (: Action => Self)
To fill in a manual render example:
Ruby Code
Copy Code code as follows:

def search
@results =search.find (Params[:query])
Case @results
When 0 then render:action=> "No_results"
When the 1 then render:action=> "Show"
When 2..10 then render:action=> "Show_many"
End
End
def search
@results =search.find (Params[:query])
Case @results
When 0 then render:action=> "No_results"
When the 1 then render:action=> "Show"
When 2..10 then render:action=> "Show_many"
End
End

But my own problem is still not resolved, why use render rendering a template can display error messages, but with redirect_to request is not it? Maybe look at the source can solve it, but do not understand, Khan. In a word, remember render and Redirect_to's usage.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.