0

我有一个 Rails 2 站点,我正在尝试向其中添加表单处理程序,但在将 html 表单字段转换为表单处理程序字段时遇到了问题。

表单代码以:

    <% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>

当我尝试类似的事情时,我不断收到错误

    <%= text_field :newsavedmap, :html=>{ :value => 'New Map', :name=>'newsavedmapname', :id=> 'savedmap_name', :size => '30' } %>

错误:

    ActionView::TemplateError (wrong number of arguments (1 for 2)) on line #284 of app/views/layouts/maptry.html.erb:

这里是字段。如何将这些转换为 Rails 2 中的表单处理程序字段?

    <input id="savemap_name" name="newsavedmapname" size="30" type="text" value="New Map"></p>

    <select id="startdrop" name="startthere">
    <OPTIONS HERE>
    </select>

    <select multiple id="waypoints" class="mobile-waypoints-remove" name="waypointsselected[]">
    <OPTIONS HERE>
    </select>

感谢您的任何帮助,您可以提供!

编辑 Text_Field 的 1 个错误代码

使用Bigxiang的方法,我得到

    Processing NewsavedmapsController#create (for IP at Date Time) [POST]
    Parameters: {"endhere"=>"", "endthere"=>"SAMPLE ADDRESS 1", "newsavedmap"=>{"newsavedmapname"=>"test Map"}, "startthere"=>"SAMPLE ADDRESS 2", "starthere"=>"", "optimize"=>"on"}

    ActiveRecord::UnknownAttributeError (unknown attribute: newsavedmapname)

"newsavedmap"=>{"newsavedmapname"=>"test Map"} 的行应该只是读

    "newsavedmapname"=>"test Map"

我怎样才能做到这一点?我的控制器开始于:

    def create

    @newsavedmap = Newsavedmap.new(params[:newsavedmap])
    @newsavedmap.name = params[:newsavedmapname]
4

1 回答 1

1

尝试这个:

<% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
  <%= f.text_field :newsavedmapname :id=>"savemap_name", :size=>30, :value=>"New Map"%>
  <%= f.select :startthere, YOUR_COLLECTIONS, {}, {:id=>"startdrop"}%>
  <%= f.select :waypointsselected, YOUR_COLLECTIONS, {}, {:id=>"waypoints", :class=>"mobile-waypoints-remove", :multiple => true}%>
<% end %>

确保 YOUR_COLLECTIONS 应该是一个类似 ['a', 'b', 'c'] 或 [['name1', id1],['name2', id2],['name3', id3]] 的数组。

如果你坚持参数是“newsavedmapname”=>“test Map”,试试这个:

<% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
  <%= text_field_tag :newsavedmapname, "New Map", :id=>"savemap_name", :size=>30%>
  <%= select_tag :startthere, options_for_select(YOUR_COLLECTIONS), {:id=>"startdrop"}%>
  <%= select_tag :waypointsselected, options_for_select(YOUR_COLLECTIONS), {:id=>"waypoints", :class=>"mobile-waypoints-remove", :multiple => true}%>
<% end %>

但我不明白为什么不使用与列名相同的参数名。例如,我看到您的 newsavedmap 模型有一个名为“name”的列。你可以直接使用

<% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
  <%= f.text_field :name , :value=>"New Map" %>
<% end %>

在您的控制器中,您可以删除行 @newsavedmap.name = params[:newsavedmapname]

def create

  @newsavedmap = Newsavedmap.new(params[:newsavedmap])

  if @newsavedmap.save
    #######
  end
end
于 2013-08-24T06:58:42.503 回答