I have several questions regarding what exactly the text_shouts_controller is doing with its polymorphic relationship to the shout model. I’ll go ahead and paste the whole controller.
class TextShoutsController < ApplicationController def create content = build_content shout = current_user.shouts.build(content: content) if shout.save redirect_to dashboard_path else flash.alert = "Could not shout." redirect_to dashboard_path end private def build_content TextShout.new(text_shout_parameters) end def text_shout_parameters params.require(:text_shout).permit(:body) end end
My confusion primarily lies at the beginning of the create action. I understand that we are setting the variable ‘content’ to a method that creates a new instance of a TextShout. But I have no idea what the next line
shout = current_user.shouts.build(content: content)
is doing. If someone could walk me through this line of code like I’m a golden retriever that would be great. I am particularly confused by the (content: content) bit. Is the ‘content’ the same as the variable we set above? Why are there two ‘contents’ in the parentheses?
Next if you could explain the text_shout_parameters method:
What exactly is this doing? And what is the require(:text_shout) referring to? And, whatever the :text_shout part is doing, where is the method getting that piece from (when rails sees :text_shout where does rails go to next)?
Thanks so much. I’m sorry for being long-winded.