Ruby on Rails:在共享文件夹上呈现表单

时间:2018-02-19 16:25:35

标签: ruby-on-rails ruby ruby-on-rails-4 render

我想将表单放在共享文件夹上以便干掉代码并为[:admin,:posts]和:posts呈现此表单。 所以我创建了一个文件夹并将表单放在app/views/shared/_form.html.slim

- if params[:admin][:posts]
  post = [:admin, @post]
- if params[:posts]
  post = @post

= simple_form_for(post, :html => { :multipart => true }) do |f|
   = f.association :categories, label: "Select the Categories:", as: :check_boxes , collection: @categories.map{|c| [c.name, c.id]}, include_hidden: false
   = f.input :title
   = f.input :subtitle
   - if @post.attachment.present?
     .attachment
       p
         = image_tag(@post.attachment.thumb.url, alt: 'Image', class: "img-responsive img-thumbnail")
         = f.check_box :remove_attachment
         | Remove image
         br
          .text-center
            small
              sample
                = "File_size #{number_to_human_size(@post.attachment.size)}"
   = f.input :attachment, as: :file, label: "File"
   = f.input :attachment_cache, as: :hidden
   = f.input :remote_attachment_url, label: "Enter URL to an image"
   = f.input :content, size: "150x150"

   = f.button :submit, class: 'btn-primary'

然后

app/views/admin/post/new.html.slimapp/views/posts/edit.html.slim

我添加了渲染:

== render 'shared/form', post: @post

所以我试着跑,我有这个错误:

ActionView::Template::Error (undefined method `[]' for nil:NilClass):
    1: - if params[:admin][:posts]
    2:   post = [:admin, @post]
    3: - if params[:posts]
    4:   post = @post

这只是我的想法。在这种情况下可以这样做或忘记这样做吗?

路线:

rake routes | grep post
Running via Spring preloader in process 2187
             admin_posts POST   /admin/posts(.:format)                   admin/posts#create
          new_admin_post GET    /admin/posts/new(.:format)               admin/posts#new
              admin_post DELETE /admin/posts/:id(.:format)               admin/posts#destroy
nullify_posts_admin_user PATCH  /admin/users/:id/nullify_posts(.:format) admin/users#nullify_posts
                    root GET    /                                        posts#index
         published_posts GET    /posts/published(.:format)               posts#published
             draft_posts GET    /posts/draft(.:format)                   posts#draft
            recent_posts GET    /posts/recent(.:format)                  posts#recent
            publish_post PATCH  /posts/:id/publish(.:format)             posts#publish
          unpublish_post PATCH  /posts/:id/unpublish(.:format)           posts#unpublish
                   posts GET    /posts(.:format)                         posts#index
               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

2 个答案:

答案 0 :(得分:2)

我看到你要做的是弄清楚你是在管理部分还是标准的帖子部分,然后改变表格的网址。

这是一种非标准的做法。 一般来说,我所看到的是只有表单的主体在共享表单部分,但操作保存在外部部分,例如:

应用程序/视图/管理/交/ new.html.slim:

= simple_form_for(post, :html => { :multipart => true }) do |f|
  == render 'shared/post_form', f: f, post: post

应用程序/视图/共享/ _post_form.html.slim:

   = f.association :categories, label: "Select the Categories:", as: :check_boxes , collection: @categories.map{|c| [c.name, c.id]}, include_hidden: false
   = f.input :title
   = f.input :subtitle
   - if post.attachment.present?
     .attachment
       p
         = image_tag(post.attachment.thumb.url, alt: 'Image', class: "img-responsive img-thumbnail")
         = f.check_box :remove_attachment
         | Remove image
         br
          .text-center
            small
              sample
                = "File_size #{number_to_human_size(post.attachment.size)}"
   = f.input :attachment, as: :file, label: "File"
   = f.input :attachment_cache, as: :hidden
   = f.input :remote_attachment_url, label: "Enter URL to an image"
   = f.input :content, size: "150x150"

   = f.button :submit, class: 'btn-primary'

注意:如果您将post作为局部变量传递到表单中,则应将其与post而不是@post一起使用(因为@post完全忽略了本地变量传入并返回到来自控制器的任何内容,在这种情况下,为什么要麻烦传入局部变量?) 您也可能永远不会在模板中调用另一个变量post ...因为它会覆盖旧的post变量,然后就会消失。将它命名为不同的东西,例如在这种情况下,如果你真的想按照原样使用模板,你可以称之为post_url_params

答案 1 :(得分:0)

因此,按照@Taryn East的想法,我把它放在了工作中:

应用程序/视图/管理/帖/ new.html.slim

= title("New Post")
.header
  h1 New Post

== render 'form', post: @post

应用程序/视图/管理/帖/ edit.html.slim

= title("Edit Post", @post.title)

.header
  h1 Edit Post

== render "form", post: @post

应用程序/视图/管理/帖/ _form.html.slim

= simple_form_for([:admin, post], :html => { :multipart => true }) do |f|
  == render "shared/post_form", f: f, post: post

应用程序/视图/帖/ _form.html.slim

= simple_form_for(post, :html => { :multipart => true }) do |f|
  == render "shared/post_form", f: f, post: post

应用程序/共享/ _post_form.html.slim

= f.association :categories, label: "Select the Categories:", as: :check_boxes , collection: @categories.map{|c| [c.name, c.id]}, include_hidden: false
= f.input :title
= f.input :subtitle
- if post.attachment.present?
  .attachment
    p
      = image_tag(post.attachment.thumb.url, alt: 'Image', class: "img-responsive img-thumbnail")
      = f.check_box :remove_attachment
      | Remove image
      br
       .text-center
         small
           sample
             = "File_size #{number_to_human_size(post.attachment.size)}"
= f.input :attachment, as: :file, label: "File"
= f.input :attachment_cache, as: :hidden
= f.input :remote_attachment_url, label: "Enter URL to an image"
= f.input :content, input_html: { rows: '15' }

= f.button :submit, class: 'btn-primary'