【问题标题】:Best_in_place gem not saving text area contentBest_in_place gem 不保存文本区域内容
【发布时间】:2014-03-17 17:40:26
【问题描述】:

所以,我不知道为什么,但是当我在文本框中输入一些文本然后刷新页面时,文本没有保存。我使用了正确的数据库字段(描述),所以我不确定出了什么问题。

_photo.html.erb

<%= best_in_place photo, :description, type: :textarea, nil: 'Enter a Caption' %>

照片架构

create_table "photos", force: true do |t|
    t.string   "file"
    t.integer  "attachable_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "attachable_type"
    t.string   "title"
    t.text     "description"
    t.boolean  "profile_photo",   default: false
  end

  add_index "photos", ["attachable_id"], name: "index_photos_on_attachable_id", using: :btree
  add_index "photos", ["attachable_type"], name: "index_photos_on_attachable_type", using: :btree

为什么不保存?

干杯!

更新:

show.js

$('.modal-title').html('<%= @photo.title %>');
$('.modal-body').html('<%= j(render "modal_photo", photo: @photo) %>');
$('#myModal').modal();

photos_controller(截图):

class PhotosController < ApplicationController
  before_filter :authenticate_user!

  def show
    @photo = current_user.photos.find(params[:id])
  end

【问题讨论】:

  • 请添加控制器的内容和调用_photo.html.erb 部分的视图。
  • 当然 - 我已经用节目和照片控制器相关代码更新了帖子。谢谢!
  • 请添加您的控制器的update 操作。
  • 您使用的是 Rails3 还是 Rails4?
  • Rails 4。让我删除它,对不起!

标签: ruby-on-rails ruby-on-rails-4 best-in-place


【解决方案1】:

这里你需要保存对象而不是单值属性。替换为

<%= best_in_place @photo, :description, type: :input %>

看到这个答案有帮助:best_in_place not saving

【讨论】:

    【解决方案2】:

    我认为你的问题可能出在控制器上:

    class PhotosController < ApplicationController
      respond_to :html, :json # Whatever formats you want to respond
    
      # Other actions...
    
      def update
        @photo = Photo.find(params[:id])
    
        respond_to do |format|
          if @photo.update_attributes(params[:photo])
            format.html { # Whatever you want to do... }
            format.json { respond_with_bip(@photo) }
          else
            format.html { # Whatever you want to do... }
            format.json { respond_with_bip(@photo) }
          end
        end
      end
    

    Best in place 提供了一种实用方法,您应该在控制器中使用该方法,以便使用 :json 格式提供 javascript 端所期望的响应。 More info in the gem's README.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多