ruby-on-rails - Rails : Edit/Update Record, 使用当前记录字段条目填充文本字段

标签 ruby-on-rails forms controller helper

我有一个存储酱汁详细信息的程序。 基本上,当我进入编辑表单时,我正在执行我应该执行的所有操作,id 已成功传递,但是所有表单字段都保持为空,而不是显示数据库中已有的值。

在我的 Controller 中:

def edit 
 @sauce = Sauce.find(params[:id])
 @pagetitle = "Edit #{@sauce.name} Sauce"
end

def update
 # Find object using form parameters
 @sauce = Sauce.find(params[:id])
 # Update the object
 if @sauce.update_attributes(params[:page])
   # If update succeeds, redirect to the list action
   flash[:notice] = "Sauce updated."
   redirect_to(:action => 'list')
 else
   # If save fails, redisplay the form so user can fix problems
   render('edit')
 end 

表单部分:

<%= error_messages_for(@sauce) %> 
 <table summary="Sauces Form Fields">
  <tr>
   <th><%= f.label(:name,"Sauce Name") %></th>
   <td><%= f.text_field(:name) %></td>
  </tr>
  <tr>
   <th><%= f.label(:description, "Description") %></th>
   <td><%= f.text_area(:description, :size => '40x5') %></td>
  </tr>
  <tr>
   <th><%= f.label(:heat_level, "Heat Level") %></th>
   <td><%= f.select(:heat_level,{ 1 => "1", 2 => "2", 3 => "3", 4 => "4", 5 => "5"}) %></td>
  </tr>
  <tr>
   <th><%= f.label(:pic_url, "Picture URL") %></th>
   <td><%= f.text_field(:pic_url) %></td>
  </tr>
  <tr>
   <th><%= f.label(:title_colour, "Title Colour") %></th>
   <td><%= f.text_field(:title_colour) %></td>
  </tr>
  <tr>
   <th><%= f.label(:description_colour, "Desc Colour") %></th>
   <td><%= f.text_field(:description_colour) %></td>
  </tr>
 </table>

我的edit.html.erb

<%= form_for(:subject, :url => {:action => 'update', :id => @sauce.id}) do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>  
<%= submit_tag("Update Subject") %>
<% end %>

我哪里出错了?

最佳答案

错误是form_for(:subject)。如果您希望 Rails 正确填写表单,您需要使用与实例变量相同的符号,或者仅使用实例变量。试试这个:

<%= form_for(@sauce, :url => {:action => 'update', :id => @sauce.id}) do |f| %>

关于ruby-on-rails - Rails : Edit/Update Record, 使用当前记录字段条目填充文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13420507/

相关文章:

php - 从循环创建的表单中检索多行...卡住了

javascript - 在 Rails 应用程序中重新加载部分内容

ruby-on-rails - 如何使用设备配置超时?

ruby-on-rails - 在散列中找到低于前一个数字的第一个键/值的最快方法

ruby-on-rails - 如何格式化 Rails 编辑字段中显示的值?

javascript - 使用 Javascript 检查输入表单中的字符和字符顺序,以显示与字符和字符顺序匹配的图像

html - Servlet返回“HTTP状态404请求的资源(/Servlet)不可用”

php - 如何将模型加载到 MVC 中的 Controller 中

php - 使用 find (cakePHP) 时临时删除连接

ruby-on-rails - Controller 最佳实践 : Multiple Methods or Multiple cases in Show