1
votes

I have a nested form with post and place. I want to get both data entries into the database upon submit. I have a foreign key in the place table called post_id. How would i keep them related on submit like the foreign key of post will give the post id and everything will be submitted post and place?? Thank you!!!

Post 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

Form -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 %>

Error Report:

` Started POST "/posts" for ::1 at 2016-05-11 20:14:53 +0900 Processing by PostsController#create as HTML Parameters: {"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\nContent-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"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 16]] Unpermitted parameter: places (0.0ms) begin transaction Command :: file -b --mime '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-1fkkpgg.JPG' Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-noib0w.JPG[0]' 2>/dev/null Command :: 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' Command :: file -b --mime '/var/folders/tb/nnp8bs4x34b4ylvjsgq5g0400000gn/T/6b1516dec860140f099ec2a5d5cbbd6320160511-94657-1rirdao.JPG'

`

Thank you!!

1
Please update your question with full error messagePavan
Iv been working on it and changed some things. im not as much getting an error but my places table isnt populating with the place input box values still. so i edited both forms and added the error message! thanks!!!Michael Lombardo
Unpermitted parameters placesMichael Lombardo

1 Answers

1
votes

In your PostController new method add

 @post = current_user.posts.new
 @places = @post.places.build

And in you Post model add

accepts_nested_attributes_for :places