可以创建,但不能更新
Able to create, but can not update
可以创建,但不能更新。
main.rb
post '/create' do
o = Organization.new(
title: params[:organization][:title],
body: params[:organization][:body],
location: [params[:organization][:longtitude], params[:organization][:latitude]]
)
if o.save
redirect '/'
else
flash[:error] = "Error saving a organization document."
end
end
get '/edit/:id' do |id|
@o = Organization.find(id)
slim :edit
end
put '/update/:id' do
@o = Organization.find(params[:id])
@o.update_attributes(title: params[:organization][:title],
body: params[:organization][:body],
location: [params[:organization][:longtitude], params[:organization] [:latitude]])
slim :show
end
edit.slim
h2 Edit
form#organizationForm action="/update/#{@o.id}" method="POST"
input type="hidden" name="_method" value="PUT"
|title
input type="text" name="organization[title]" value="#{@o.title}"
|body
input type="text" name="organization[body]" value="#{@o.body}"
|longtitude
input type="text" name="organization[longtitude]" value="#{@o.location[0]}"
|latitude
input type="text" name="organization[latitude]" value="#{@o.location[1]}"
input type="submit" name="submit" value="Update"
organization.rb
class Organization
include Mongoid::Document
include Mongoid::Geospatial
field :title, type: String
field :body, type: String
# define fields
field :location, type: Point, spatial: true
field :route, type: LineString
field :area, type: Polygon
field :around, type: Circle
end
错误消息,
Mongoid::Errors::UnknownAttribute at /update/55a6781373696454b5000000
Problem: Attempted to set a value for 'longtitude' which is not allowed on the model Organization. Summary: Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call Organization#longtitude= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError. Resolution: You can include Mongoid::Attributes::Dynamic if you expect to be writing values for undefined fields often.
file: processing.rb location: process_attribute line: 96
我认为不需要包含 Mongoid::Attributes::Dynamic
,因为可以创建。
只是我忘记了 运行 更改 main.rb 代码后的程序。
代码没问题。
可以创建,但不能更新。
main.rb
post '/create' do
o = Organization.new(
title: params[:organization][:title],
body: params[:organization][:body],
location: [params[:organization][:longtitude], params[:organization][:latitude]]
)
if o.save
redirect '/'
else
flash[:error] = "Error saving a organization document."
end
end
get '/edit/:id' do |id|
@o = Organization.find(id)
slim :edit
end
put '/update/:id' do
@o = Organization.find(params[:id])
@o.update_attributes(title: params[:organization][:title],
body: params[:organization][:body],
location: [params[:organization][:longtitude], params[:organization] [:latitude]])
slim :show
end
edit.slim
h2 Edit
form#organizationForm action="/update/#{@o.id}" method="POST"
input type="hidden" name="_method" value="PUT"
|title
input type="text" name="organization[title]" value="#{@o.title}"
|body
input type="text" name="organization[body]" value="#{@o.body}"
|longtitude
input type="text" name="organization[longtitude]" value="#{@o.location[0]}"
|latitude
input type="text" name="organization[latitude]" value="#{@o.location[1]}"
input type="submit" name="submit" value="Update"
organization.rb
class Organization
include Mongoid::Document
include Mongoid::Geospatial
field :title, type: String
field :body, type: String
# define fields
field :location, type: Point, spatial: true
field :route, type: LineString
field :area, type: Polygon
field :around, type: Circle
end
错误消息,
Mongoid::Errors::UnknownAttribute at /update/55a6781373696454b5000000 Problem: Attempted to set a value for 'longtitude' which is not allowed on the model Organization. Summary: Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call Organization#longtitude= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError. Resolution: You can include Mongoid::Attributes::Dynamic if you expect to be writing values for undefined fields often. file: processing.rb location: process_attribute line: 96
我认为不需要包含 Mongoid::Attributes::Dynamic
,因为可以创建。
只是我忘记了 运行 更改 main.rb 代码后的程序。
代码没问题。