- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个包含 post
和 place.
的嵌套表单。我想在提交时将这两个数据条目都输入到数据库中。我在位置表中有一个名为 post_id
的外键。我如何让它们在提交时保持相关,就像帖子的外键会给出帖子 ID 并且所有内容都将提交 post
和 place
?谢谢!!!
后 Controller :
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.new
@post.places = Place.new
end
def create
@post = current_user.posts.create(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
@place = @post.place;
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(:place_id, :image, :caption, :places_attributes => [:id, :post_id, :city, :country, :address, :streetnumber, :street, :state])
end
def place_params
params.require(:place).permit(:country, :city)
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
表单 -home.html:
<%= simple_form_for @post, html: { multipart: true } do |f| %>
<div class="row">
<div class="col-md-12">
<%= f.error_notification %>
</div>
</div>
<div class="container-fluid">
<div class="dont">
<h4>Upload an image (this is required):</h4>
<%= f.input :image, label: false, input_html: { onChange: 'loadFile(event)' } %>
</div>
<%= f.simple_fields_for :places do |o| %>
<div class="dont">
<%= o.input :address, label: false, placeholder: "search", class: 'controls',:input_html =>{:id => 'pac-input'} %>
<input id="latitude" name="latitude" value=" @#{latitude} " type="text">
<input id="longitude" name="longitude" value=" @#{longitude} " type="text">
<input id="action" name="action" value"test" type="hidden">
<%= o.input :streetnumber, label: false, class: 'controls',:input_html =>{:id => 'streetnumber'},:as => :hidden %>
<%= o.input :street, label: false, class: 'controls',:input_html =>{:id => 'street'}, :as => :hidden %>
<%= o.input :city, label: false, class: 'controls',:input_html =>{:id => 'city'}, :as => :hidden %>
<%= o.input :state, label: false, class: 'controls',:input_html =>{:id => 'state'}, :as => :hidden %>
<%= o.input :country, label: false, class: 'controls',:input_html =>{:id => 'country'}, :as => :text, :as => :hidden %>
</div>
<% end %>
<div class="dont">
<%= f.input :caption, label: false, placeholder: 'Add your caption' %>
</div>
<div class="dont">
<%= f.button :submit, class: 'btn-success btn-block' %>
</div>
</div>
<% end %>
错误报告:
`在 2016-05-11 20:14:53 +0900 开始为::1 发布“/posts”由 PostsController 处理#create as HTML 参数:{"utf8"=>"✓", "authenticity_token"=>"cJP0UGjMFD244w2D4DqLC+VV86wO/i+ScOxbKUowMKSxx9JnwOQ77EFeyLAZY1E3/YSQnOOZ2nIbu/Dc9mok6w==", "post"=>{"image"=>#, @original_filename="IMG_0645.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"IMG_0645.JPG\"\r\n内容-Type: image/jpeg\r\n">, "places"=>{"address"=>"Unnamed Road, Mokro, Bosnia and Herzegovina", "streetnumber"=>"undefined", "street"=>"Unnamed Road", "city"=>"Mokro", "state"=>"Republika Srpska", "country"=>"Bosnia and Herzegovina"}, "caption"=>""}, "latitude"=>"43.88522775121559", "longitude"=>"18.6328125", "commit"=>"Create Post"} 用户负载 (0.1ms) SELECT "users".* FROM "users"WHERE "users"."id"= ? ORDER BY "users"."id"ASC LIMIT 1 [["id", 16]]不允许的参数:地方 (0.0ms) 开始交易命令::file -b --mime '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-1fkkpgg.JPG'命令::识别格式 '%wx%h,%[exif:orientation]' '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-noib0w.JPG[0]' 2>/dev/null命令::identify -format %m '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-noib0w.JPG[0]'Command::convert '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-noib0w.JPG[0]' -auto-orient -resize "640"'/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/74ec5b80c34294faf295a1917f07ae9720160511- 94657-1rtm7lc'命令::file -b --mime '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-1rirdao.JPG'
`
谢谢!!
最佳答案
在你的 PostController 新方法中添加
@post = current_user.posts.new
@places = @post.places.build
然后在你的 Post 模型中添加
accepts_nested_attributes_for :places
关于html - PostsController 中的 NoMethodError#create "creating nested form multi tables",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37155567/
上下文: 我从存储库中提取了最新的代码,并尝试确保我将要推送的更改能够与该版本的代码一起使用。这是一个 Ruby on Rails 应用程序。另外值得注意的是,在运行我从网络上提取的主应用程序时,不会
让我们考虑以下代码: class Try CONST = xxx("42") private_class_method def self.xxx(str) str.to_i end end
我一直在寻找解决方案,但最相关的答案似乎规定使用 self. ,我已经在做;我无法用我可以找到我做错了什么的方式来表达我的搜索。我正在定义一个类,如下所示,但是当我尝试创建一个实例时,我得到了 NoM
我向 String 类添加了一个额外的方法。我想稍后使用此方法,但出现 no Method 错误。 class String def as_file_full_path NSSear
我尝试使用 ruby on Rails 请求数据库中的日期时间条目。这在 Linux 环境中工作得很好,但在我的 Windows 环境中我得到了 NoMethodError。调试时,我发现将日期时
我正在努力学习 ruby。我卡在第 26 课了。Here是文件的链接。这是错误所在的代码片段: five = 10 - 2 + 3 - 6 puts "This should be five: #
我不想在某个地方引发 NoMethodError 而只是针对某个类(例如 NilClass)。 例如 begin maybe_nil_maybe_not_nil = nil maybe_nil
我有一个正在编写的 sinatra 应用程序。我正在尝试编写一个页面来显示存储在数据库中的各个模式。使用 datamapper 我创建了一个类: require 'dm-core' require '
不确定是否有很多人熟悉 Ruby 的脚手架扩展,但我查看了他们的文档、论坛,甚至 Heroku 测试站点的源代码,但没有找到答案。 我制作了一个基本的 Sinatra 应用程序并按照 RDoc 的说明
我正在创建一个 Die 类,它具有输出骰子编号的功能。它已被命名为 printout 和 output,但由于 NoMethodError,函数继续失败。这是我的代码: class Die def
Whole source code here 我觉得我的程序流程有逻辑错误,返回NoMethodError 首先,一段导致错误的代码。 " /> #Error Text NoMethodError a
我正在尝试从我的一个规范助手中的模块访问方法 我将模块包含在测试助手中 module Support class RestHelper include Rest::Rest def
我在学习 Ruby 的时候正在做一个简单的 Pi Generator,但是我在 RubyMine 6.3.3 上一直遇到 NoMethodError,所以我决定用尽可能简单的方式创建一个新项目和新类,
我有一个这样的模块: module Prober def probe_invoke(type, data = {}) p = Probe.new({:probe_type => type.
我有一个 ruby 程序,它在特定的命名空间中创建部署、服务和 ing。当我想创建一个命名空间时,它会提示 NoMethodError。 这是代码: namespace = Kubeclient:
context 'with event_type is available create event' do let(:event_type) { EventType.where( name: '
我在 Ruby 中发现了一个奇怪的问题,我不太确定这是一个问题还是最近 Ruby 版本中引入的一个功能。 基本上当我们调用一个未定义的方法时,我们会得到一个 undefined method Ruby
我有以下 Ruby 代码,其中 BigClass 的每个实例创建一个具有 BigClass 实例的数组(直到最大深度)。 class BigClass # Increase this depend
我正在尝试 ror 教程,我遇到了以下代码行: index.html.erb: @players %> _player.html.erb: players_controller.rb: de
当我的 DeltaSynWorker 运行时,我收到 NoMethodErrors。这发生在作为当前工作应用程序的修订版构建的应用程序中。我不是最初的程序员,我是从 Java 背景开始的(我提到这一点
我是一名优秀的程序员,十分优秀!