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 

形式 Partial :

<%= 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 に答える