Turbolinks或其他一些我可能会丢失的rails js文件?

时间:2014-09-11 18:05:01

标签: javascript ruby-on-rails ruby

所以我遇到了这个问题:

<div class="container">
  <%= form_for @blog, :url => user_blogs_path(current_user.id), :html => {:class => 'form-horizontal'} do |f| %>
    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
        <h1>Create Your Blog</h1>
      </div>
    </div>

    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-6">
        <%= render :partial => 'form_errors', :object => @blog %>
      </div>
    </div>
    <%= render :partial => 'blog_form', :locals => {:f => f} %>
    <div class="form-group">
      <hr>
      <div class="col-sm-offset-2 col-sm-10">
        <%= f.submit "Create", :class => 'btn btn-primary' %>
      </div>
    </div>
  <% end %>
</div>

点击上面表单中的提交不会做任何事情。我必须刷新页面,填写表单,然后才点击创建工作。

Controller文件:

class BlogsController < ApplicationController
  before_action :authenticate_user!
  before_action :redirect_not_blog_owner, except: [:index, :new, :create, :show]

  def new
    @blog = Blog.new
  end

  def index
    @blogs = User.find_by!(id: params[:user_id]).blogs.all()
  end

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

  def create
    @blog = Blog.new(blog_create_params)
    @blog.user_name = current_user.user_name

    if @blog.save && current_user.add_role_for_blog(@blog, Role.find_by(role: 'Blog-Owner'))
      flash[:success] = 'New Blog Created!'
      redirect_to user_blogs_path
    else
      render :new
    end
  end

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

  def update
    @blog = Blog.find_by!(id: params[:id])
    user = User.find_by!(id: params[:user_id])

    @blog.update_attributes(blog_update_params)
    if @blog.save
      flash[:success] = 'Updated Blog'
      redirect_to user_blog_path(user.id, @blog.id)
    else
      render :edit
    end
  end

  def destroy
    blog = Blog.find_by!(id: params[:id])
    user = User.find_by!(id: params[:user_id])

    if user && blog
      user.delete_role_for_blog(blog)
      blog.destroy!
      redirect_to user_blogs_path
      flash[:success] = 'Deleted Blog!'
    end
  end

  def blog_create_params
    params.require(:blog).permit(:title, :blog_description)
  end

  def blog_update_params
    params.require(:blog).permit(:title, :blog_description)
  end

end

这就是控制器,新操作和创建操作都能正常工作。那是表格实际提交的时候。所以我以为我错过了一个rails gem所以继承我的宝石文件:

source 'https://rubygems.org'

gem 'rails', '4.1.5'
gem 'pg'
gem 'sass-rails', '~> 4.0.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
gem 'therubyracer'
gem "bower-rails", "~> 0.7.2"
gem 'kaminari'

gem 'rspec-rails', '2.99.0', group: [:test, :development]
gem 'letter_opener', group: [:development]

gem 'promiscuous', :git => 'git@github.com:promiscuous-io/promiscuous.git'
gem "active_model_serializers", '0.8.1'
gem 'ancestry', '2.1.0'
gem 'rack-attack'
gem 'backbone-on-rails'
gem 'react-rails',  :git => 'git@github.com:reactjs/react-rails.git', :tag => '0.11'
gem 'showdown-rails'
gem 'compass-rails'
gem 'pry', require: 'pry'
gem 'pry-rails'
gem 'twitter'
gem 'figaro'
gem 'rack-cors', require: 'rack/cors', :git => 'git@github.com:AdamKyle/Rack-Cors.git'

#Assets
gem "font-awesome-rails"

group :development do
  gem 'capistrano', '~> 2.15'
  gem 'rvm-capistrano', '~> 1.4'
end

group :test do
  gem 'database_cleaner'
  gem 'guard-rspec'
  gem 'factory_girl_rails'
  gem 'simplecov', '0.8.2'
  gem 'json_spec', '1.1.2'
  gem 'capybara'
end

问题:您必须刷新表单,重新填充表单,然后单击“创建工作”。为什么?

附加说明:没有可与此表单进行交互的自定义​​JavaScript。

1 个答案:

答案 0 :(得分:0)

Turbolinks(据我所知)使用javascript来抓取每个页面的主体然后填充页面而不重新加载,所以你添加的javascripts可能无法正确加载。

你真的不需要使用turbolink来运行你的应用程序,所以尝试通过删除app / assets / javsascripts / application.js文件中的行来删除它,你的表单应该可以正常工作。