【发布时间】:2016-05-07 20:06:54
【问题描述】:
我在编辑帖子时遇到问题。当我单击编辑并且假设将我重定向到要编辑的页面时,我得到一个错误。我有两张桌子,一张是posts,另一张是places。我的places 表中有一个名为post_id 的外键。每个帖子has_one 地方。我知道我在PostController 中使用“place”进行的“编辑”没有被正确调用,因为它可能需要被引用。我该怎么做才能使代码正常工作??
PostsController:
class PostsController < ApplicationController
before_action :authenticate_user!, :except => [:show, :index, :new]
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :owned_post, only: [:edit, :update, :destroy]
def index
@post = Post.new
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = current_user.posts.build
@posts = Post.all
end
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Your post has been created!"
redirect_to root_path
else
flash[:alert] = "Your new post couldn't be created! Please check the form."
render :new
end
end
def find
@place = Place.new
end
def edit
@post = Post.find(params[:id])
@place = Place.find(params[:id])
end
def update
if @post.update(post_params)
flash[:success] = "Post updated."
redirect_to root_path
else
flash.now[:alert] = "Update failed. Please check the form."
render :edit
end
end
def destroy
@post.destroy
flash[:success] = "Your Post has been removed."
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:image, :caption, :latitude, :longitude)
end
def set_post
@post = Post.find(params[:id])
end
def owned_post
unless current_user == @post.user
flash[:alert] = "That post doesn't belong to you!"
redirect_to root_path
end
end
end
我还想在创建新帖子时向此表单添加地点,以便地点和帖子表都填充彼此相关的正确字段。
创建帖子的表单:
<%= form_image_select(@post) %>
<%= simple_form_for @post, html: { multipart: true } do |f| %>
<div class="row">
<div class="col-md-12 text-center">
<%= f.error_notification %>
</div>
</div>
<div class="container-fluid">
<div class="form-group text-center">
<h4>Upload an image (this is required):</h4>
<%= f.input :image, label: false, input_html: { onChange: 'loadFile(event)' } %>
</div>
<%= simple_fields_for place.new do |o| %>
<div class="form-group text-center">
<%= o.input :address, label: false, placeholder: "search", class: 'controls', id: "pac-input" %>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
</div>
<% end %>
<div class="form-group text-center">
<%= f.input :caption, label: false, placeholder: 'Add your caption' %>
</div>
<div class="form-group text-center">
<%= f.button :submit, class: 'btn-success btn-block' %>
</div>
</div>
<% end %>
新的错误日志:
于 2016-05-08 05:59:33 +0900 开始为 ::1 获取“/posts/17/edit” PostsController#edit 处理为 HTML 参数:{"id"=>"17"} 用户负载 (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 16]] Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ?限制 1 [["id", 17]] 用户负载 (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ?限制 1 [["id", 16]] Place Load (0.1ms) SELECT "places".* FROM "places" WHERE "places"."post_id" = ?限制 1 [[“post_id”,17]] 渲染的帖子/_form.html.erb (14.0ms) 在布局/应用程序中渲染的帖子/edit.html.erb (14.9ms) 在 21 毫秒内完成 500 内部服务器错误(ActiveRecord:0.3 毫秒)
ActionView::Template::Error(未定义的局部变量或方法place' for #<#<Class:0x007f92b27c7738>:0x007f92bb24b288>):
17: <h4>Upload an image (this is required):</h4>
18: <%= f.input :image, label: false, input_html: { onChange: 'loadFile(event)' } %>
19: </div>
20: <%= simple_fields_for place.new do |o| %>
21: <div class="form-group text-center">
22: <%= o.input :address, label: false, placeholder: "search", class: 'controls', id: "pac-input" %>
23: <input id="pac-input" class="controls" type="text" placeholder="Search Box">
app/views/posts/_form.html.erb:20:inblock in _app_views_posts__form_html_erb__1067044067393355525_70134077243420'
app/views/posts/_form.html.erb:8:in _app_views_posts__form_html_erb__1067044067393355525_70134077243420'
app/views/posts/edit.html.erb:2:in_app_views_posts_edit_html_erb__4372466127864082923_70134042275460'
【问题讨论】:
-
您正在查找具有相同 ID 的帖子和地点。它们不一定具有相同的 id。此外,根据您的描述,您应该在帖子中包含 place_id,而不是相反。因为帖子只有一个地方,但一个地方可以有很多帖子。
标签: ruby-on-rails database activerecord controller foreign-keys