【问题标题】:How can I update or save a record using the same HTML form in Ruby on Rails?如何在 Ruby on Rails 中使用相同的 HTML 表单更新或保存记录?
【发布时间】:2013-08-11 22:24:04
【问题描述】:

这是我第一次尝试在 Ruby on Rails 中更新记录,因此在此先感谢您的帮助。我有一个 HTML 表单。在表单中,我想要更新 MySQL 中 newsavedmaps 表中的记录的选项。

表格比下面的要复杂得多,但它有这样的基本结构:

ma​​ptry.html.erb

    <form id="createaMap" action="/newsavedmaps" method="post">                 
    <label for="savemap_name">Map Title</label>
    <input id="savemap_name" name="newsavedmapname" size="30" type="text" value="New Map"></p>
    <%= link_to 'Update Saved Map', @newsavedmap, :confirm => 'Are you sure?', :method => :put %>
    <input type="submit" id="savethismap" value="Save As New Map">
    </form>

newsavedmaps 视图文件夹中的edit.html.erb

    <h1>Editing newsavedmap</h1>

    <% form_for(@newsavedmap) do |f| %>
      <%= f.error_messages %>

      <p>
        <%= f.submit 'Update' %>
      </p>
    <% end %>

    <%= link_to 'Show', @newsavedmap %> |
    <%= link_to 'Back', newsavedmaps_path %>

如果我从新记录开始,请保存地图。

但是,如果我从已定义 @newsavedmap 的位置开始并单击“更新保存的地图”链接,它只会将我带到“/newsavedmaps/ID”。

我知道我可以使用表单助手,但我试图避免依赖于重新创建整个表单的解决方案,因为它是在 HTML 中设置的。再次感谢您的帮助!

编辑 1

这是我尝试使用 Bigxiang 的代码的日志。看起来正在发送正确的信息,但没有更新记录。请注意,有两个开始和两个结束输入,并且取开始或结束地址,这就是为什么下面两个留空的原因。

