它工作得很好但是现在我出于某种原因出现了这个错误?
class Post < ApplicationRecord
belongs_to :user
geocoded_by :address
after_validation :geocode, if: :address_changed?
has_many :rsvps
has_many :users, through: :rsvps
end
form.html.erb
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date, id: :post_date %>
</div>
<div class="field">
<%= form.label :name %>
<%= form.text_area :name, id: :post_name %>
</div>
<div class="action">
<%= form.label :address %>
<%= form.text_area :address, id: :post_address %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id, id: :post_user_id, value: current_user.id %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description, id: :post_description %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
发布控制器
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
@post = Post.find(params[:id])
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
unless current_user == @post.user
redirect_back fallback_location: root_path, notice: 'User is not owner'
end
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:date, :user_id, :description, :name, :address)
end
load_and_authorize_resource
end
迁移
class AddLatitudeAndLongitudeToPost < ActiveRecord::Migration[5.1]
def change
add_column :posts, :latitude, :float
add_column :posts, :longitude, :float
add_column :posts, :address, :string
end
end
发布迁移
class CreatePosts < ActiveRecord::Migration[5.1]
def change
create_table :posts do |t|
t.datetime :date
t.string :name
t.integer :user_id
t.text :description
t.timestamps
end
end
end
我不确定它会如何自动停止工作,因为实际上没有任何变化。我只是弄清楚为什么谷歌静态地图出错了。但是我没有实现任何更改,所以我不知道可能导致此错误的原因是什么?我将不胜感激,谢谢。
答案 0 :(得分:0)
ActiveModel::UnknownAttributeError
的可能原因是当您尝试分配表中不存在的属性时。
您必须向Post
模型添加一些属性,但必须忘记运行迁移
bundle exec rake db:migrate
运行此命令应解决问题
注意:我可以看到有一个新的迁移AddLatitudeAndLongitudeToPost
,它向posts
表添加了一些列。使用以下命令检查迁移状态:
bundle exec rake db:migrate:status
检查schema.rb