简体   繁体   中英

Trying to update a record - Rails

I'm simply trying to update a name and description on the assignment modal. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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