我还有什么需要检查的吗?

    Processing NewsavedmapsController#update (for IP at DATE) [PUT]
    Parameters: {"endhere"=>"", "endthere"=>"1600 Pennsylvania Avenue, Washington, DC 20500", "newsavedmapname"=>"New Map test", "startthere"=>"1201 Farragut Street Northwest, Washington, DC 20011", "id"=>"74", "starthere"=>"", "optimize"=>"on"} 
    Redirected to http://site.com/maptry
     Completed in 11ms (DB: 0) | 302 Found [http://site.com/newsavedmaps/74]

NewsavedmapsController#update

    def update
       @newsavedmap = Newsavedmap.find(params[:id])
        respond_to do |format|
          if @newsavedmap.update_attributes(params[:newsavedmap])
            flash[:notice] = 'Newsavedmap was successfully updated.'
            format.html { redirect_to "/maptry" }
            format.xml  { head :ok }
          else
            format.html { render :action => "edit" }
            format.xml  { render :xml => @newsavedmap.errors, :status => :unprocessable_entity }
          end
        end
      end

newsavedmaps 的edit.html.erb

在我的路线中,我有 map.resources :newsavedmaps,我在视图中为 Newsavedmaps 创建了一个文件夹。其中,我有一个如下所示的 edit.html.erb 文件:

    <h1>Editing newsavedmap</h1>

    <% form_for(@newsavedmap) do |f| %>
      <%= f.error_messages %>

    id: <%= text_field :newsavedmap, :id %><br />
    itinerary_id: <%= text_field :newsavedmap, :itinerary_id %><br />
    start: <%= text_field :newsavedmap, :start %><br />
    start_lat: <%= text_field :newsavedmap, :start_lat %><br />
    start_long: <%= text_field :newsavedmap, :start_long %><br />
    start_masterlocation_id: <%= text_field :newsavedmap, :start_masterlocation_id %><br />
    end: <%= text_field :newsavedmap, :end %><br />
    end_lat: <%= text_field :newsavedmap, :end_lat %><br />
    end_long: <%= text_field :newsavedmap, :end_long %><br />
    end_masterlocation_id: <%= text_field :newsavedmap, :end_masterlocation_id %><br />
    name: <%= text_field :newsavedmap, :name %><br />
    optimize: <%= text_field :newsavedmap, :optimize %><br />

      <p>
        <%= f.submit 'Update' %>
      </p>
    <% end %>

    <%= link_to 'Show', @newsavedmap %> |
    <%= link_to 'Back', newsavedmaps_path %>

不幸的是,当我尝试转到 /newsavedmaps/ID 时,这会导致错误。错误是“未定义的方法‘endhere’。”但是,我不确定这是否是正确的方法。

【问题讨论】:

  • 显然你的模型没有endhere 方法。该列是否存在于您的数据库中?
  • 谢谢,bdares。我的错;我使用了错误的列标题。使用我在上面修复的 edit.html.erb 版本,我可以手动编辑字段并提交表单,并且可以正常工作。但是,使用 maptry.html.erb 提交表单仍然会给出相同的日志代码,而不会对记录进行任何更改:Processing NewsavedmapsController#update (for IP at DATE) [PUT] Parameters: {"endhere"=&gt;"", "endthere"=&gt;"1600 Pennsylvania Avenue, Washington, DC 20500", "newsavedmapname"=&gt;"New Map test", "startthere"=&gt;"1201 Farragut Street Northwest, Washington, DC 20011", "id"=&gt;"74", "starthere"=&gt;"", "optimize"=&gt;"on"}
  • 再次查看您的表单,您没有将输入与模型正确关联。看看@Bigxiang 的回答,看看他的表单与你的不同。他的表单应该可以正常工作。
  • Bdares,我看到他使用了单独的表单处理程序字段,例如 。你是说这和我在maptry.html.erb中的字段有区别,比如&lt;input id="savemap_name" name="newsavedmapname" size="30" type="text" value="New Map"&gt;&lt;/p&gt;
  • 与模型关联后生成的表单会有name="newsavedmap[endhere]"这样的属性,而不是name="endhere",两者都试一下,用firebug看看

标签: mysql ruby-on-rails formhelper


【解决方案1】:

我认为这里有几个问题。

第一,不能使用提交表单

&lt;%= link_to 'Update Saved Map', @newsavedmap, :confirm =&gt; 'Are you sure?', :method =&gt; :put %&gt;

因为link_tomethod会生成动态表单并提交,所以你的表单不会被提交。

第二,我不确定在更新定义的@newsavedmap 时要提交到哪里。

如果你想提交到“/newsavedmaps/ID”并执行控制器的update方法保存记录,我建议使用这个

<%= form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f|%>
    End Here: <%= f.text_field :endhere %><br />
    End There: <%= f.text_field :endthere %><br />
    Newsavedmap Name: <%= f.text_field :newsavedmapname %><br />
    Start There: <%= f.text_field :startthere %><br />

    Start There: <%= f.text_field :starthere %><br />
    <input type="submit" id="savethismap" value="Save">
<% end %>

保留你表单的所有内容,只需修改form标签为form_for,所以如果你的@newsavedmap是新记录,保存按钮会执行控制器的create方法,如果@newsavedmap是定义的记录,保存按钮将使用正确的参数执行控制器的update方法。

【讨论】:

  • Bigxiang,我已经更新了代码,但我仍然遇到了更新记录的问题。您对可能发生的事情有任何想法吗?
  • 你好,可以给我看看NewsavedmapsController#update的代码吗?
  • 还添加了edit.html.erb。最初,newsavedmaps 的 edit.html.erb 没有任何文本输入字段,所以我猜这是问题的一部分。
  • 嗨,大翔。请参阅我对上述 bdares 的评论。
猜你喜欢
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
相关资源
最近更新 更多