简体   繁体   English

尝试更新记录-Rails

[英]Trying to update a record - Rails

I'm simply trying to update a name and description on the assignment modal. 我只是在尝试更新assignment模式的名称和说明。 I can't seem to figure out what I'm doing wrong? 我似乎无法弄清楚我在做什么错?

Here is my code: 这是我的代码:

Controller 控制者

 def edit
    @assignment = Assignment.find_by(id: params[:id])
  end

  def update
    @assignment = Assignment.find_by(id: params[:id])
    if @assignment.update_attributes(assignment_params)
      flash[:notice] = "Successfully Edited The Assignment"
      redirect_to account_assignments_path
    else
      render 'edit'
    end
  end

VIEW 视图

<td><%= button_to "edit assignment", edit_account_assignment_path(assignment), method: :get %>

Which goes to 去哪

 <%= form_for @assignment, url: account_assignments_path(@assignment) do |form| %>
  <%= form.label :name %>:
  <%= form.text_field :name %>

  <%= form.label :description %>
  <%= form.text_field :description %>

  <%= form.submit %>
<% end %>

Spec 规格

it "edits an assignment" do
  user = create(:user)
  account = create(:account, users: [user])
  assignment = create(:assignment, account_id: account.id)
  login_user(user)

  visit account_assignments_path
  click_button "edit assignment"

  expect(current_path).to eq(edit_account_assignment_path(assignment))

  fill_in :assignment_name, with: "Edited"
  fill_in :assignment_description, with: "Edited Too"
  click_button "Update Assignment"

  expect(current_path).to eq(account_assignments_path)
  expect(page).to have_content("Successfully Edited The Assignment")
end

error: 错误:

Creating a assignment assignment management edits an assignment
 Failure/Error: click_button "Update Assignment"

 ActionController::RoutingError:
   No route matches [PATCH] "/account/assignments.1"

Change this line: 更改此行:

<%= form_for @assignment, url: account_assignments_path(@assignment) do |form| %> 

to: 至:

<%= form_for @assignment, url: account_assignment_path(@assignment) do |form| %> 

Read about Path and URL Helpers 了解有关Path and URL Helpers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM