简体   繁体   中英

Ruby On Rails: Understanding Form Submission

I have been previously working on Spring MVC mostly so thought about understanding ROR for a change.

I was going though the tutorials on CodeAcademy, wherein I got a little confused by the following Form Submission mechanism.

So here's a sample form.

<%= form_for(@message) do |f| %>  
  <div class="field"> 
    <%= f.label :message %><br> 
    <%= f.text_area :content %> 
  </div> 
  <div class="actions"> 
    <%= f.submit "Create" %> 
  </div> 
<% end %>

The corresponding action handler in Controller is as follows:

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

I am confused by the following line params.require(:message).permit(:content) Since :message is a label field, it should always have a value. So .required(:message) should always be true and the method message_params should always return the value of the textbox. Correct?

I am not sure if I understand it completely. Could someone explain it a bit clearly.

message is just the label of the content field.

The require(:message) is the parameter hash of your form and permit(:content) is your permitted content parameter. So your parameter hash could look something like this:

{
  message: {
    content: "Your content here..."
  }
}

This technique is called " Strong Parameters ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM