Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails5 tinymce rails图像上载/上载的图片未显示在编辑页面中_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Rails5 tinymce rails图像上载/上载的图片未显示在编辑页面中

Ruby on rails Rails5 tinymce rails图像上载/上载的图片未显示在编辑页面中,ruby-on-rails,ruby,Ruby On Rails,Ruby,嗨,我正在用博客模型和用户模型制作博客网站。我使用carrierwave、TinyMCE、TinyMCE rails图像上传到WISYWIG的achive博客。基本上我是按照这个网站上传开发中的图片的: 在我创建新的博客文章、上传图片和文本后,我可以在show页面(blogs/show.html.erb)中看到文本和图片。然而,当我尝试移动到编辑页面(blogs/edit.html.erb)时,我只能看到文本(上传的图片没有显示)。我想在编辑页面中显示图片和文本,以便我可以正确编辑博客页面。我怎

嗨,我正在用博客模型和用户模型制作博客网站。我使用carrierwave、TinyMCE、TinyMCE rails图像上传到WISYWIG的achive博客。基本上我是按照这个网站上传开发中的图片的:

在我创建新的博客文章、上传图片和文本后,我可以在show页面(blogs/show.html.erb)中看到文本和图片。然而,当我尝试移动到编辑页面(blogs/edit.html.erb)时,我只能看到文本(上传的图片没有显示)。我想在编辑页面中显示图片和文本,以便我可以正确编辑博客页面。我怎样才能解决这个问题

*我删除了Application.js中的TurboLink

Blog.rb

class Blog < ApplicationRecord 
   mount_uploader :file, ImageUploader
   validates :text, presence: true, length: { maximum:140}
   validates :title, presence: true, length: { maximum: 20}
end
Rails.application.routes.draw do
 resources :blogs
 post '/tinymce_assets', to: 'blogs#upload_image'
end
<%= render 'form', blog: @blog %>
<%= link_to 'Back', blogs_path %>
<%= form_for(blog) do |f| %>
   <%= f.label :title %>
   <%= f.text_field :title %>

   <%= f.label :text %>
   <%= f.text_area :text, :class => "tinymce", :rows => 100,
   :cols => 120 %>
   <%= tinymce :text_css => asset_path('application.css')%>
   <%= f.submit %>
<% end %>
  <%= @blog.title %>

  <%= sanitize @blog.text, tags: %w(h1 h2 h3 h4 h5 h6 ul ol li p a img 
  table tr td em br strong),  attributes:  %w(id class href src) %>

  <%= link_to 'Edit', edit_blog_path(@blog) %>
  <%= link_to 'Back', blogs_path %>
class BlogsController < ApplicationController

   respond_to :json

   def new
     @blog = Blog.new
   end

   def create
     @blog = Blog.new(blog_params)

     if @blog.save
     redirect_to @blog
     else
     render 'new'
     end
   end

   def show
     @blog = Blog.find(params[:id])
   end

   def index
     @blogs = Blog.all
   end

   def edit
     @blog = Blog.find(params[:id])
   end

   def update
     @blog = Blog.find(params[:id])
     if @blog.update(blog_params)
     redirect_to @blog
     else
     render 'edit'
     end
   end

   def destroy
     @blog = Blog.find(params[:id])
     @blog.destroy
     redirect_to blogs_path
   end

   def upload_image 
     image = Blog.create params.permit(:file, :alt, :hint )
      render json: {
        image: {
          url: image.file.url
        }
      }, content_type: "text/html"
    end

    private

    def blog_params
      params.require(:blog).permit(:title, :text, :file, :hint, :alt)
    end
end
class ImageUploader < CarrierWave::Uploader::Base

   storage :file

   def store_dir
   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end

   def extension_white_list
   %w(jpg jpeg gif png)
   end
end
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require tinymce
<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title))%></title> 
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-
     turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 
     'reload' %>
    <%= render 'layouts/shim' %>
   </head>
  <body>
   <%= render 'layouts/header' %>
   <%= tinymce_assets %> 
   <div class="container">
   <%= yield %>
   <%= render 'layouts/footer' %>
   </div>
   <%= debug(params) if Rails.env.development? %> 
  </body>
</html>
博客/edit.html.erb

class Blog < ApplicationRecord 
   mount_uploader :file, ImageUploader
   validates :text, presence: true, length: { maximum:140}
   validates :title, presence: true, length: { maximum: 20}
end
Rails.application.routes.draw do
 resources :blogs
 post '/tinymce_assets', to: 'blogs#upload_image'
end
<%= render 'form', blog: @blog %>
<%= link_to 'Back', blogs_path %>
<%= form_for(blog) do |f| %>
   <%= f.label :title %>
   <%= f.text_field :title %>

   <%= f.label :text %>
   <%= f.text_area :text, :class => "tinymce", :rows => 100,
   :cols => 120 %>
   <%= tinymce :text_css => asset_path('application.css')%>
   <%= f.submit %>
<% end %>
  <%= @blog.title %>

  <%= sanitize @blog.text, tags: %w(h1 h2 h3 h4 h5 h6 ul ol li p a img 
  table tr td em br strong),  attributes:  %w(id class href src) %>

  <%= link_to 'Edit', edit_blog_path(@blog) %>
  <%= link_to 'Back', blogs_path %>
class BlogsController < ApplicationController

   respond_to :json

   def new
     @blog = Blog.new
   end

   def create
     @blog = Blog.new(blog_params)

     if @blog.save
     redirect_to @blog
     else
     render 'new'
     end
   end

   def show
     @blog = Blog.find(params[:id])
   end

   def index
     @blogs = Blog.all
   end

   def edit
     @blog = Blog.find(params[:id])
   end

   def update
     @blog = Blog.find(params[:id])
     if @blog.update(blog_params)
     redirect_to @blog
     else
     render 'edit'
     end
   end

   def destroy
     @blog = Blog.find(params[:id])
     @blog.destroy
     redirect_to blogs_path
   end

   def upload_image 
     image = Blog.create params.permit(:file, :alt, :hint )
      render json: {
        image: {
          url: image.file.url
        }
      }, content_type: "text/html"
    end

    private

    def blog_params
      params.require(:blog).permit(:title, :text, :file, :hint, :alt)
    end
end
class ImageUploader < CarrierWave::Uploader::Base

   storage :file

   def store_dir
   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end

   def extension_white_list
   %w(jpg jpeg gif png)
   end
end
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require tinymce
<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title))%></title> 
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-
     turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 
     'reload' %>
    <%= render 'layouts/shim' %>
   </head>
  <body>
   <%= render 'layouts/header' %>
   <%= tinymce_assets %> 
   <div class="container">
   <%= yield %>
   <%= render 'layouts/footer' %>
   </div>
   <%= debug(params) if Rails.env.development? %> 
  </body>
</html>
布局/Application.html.erb

class Blog < ApplicationRecord 
   mount_uploader :file, ImageUploader
   validates :text, presence: true, length: { maximum:140}
   validates :title, presence: true, length: { maximum: 20}
end
Rails.application.routes.draw do
 resources :blogs
 post '/tinymce_assets', to: 'blogs#upload_image'
end
<%= render 'form', blog: @blog %>
<%= link_to 'Back', blogs_path %>
<%= form_for(blog) do |f| %>
   <%= f.label :title %>
   <%= f.text_field :title %>

   <%= f.label :text %>
   <%= f.text_area :text, :class => "tinymce", :rows => 100,
   :cols => 120 %>
   <%= tinymce :text_css => asset_path('application.css')%>
   <%= f.submit %>
<% end %>
  <%= @blog.title %>

  <%= sanitize @blog.text, tags: %w(h1 h2 h3 h4 h5 h6 ul ol li p a img 
  table tr td em br strong),  attributes:  %w(id class href src) %>

  <%= link_to 'Edit', edit_blog_path(@blog) %>
  <%= link_to 'Back', blogs_path %>
class BlogsController < ApplicationController

   respond_to :json

   def new
     @blog = Blog.new
   end

   def create
     @blog = Blog.new(blog_params)

     if @blog.save
     redirect_to @blog
     else
     render 'new'
     end
   end

   def show
     @blog = Blog.find(params[:id])
   end

   def index
     @blogs = Blog.all
   end

   def edit
     @blog = Blog.find(params[:id])
   end

   def update
     @blog = Blog.find(params[:id])
     if @blog.update(blog_params)
     redirect_to @blog
     else
     render 'edit'
     end
   end

   def destroy
     @blog = Blog.find(params[:id])
     @blog.destroy
     redirect_to blogs_path
   end

   def upload_image 
     image = Blog.create params.permit(:file, :alt, :hint )
      render json: {
        image: {
          url: image.file.url
        }
      }, content_type: "text/html"
    end

    private

    def blog_params
      params.require(:blog).permit(:title, :text, :file, :hint, :alt)
    end
end
class ImageUploader < CarrierWave::Uploader::Base

   storage :file

   def store_dir
   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end

   def extension_white_list
   %w(jpg jpeg gif png)
   end
end
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require tinymce
<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title))%></title> 
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-
     turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 
     'reload' %>
    <%= render 'layouts/shim' %>
   </head>
  <body>
   <%= render 'layouts/header' %>
   <%= tinymce_assets %> 
   <div class="container">
   <%= yield %>
   <%= render 'layouts/footer' %>
   </div>
   <%= debug(params) if Rails.env.development? %> 
  </body>
</html>

我解决了这个问题。这是因为相对url设置为true。 在我的视图(_form.html.erb)中,我添加了false%>这解决了问题。谢谢