【发布时间】:2013-08-11 22:24:04
【问题描述】:
这是我第一次尝试在 Ruby on Rails 中更新记录,因此在此先感谢您的帮助。我有一个 HTML 表单。在表单中,我想要更新 MySQL 中 newsavedmaps 表中的记录的选项。
表格比下面的要复杂得多,但它有这样的基本结构:
maptry.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"=>"", "endthere"=>"1600 Pennsylvania Avenue, Washington, DC 20500", "newsavedmapname"=>"New Map test", "startthere"=>"1201 Farragut Street Northwest, Washington, DC 20011", "id"=>"74", "starthere"=>"", "optimize"=>"on"} -
再次查看您的表单,您没有将输入与模型正确关联。看看@Bigxiang 的回答,看看他的表单与你的不同。他的表单应该可以正常工作。
-
Bdares,我看到他使用了单独的表单处理程序字段,例如 。你是说这和我在maptry.html.erb中的字段有区别,比如
<input id="savemap_name" name="newsavedmapname" size="30" type="text" value="New Map"></p>? -
与模型关联后生成的表单会有
name="newsavedmap[endhere]"这样的属性,而不是name="endhere",两者都试一下,用firebug看看
标签: mysql ruby-on-rails formhelper