How does create work?


#1

I don't understand this part:

def create 
  @message = Message.new(message_params) 
  if @message.save 
    redirect_to '/messages' 
  else 
    render 'new' 
  end 
end

private

def message_params
  params.require(:message).permit(:content)
end

would anyone be so kind to explain me what it does and how?
Thanks!


#2

Hi Chitzui,

message_params says to the computer, “I must have a message come in when the form is submitted, and I want it to have the content field along with it”.

@message = Message.new(message_params) saves a new message (with the message parameters) into a variable, @message.

Then, the if/else statement says to the computer, “If the message gets saved, redirect the page to the index action for Messages. Otherwise, show the new action for Messages”.

Does this help? :slight_smile: