html - PostsController 中的 NoMethodError#create "creating nested form multi tables"

标签 html ruby-on-rails database input controller

我有一个包含 postplace. 的嵌套表单。我想在提交时将这两个数据条目都输入到数据库中。我在位置表中有一个名为 post_id 的外键。我如何让它们在提交时保持相关,就像帖子的外键会给出帖子 ID 并且所有内容都将提交 postplace?谢谢!!!

后 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/

相关文章:

mysql - 如何查看MySql用户的创建时间?

html - 在一个 div 中居中一个 div

javascript - 使用 jQuery 编辑 CSS 变量的最简单方法是什么?

ruby-on-rails - Rails ActiveJob 从 Controller 启动

javascript - Ruby On Rails before_filter 调用了两次?

ruby-on-rails - 是否有 View 页面中使用的所有 RoR 助手的列表?

php - 如何使用laravel和mysql查看基于日期范围的数据

java - Netezza 流式处理结果集

JavaScript - 使 Canvas 成为全局变量

html - Adsense 每页有多个广告?