SeekingTruth SeekingTruth - 21 days ago 6
Javascript Question

Turbolinks or some other rails js file that I might be missing?

So I have ran into this issue where:

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


Clicking submit in the form above does nothing. I have to refresh the page, fill out the form and then and only then does clicking create work.

The Controller file:

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


Thats the controller, The new and the create action all work properly. That is when the form actually submits. So I thought I was missing a rails gem so heres my gem file:

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


Issue: You have to refresh the form, refill it in, then clicking create works. why?

Additional Notes: There is no custom javascript that would be interacting with this form.

Answer

Turbolinks (as I understand it) uses javascript to grab the body of each page and then populate the page without reloading, so javascripts that you have added might not get loaded correctly.

You really don't need turbolinks for your app to run, so try removing it by deleting the line in your app/assets/javsascripts/application.js file and your forms should work just fine.