Ruby on Rails 4
我有一个表单来编辑记录。该表单与创建新记录相同。 (除了措辞)。当我点击提交时,它给出了nilLNilClass。我的代码适用于我更新我的其他不涉及连接表的记录,所以我不确定是什么问题。
表格:
<%= form_for(@test) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :type %><br>
<%= f.text_field :type %>
</div>
<div class="field">
<%= f.label :category %><br>
<%= f.select :category, [ ["IP Voice Telephony", "ip_voice"], ["IP Video Surveillance", "ip_video_surveillance"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"] ], {prompt: "Select Category"}, class: "input-lg", :id => "category" %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div id="container" style="width:1140px; margin-left: auto; margin-right: auto;">
<button type="button" class="reset">Reset Search</button>
<table width="100%" class="tablesorter">
<thead>
<tr>
<th width="5%" class="filter-false"><input type="checkbox" onClick="toggle(this)"></th>
<th width="37%" data-placeholder="Search">Content</th>
<th width="10%" data-placeholder="Search">Type</th>
<th width="10%" data-placeholder="Search">Category</th>
<th width="10%" data-placeholder="Search">Product</th>
<th width="10%" data-placeholder="Search">User</th>
<th width="8%" data-placeholder="Search">Active</th>
</tr>
</thead>
<tbody>
<% @questions.each do |q| %>
<tr>
<td><%= check_box_tag "test[question_ids][]", q.id, @test.question_ids.include?(q.id) %></td>
<td><%= q.content %></td>
<td><%= q.question_type %></td>
<td><%= q.category %></td>
<td>GXP</td>
<td><%= q.user_id %></td>
<td><%= q.active %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
</div>
<div class="actions">
<%= f.submit "Update Test", class: "btn btn-lg btn-primary" %>
</div>
<% end %>
控制器:
before_action :set_test, only: [:show, :edit, :update, :destroy]
before_action :signed_in_user, only: [:index, :edit, :show, :update, :destroy]
def edit
@test = Test.find(params[:id])
@questions = Question.all
end
def update
respond_to do |format|
if @test.update(test_params)
format.html { redirect_to @test, notice: 'Test was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @test.errors, status: :unprocessable_entity }
end
end
end
def test_params
params.require(:test).permit(:name, :user_id, :type, :category, :description, :question_ids => [], questions_attributes: [ :id ] ).
merge user_id: current_user.id
end
错误:
Started PATCH "/tests/98" for 127.0.0.1 at 2014-05-20 10:12:58 -0400
Processing by TestsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x=", "test"=>{"name"=>"one two three four, spelling YEah!!", "type"=>"", "category"=>"ip_voice", "description"=>"not so smart, wooops", "question_ids"=>["1"]}, "commit"=>"Update Test", "id"=>"98"}
Test Load (0.3ms) SELECT "tests".* FROM "tests" WHERE "tests"."id" = ? LIMIT 1 [["id", "98"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'x' LIMIT 1
Completed 500 Internal Server Error in 6ms
NoMethodError (undefined method `update' for nil:NilClass):
app/controllers/tests_controller.rb:56:in `block in update'
app/controllers/tests_controller.rb:55:in `update'
这是第56行:if @test.update(test_params)
我确实认为我必须在edit()中定义@test是很奇怪的。我也试过@ test.save同样的错误。我误解了一些事情。
答案 0 :(得分:4)
您需要在@test
操作中定义update
。目前尚未定义,因此其值为nil
,您会收到错误。
在调用update
之前,在@test.update
操作中添加此行:
@test = Test.find(params[:id])
我看到你有一个before_action
回调用于设置测试,即set_test
。在这种情况下,您不需要
@test = Test.find(params[:id])
在update
行动中。您需要做的就是在update
回调中添加before_action
操作:
before_action :set_test, only: [:show, :edit, :update, :destroy]
此外,由于您已在@test
回调中添加了edit
操作,因此建议您<{>>删除来自edit
操作的before_action
设置
最后,set_test
(理想情况下为private
方法)方法应如下所示:
def set_test
@test = Test.find(params[:id])
end
答案 1 :(得分:1)
您需要在控制器中执行此操作
before_action :set_test, only: [:show,:edit,:update,:destroy]
并在控制器的末尾
private
def set_test
@test = Test.find(params[:id])
end
另外,更新您的编辑方法
def edit
#@test = Test.find(params[:id]) remove this line you wont need it.
@questions = Question.all
end
或强>
你可以按照 @kirti Thorat 的回答。