[RUBY] Criteria for proper use of render and redirect_to

【Overview】

1. Conclusion </ b>

2. How to use </ b>

3. Why does such a difference occur? </ B>

4. What I learned from here </ b>

  1. Conclusion

Do you want to pinch the Controller or do you want to pinch it once </ b>
2. How to use

render

(i) I just want to specify a View file and display it

controller.rb


def
 render '***(View file name).index(Action name) #---"a"
end

or

def
 render :index(Action name) #---"b"
end

What is the difference between "a" and "b" a➡︎ I want to display the actions of different controllers b ➡︎ I want to display the action of the same controller that describes render It means that.

(ii) I want to use the same program in multiple html.erb files

python


<%= render 'public/error_messages', model: f.object %> #---Excerpt from my article

In the above case, when you enter empty in the index, new, show action, the message "Requirements have not been entered!" Is displayed. ( How to put out error bundling )

redirect_to

(i) I want to perform an action once through Controller processing

controller


def
 redirect_to("/app/(View folder name)/(Action name.html.erb)")
end

or

def
 redirect_to ****_****_path
end

In other words, it means specifying the URL.

  1. Why does such a difference occur?

The bottom line is whether there is a "Controller again" in between.

❶ render is for the movement of Controller ➡︎ View ❷ redirect_to is Controller ➡︎routes.rb (id based on URL) ➡︎Controller ➡︎View It will be a movement. If only render is used, it only displays the "just" page, so processing may be troublesome and an error may occur. Since redirect_to sandwiches the Controller once, the update / destory action can be performed without error.


Recommended Posts