2

我有一个存储酱汁细节的程序。基本上,当我进入编辑表单时,我正在做我应该做的所有事情,id 已成功通过,但是所有表单字段都保持为空,而不是显示数据库中已经存在的值。

在我的控制器中:

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 %>

我哪里错了?

4

1 回答 1

3

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

<%= form_for(@sauce, :url => {:action => 'update', :id => @sauce.id}) do |f| %>
于 2012-11-16T16:32:26.183 回答