简体   繁体   中英

Ruby on rails form_tag - sending model parameters to controller

Controller: @micropost = Micropost.new(params[:micropost])

But this form_tag is sending me params[:content] instead of params[:micropost][:content]

<%= form_tag( {:controller => :microposts, :action => :create}, :remote => true) do %>

    <%= text_area_tag :content, "", :size=> "20x2" %>
    ...
    ...
    ...
    <%= submit_tag "submit" %>
<% end %>

server:

Processing by MicropostsController#create as JS
Parameters: {"utf8"=>"✓", "content"=>"sdfsdf", "commit"=>"submit"}

You have to do either of the following

<%= text_area_tag "micropost[content]", "", :size=> "20x2" %>

OR

<%= form_for :micropost, :url=>{ :controller => :microposts, :action => :create}, :remote => true do |f| %>
    <%= f.text_area :content, "", :size=> "20x2" %>
<% end %>

You have to avoid mixing form_for and input_tag .

When you declare a form_for @an_object do |form| , the best practice is to use form.text_area :content when :content is an attribute of your @an_object .

In this case, you can also write: text_area_tag "an_object[content]" , but it's a little more dirty.

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