我是rails的新手,无法让我的编辑表单更新属性。
这是我的routes.rb项目和注意事项:
resources :projects do
resources :notes
end
这是我在views / notes / edit.html.erb中的form_for:
<%= form_for [@project, @note] do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, rows: 10, class: "form-control" %>
</div>
<div class="action">
<%= f.submit "Save Note", class: "btn btn-success" %>
</div>
<% end %>
最后这是我的notes_controller.rb:
class NotesController < ApplicationController
before_action :find_note, only: [:show, :edit, :update, :destroy]
def index
@notes = Note.all
end
def new
@project = Project.find(params[:project_id])
@note = @project.notes.new
end
def create
@project = Project.find(params[:project_id])
@note = @project.notes.build(note_params)
@note.project = @project
@note.save ? flash[:notice] = "Note created." : flash[:error] = "Note could not be created, please try again."
redirect_to [current_user, @project]
end
def show
end
def edit
end
def update
if @note.update_attributes(note_params)
@note.save
flash[:notice] = "Note updated."
redirect_to authenticated_root_path
else
flash[:error] = "Could not update note."
render :edit
end
end
def destroy
@note.delete ? flash[:notice] = "Note deleted." : flash[:error] = "Note could not be deleted."
redirect_to user_project_path
end
private
def note_params
params.require(:note).permit(:title, :body)
end
def find_note
@project = Project.find(params[:project_id])
@note = @project.notes.find(params[:id])
end
end
这是我的终端正在输出的内容:
Started GET "/projects/12/notes/13/edit?utf8=%E2%9C%93&_method=patch&authenticity_token=vnIoOSi0ksMNI7uU0aMnBpkTBWi75wy%2BYvbs3RxO5sPIbvFzDA30%2B32dJxurdcsiu9zsIpuDCR%2FARUamBRrYmg%3D%3D¬e%5Btitle%5D=Transporter+Materials¬e%5Bbody%5D=Star+dust%2C+data&commit=Save+Note" for 127.0.0.1 at 2015-05-25 08:38:16 -0700
Processing by NotesController#edit as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"vnIoOSi0ksMNI7uU0aMnBpkTBWi75wy+Yvbs3RxO5sPIbvFzDA30+32dJxurdcsiu9zsIpuDCR/ARUamBRrYmg==", "note"=>{"title"=>"Transporter Materials", "body"=>"Star dust, data"}, "commit"=>"Save Note", "project_id"=>"12", "id"=>"13"}
Project Load (0.2ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1 [["id", 12]]
Note Load (0.2ms) SELECT "notes".* FROM "notes" WHERE "notes"."project_id" = ? AND "notes"."id" = ? LIMIT 1 [["project_id", 12], ["id", 13]]
Rendered notes/edit.html.erb within layouts/application (3.4ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 200 OK in 328ms (Views: 296.4ms | ActiveRecord: 2.3ms)
提前感谢任何建议。
答案 0 :(得分:0)
如果您使用@project.notes.build(note_params)
。
创建操作
@note.project = @project
并在更新中
如果您使用的是@note.update_attributes(note_params)
这不是必需的@note.save
并且最好尝试使用@note.update_attributes!(note_params)
它会告诉你为什么它无法保存。
<%= form_for [@project, @note], :url => project_note_path(@project,@note), :method => :put do |f| %>