【发布时间】:2014-05-20 22:43:41
【问题描述】:
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 有同样的错误。我误会了什么。
【问题讨论】:
-
你有一个
before_action用于在你的控制器中设置@test变量吗?如果没有,你必须定义一个 -
等等,我愿意,before_action :set_test, only: [:show, :edit, :update, :destroy] 你不是这个意思吗?
-
您也必须为此定义操作。检查我的答案。
标签: ruby-on-rails ruby ruby-on-rails-4