I am testing creating a link_to with :remote=> true to get the ajax remote link handling.
<%= link_to "Get Books", "/books", :remote=> true %>
The helper for rails3 resolves the code correctly in the view and I get as source:
Get Books
And I have the default javascript included:
<%= javascript_include_tag :defaults %>
And the source head resolves as:
<script src="/javascripts/jquery.js?1302296105" type="text/javascript"></script>
<script src="/javascripts/rails.js?1302139751" type="text/javascript"></script>
<script src="/javascripts/application.js?1305999509" type="text/javascript"></script>
And in the controller I have:
respond_to :html, :json
However when I click the link it loads as regular link, not ajax and on the server log:
Processing by BooksController#index as HTML
What am I missing?
Make sure that you have installed the jQuery UJS Plugin:
Add this to your gemfile:
gem 'jquery-rails', '>= 1.0.3'
And then run:
bundle install
rails g jquery:install
I had a similar issue: I expected a remote link to be processed as JS request but Rails somehow was processing it as HTML.
The problem is we don't specify a format and then Rails could choose any of the formats depending on the server configuration.
TL;DR; Specify the request type when using remote:
<%= link_to "Get Books", "/books", data: {remote: true, type: "script"} %>
Related
I have following html code in a view whose layout is false.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="manifest" href="manifest.json">
Jquery is being loaded but not the manifest.json, when I view page source and look at them I get this error:
No route matches [GET] "/manifest.json"
Even though I have included manifest.json inside the javascripts folder inside assets folder.
I also tried
<%= javascript_include_tag "manifest.json" %>
but that didn't work either..and that get routes error
I tried
<%= manifest_link_tag "manifest" %>
Again that didn't work out, gave error :
undefined method `manifest_link_tag'
I also added the manifest.json inside initializers/assets.rb but still no luck!!
I think you need to include it like this:
<link rel="manifest" href="<%= asset_path 'manifest.json' %>">
My solution is:
= tag(:link, rel: 'manifest', href: some_path)
or
= tag(:link, rel: 'manifest', href: asset_path('manifest.json'))
I cannot get the galetahub ckeditor gem to work with Rails 4 for me. I searched for any problems online but cannot find any. I'm following the instructions exactly.
I include gem "ckeditor" in my Gemfile
I include gem "carrierwave" and gem "mini_magick"
I run rails generate ckeditor:install --orm=active_record --backend=carrierwave
I run rake db:migrate
Inside application.rb I include config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
Inside routes.rb I have mount Ckeditor::Engine => '/ckeditor'
I'm using SimpleForm so I paste the following ERB <%= f.input :description, as: :ckeditor %> in my view.
And I think that's it. But my text area does not convert to a CKeditor area for some reason.
STEP 1: Add gem 'paperclip' and gem "ckeditor" in your gemfile.
STEP 2: Bundle Install.
STEP 3: rails generate ckeditor:install --orm=active_record --backend=paperclip
STEP 4: Place config.autoload_paths += %W(#{config.root}/app/models/ckeditor) in application.rb
STEP 5: Place mount Ckeditor::Engine => "/ckeditor" if not present in routes.rb already and run db:migrate
STEP 6: Open application.html.erb and place this <%= javascript_include_tag 'ckeditor/ckeditor.js' %> in header.
STEP 7: Place this in footer(above the body tag) in application.html.erb
<script type="text/javascript">$(document).ready(function() {
if ($('textarea').length > 0) {
var data = $('textarea');
$.each(data, function(i) {
CKEDITOR.replace(data[i].id);
});
}
});</script>
STEP 8: Restart the WEBrick SERVER.
That's it.
Else
Download the CKEditor Zip file, extract the files and place them in the sub directory “javascripts/ckeditor”, add the main JS file to the layout..
javascript_include_tag 'ckeditor/ckeditor.js'
Place this in footer(above the body tag) in application.html.erb
<script type="text/javascript">$(document).ready(function() {
if ($('textarea').length > 0) {
var data = $('textarea');
$.each(data, function(i) {
CKEDITOR.replace(data[i].id);
});
}
});</script>
I have the same problem using rails 4 and apparently the problem is that the form helper
form.cktext_area
Or in your case
f.input :description, as: :ckeditor
it's not generating what it supposed to generate, and you don't have to load the editor manually, the only thing you need to do is to is to add the class 'ckeditor' to your textarea and it will load automatically, like this:
f.cktext_area :body, :class => 'ckeditor'
Meanwhile the Galetahub gem has been updated, but it has to be updated in your app manually. Read the github page: https://github.com/galetahub/ckeditor.
ajkumar basically answered the question well already, but if you are still lost, all you need to do is download the js file, include it in your html, have a script snippet included in the HTML to activate ckeditor on a certain textarea tag ID, and then change the class of the "textarea" tag you want to change to ckeditor. Quick sample below
<!DOCTYPE html>
<html>
<head>
<title>A Simple Page with CKEditor</title>
<!-- Make sure the path to CKEditor is correct. -->
<script src="../ckeditor.js"></script>
</head>
<body>
<form>
<textarea name="editor1" id="editor1" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
<script>
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace( 'editor1' );
</script>
</form>
</body>
</html>
The galetahub gem is currently broken on Rails 4. This one is working fine though: https://github.com/tsechingho/ckeditor-rails
In case you are having trouble making it work with active admin, make sure to put this:
config.register_javascript 'ckeditor/ckeditor.js'
config.register_javascript 'ckeditor/init.js'
Into config/initializers/active_admin.rb
I'm just trying to learn a bit of jQuery with Rails at the moment. I'm following the example here:
Ideas on simple 'Hello World' Rails-Ajax Example?
My view looks like this:
/views/main/ajax_msg.erb
<div id="div_msg">
<%= link_to "Wow",
{:controller=>"main" , :action=>'ajax_msg' , :id=>'1'},
:remote => true,
:class => 'link1',
:update => "div_hello",
:id => 'link1'
%>
</div>
<div id="div_hello">
</div>
My controller looks like this:
/controllers/main_controller.rb
def ajax_msg
#msg1 = "Buenos Dias!"
respond_to do|format|
format.html
format.js
format.json
end
end
My javascript file looks like this:
/views/main/ajax_msg.js
jQuery('#div_hello').html("<%= #msg1 %>");
My /views/layouts/application.html.erb begins like this...
<!DOCTYPE html>
<html>
<head>
<title>SomeApp</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
My application.js contains...
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree .
My console log shows me this:
Started GET "/main/ajax_msg?id=1" for 127.0.0.1 at 2013-07-09 21:44:57 +0100
Processing by MainController#ajax_msg as JS
Parameters: {"id"=>"1"}
Rendered main/ajax_msg.js within layouts/application (1.0ms)
Completed 200 OK in 19ms (Views: 19.0ms | ActiveRecord: 0.0ms)
I have tried many different combos by looking at other people's problems on related issues but nothing seems to work. I have tried the following:
- using $ instead of jQuery in the javascript file
- putting the javascript in application.js
- switching the order between stylesheet_link_tag and javascript_include_tag
Am I missing something quite basic here?
Thanks.
Try renaming /views/main/ajax_msg.js to /views/main/ajax_msg.js.erb so that file can handle embedded Ruby?
For debugging and to make sure it's rendering embedded ruby you can just try doing any other string in there:
jQuery('#div_hello').html("<%= puts 'blah' %>");
I think that is the problem.
1) File should be called js.erb, because, obvioulsy, if you are using Ruby variable inside, it should be extended ruby
2) From Jquery API:
Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.
That means, that string should be wrapped with tags, if you are trying to set page element.
Try:
$('#div_hello').text("<%= #msg1 %>")
Also, if this doesn't work, try to escape javascript:
$('#div_hello').text("<%= j #msg1 %>")
Environment: Rails 3.2.3, ruby 1.9.3-p125
My gemfile has the following in it:
gem 'calendar_date_select'
my layouts/application.html.erb has the following statement (as the last statement in the head section):
<%= calendar_date_select_includes %>
I installed the gem via gem install calendar_date_select
and ran rake bundle:install
I read the documentation, and some of the responses on StackOverflow, and here's what I ended up putting in my view:
<%= form_for(#panel) do |f| %>
.......
Start Date/Time: <%= f.calendar_date_select :start_time, :iso_date => true %>
I am getting the following error message:
undefined method `calendar_date_select' for #<#:0x007fae3b136938>
Any ideas?
I've switched from using that gem to just using jqueryUI which has the same functionality.
To do that:
view
= f.text_field :content_date, id: 'datepicker', size:10
= f.time_select :content_date, :ignore_date => true # Added later. not shown in screenshot
Gemfile
# Nothing - jquery & jqueryUI are now the default.
# Check web pages to see what really gets included (in the header).
application.js
%script
# The main code
$(function() {
$( "#datepicker" ).datepicker();
});
# This makes the pop-up show the actual db date by fixing a format difference.
$(function (){
var dateInput = $("#datepicker");
var format = 'yy-mm-dd';
dateInput.datepicker({dateFormat: format});
dateInput.datepicker('setDate', $.datepicker.parseDate(format, dateInput.val()));
});
Use this code to enable time in calender select tag
<%= f.calendar_date_select "date", :time => true , :readonly=>true, :popup=>"force" %>
This is an issue I have been working around for some time now thinking I would eventually stumble on an explaination. I have not and it's now becoming a little more problematic for me.
I used rails-generate-scaffold to create a simple (users and posts) application. This was done after installing jquery and jquery-ui. I added actions "login" and "auth" to the users controller. Login.html.erb contains some javascript that sends .ajax request to the users#auth action passing the login information (email and password) as parameters.
The the template auth.js.erb exists. The "auth" action responds to format.js. The request looks normal, but rails processes the request with the "show" action rather than the "auth" action.
In other words, a request to userscontrollers#auth (via .ajax) is being processed by userscontroller#show (as JS).
The problem goes away if I remove the "resources :users" route that scaffold added (the correct action is then called). But without this route other useful scaffold stuff becomes unuseable (like users#new).
From Gemfile: gem 'jquery-rails', '>=0.2.6'
Installed jQuery with: rails generate jquery:install --ui
From ../layouts/application.html.erb
<head>
<title>Tab1</title>
<%= stylesheet_link_tag :all %>
<%= stylesheet_link_tag 'jquery-ui-1.8.16.custom.css' %>
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag 'jquery.min.js' %>
<%= javascript_include_tag 'jquery.min.js', 'jquery-ui.min.js' %>
<%= csrf_meta_tag %>
</head>
Here is ./log/development.log to shows the request and the response.
Started GET "/users/auth/?email=steve&password=[FILTERED]&_=1326063255777" for 24.11.242.181 at 2012-01-08 17:53:45 -0500
Processing by UsersController#show as JS
Parameters: {"email"=>"steve", "password"=>"[FILTERED]", "_"=>"1326063255777", "id"=>"auth"}
^[[1m^[[36mUser Load (0.2ms)^[[0m ^[[1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 0 LIMIT 1^[[0m
Completed 404 Not Found in 6ms
ActiveRecord::RecordNotFound (Couldn't find User with ID=auth):
app/controllers/users_controller.rb:43:in `show'
Suggests that the request --> GET "/users/auth/?email ... is being processed by UsersController#show as JS
Thanks