simple_form不保存(但是类似的形式)

时间:2016-09-22 08:28:29

标签: ruby-on-rails ruby-on-rails-3 simple-form

我正在创建一个具有图像(实际上是文本)的应用程序,这些图像嵌套在项目中浅层嵌套的帖子中。 (请注意,我的图片'实际上是文字,因此您可以将其视为'评论')

我的routes.rb是:

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

其中包含以下路线:

         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

我的模型如下:

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

我的帖子表迁移是:

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

图像表迁移是:

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

我正在使用simple_form为我的项目创建帖子,这非常有效。用于创建帖子的控制器和视图是:

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

这很有效。我想做同样的事情,现在有一个帖子的图像。看看我的路线,我会认为帖子与项目的关系与图片的发布基本相同。所以我使用了类似的代码如下:

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

单击“提交”时,不保存任何内容并呈现新表单。我添加了一个警报,检查实际上它没有保存,警报正在工作并告诉我它没有保存。为什么不能保存?

请记住,对于我的图像&#39;实际上是文本(以评论的方式)。对不起,如果这令人困惑。

任何有关此的帮助将不胜感激。谢谢!

0 个答案:

没有答案