jeudi 22 septembre 2016

simple_form not saving (but a similar form is)

I am creating an app that has images (actually text) which are shallow nested in posts which are shallow nested in projects. (Please note that my 'images' are actually text, so you can think of them as 'comments')

My routes.rb is:

devise_for :users
resources :projects, shallow: true do
    resources :posts, shallow: true do
        resources :images
    end
end
root 'projects#index'

Which give the following routes:

         post_images GET    /posts/:post_id/images(.:format)          images#index
                     POST   /posts/:post_id/images(.:format)          images#create
      new_post_image GET    /posts/:post_id/images/new(.:format)      images#new
          edit_image GET    /images/:id/edit(.:format)                images#edit
               image GET    /images/:id(.:format)                     images#show
                     PATCH  /images/:id(.:format)                     images#update
                     PUT    /images/:id(.:format)                     images#update
                     DELETE /images/:id(.:format)                     images#destroy
       project_posts GET    /projects/:project_id/posts(.:format)     posts#index
                     POST   /projects/:project_id/posts(.:format)     posts#create
    new_project_post GET    /projects/:project_id/posts/new(.:format) posts#new
           edit_post GET    /posts/:id/edit(.:format)                 posts#edit
                post GET    /posts/:id(.:format)                      posts#show
                     PATCH  /posts/:id(.:format)                      posts#update
                     PUT    /posts/:id(.:format)                      posts#update
                     DELETE /posts/:id(.:format)                      posts#destroy
            projects GET    /projects(.:format)                       projects#index
                     POST   /projects(.:format)                       projects#create
         new_project GET    /projects/new(.:format)                   projects#new
        edit_project GET    /projects/:id/edit(.:format)              projects#edit
             project GET    /projects/:id(.:format)                   projects#show
                     PATCH  /projects/:id(.:format)                   projects#update
                     PUT    /projects/:id(.:format)                   projects#update
                     DELETE /projects/:id(.:format)                   projects#destroy
                root GET    /                                         projects#index

My models are as follows:

project.rb

class Project < ApplicationRecord
    belongs_to :user
    has_many :posts
    has_many :images
end

post.rb

class Post < ApplicationRecord
  belongs_to :project
  belongs_to :user
  has_many :images
end

image.rb

class Image < ApplicationRecord
  belongs_to :project
  belongs_to :post
  belongs_to :user
end

My post table migration is:

create_table :posts do |t|
  t.string :title
  t.text :content
  t.references :project, foreign_key: true
  t.references :user, foreign_key: true

  t.timestamps
end

And image table migration is:

create_table :images do |t|
  t.text :caption
  t.references :project, foreign_key: true
  t.references :post, foreign_key: true
  t.references :user, foreign_key: true

  t.timestamps
end

I am using a simple_form to create posts for my projects, which is working perfectly. The controller and view for creating posts are:

def create
    @project = Project.find(params[:project_id])
    @post = @project.posts.create(post_params)
    @post.user_id = current_user.id

    if @post.save
        redirect_to post_path(@post)
    else
        render 'new'
    end
end
-------------------------------------------------------
private
    def post_params
        params.require(:post).permit(:title, :content)
    end
end
-------------------------------------------------------
<%= simple_form_for([@project, @project.posts.build]) do |f| %>
    <%= f.input :title, label: "Post Title" %>
    <%= f.input :content, label: "Post Descriptioon" %>
    <%= f.button :submit %>
<% end %>

This works great. I want to do the exact same thing, now with images for a post. Looking at my routes, I would have thought that the relationship of post to project is basically the same as image to post. So I have used a similar code as below:

def create
    @post = Post.find(params[:post_id])
    @image = @post.images.create(image_params)
    @image.user_id = current_user.id

    if @image.save
        flash[:alert] = "Image saved successfully!"
        redirect_to post_path(@post)
    else
        flash[:alert] = "Image not saved!"
        render 'new'
    end
end
-------------------------------------------------------
private
    def image_params
    params.require(:image).permit(:caption)
    end
end
-------------------------------------------------------
<%= simple_form_for([@post, @post.images.build]) do |f| %>
    <%= f.input :caption %>
    <%= f.button :submit %>
<% end %>

When clicking submit, nothing is saved and a new form is rendered. I have added an alert to check that in fact it is not saving and the alert is working and telling me that it has not saved. Why would it not be saving?

Please remember that for this my 'images' are actually text (in the way a comment would be). Sorry if this is confusing.

Any help with this would be appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire