Mysql 如何在RubyonRails中使用相同的HTML表单更新或保存记录?
这是我第一次尝试在RubyonRails中更新记录,所以提前感谢您的帮助。我有一个HTML表单。在表单中,我希望在MySQL中更新newsavedmaps表中的记录 该表单比下面的表单复杂得多,但它具有以下基本结构: maptry.html.erbMysql 如何在RubyonRails中使用相同的HTML表单更新或保存记录?,mysql,ruby-on-rails,formhelper,Mysql,Ruby On Rails,Formhelper,这是我第一次尝试在RubyonRails中更新记录,所以提前感谢您的帮助。我有一个HTML表单。在表单中,我希望在MySQL中更新newsavedmaps表中的记录 该表单比下面的表单复杂得多,但它具有以下基本结构: maptry.html.erb <form id="createaMap" action="/newsavedmaps" method="post"> <label for="savemap_name">Ma
<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>
NewsavedmapsController#更新
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
edit.html.erb for newsavedmaps
在我的路线中,我有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 %>
编辑newsavedmap
id:
行程号:
开始:
开始时间:
开始时间:
开始\u主位置\u id:
结束:
结束时间:
结束时长:
结束主位置id:
名称:
优化:
|
不幸的是,当我尝试转到/newsavedmaps/ID时,这会导致一个错误。错误是“undefined method`endhere.”。不过,我不确定这是否是正确的方法 我认为这里有几个问题 首先,您不能使用提交表单
“你确定吗?”,:method=>:put%>
因为使用方法链接到将生成一个动态表单并提交,所以您的表单不会被提交
第二,我不确定更新定义的@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 %>
{:id=>'createaMap'}do | f |%>
到此结束:
到此结束:
Newsavedmap名称:
从那里开始:
从那里开始:
保留表单的所有内容,只需将表单标签修改为表单,这样如果@newsavedmap是一条新记录,保存按钮将执行控制器的create
方法,如果@newsavedmap是一条已定义的记录,保存按钮将执行控制器的update
方法,参数正确。,我已经更新了代码,但在更新记录时仍然遇到问题。你对可能发生的事情有什么想法吗?嗨,你能给我看看NewsavedmapsController#update的代码吗?还添加了edit.html.erb。最初,newsavedmaps的edit.html.erb没有任何文本输入字段,所以我猜这是问题的一部分。嗨,Bigxiang。请参阅我对上述bdares的回复。很明显,您的模型没有endhere
方法。您的数据库中是否存在该列?谢谢,b战神。我的错;我使用了错误的列标题。使用上面我修复的edit.html.erb版本,我可以手动编辑字段并提交表单,它可以正常工作。但是,使用maptry.html.erb提交表单时,仍然会给出相同的日志代码,而不会对记录进行任何更改:正在处理NewsavedmapsController#更新(用于日期的IP)[PUT]参数:{“endhere”=>,“endhere”=>“1600宾夕法尼亚大道,华盛顿特区20500”,“newsavedmapname”=>“新地图测试”,“startthere”=>“华盛顿特区西北法拉古特街1201号,20011”,“id”=>“74”,“starthere”=>,“优化”=>“on”}
再看看你的表单,你没有正确地将输入与模型关联起来。看看@Bigxiang的答案,看看他的表单与你的表单是如何构建的。他的表单应该工作正常。B战神,我看到他使用了单独的表单处理程序字段,比如。你是说这与我的字段I有区别吗n maptry.html.erb,类似于
?与模型关联后生成的表单将具有类似于name=“newsavedmap[endhere]”的属性,而不是name=“endhere”
,请尝试这两种方法,并使用firebug查看
<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 %>
<%= 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 %>