I'm a newbye that is trying to do little things in Rails, at the moment I have created an app that build some links and I want to refresh the content of a Div when they are clicked.
In my application.html.erb I have the layout with the next head:
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
(I have read about change the second line for :defaults but if do that I get a 404 error defauls.js)
I have an index.html.erb that render a partial with this:
<%= render :partial => 'masvisitadas' %>
In my _masvisitadas.html.erb I have the code that generate the links:
<%= link_to solution.SolutionName, "/soluciones/despliega/" + solution.Id.to_s, :remote=>true %>
This generate the next html:
http://localhost:3000/soluciones/despliega/501D0000000QWp6IAG
In my controller I have the next def:
def despliega
respond_to do |format|
format.js {}
end
end
Finally I have created despliega.js.erb:
jQuery(function($) {
var html = "<%= escape_javascript(render('despliega')) %>";
$("#customTopLeft").prepend(html)
$("customTopLeft").html("<%= escape_javascript(render(:partial => 'despliega')) %>");
});
In the log of the web server when i click a link it seems all going well:
Started GET "/soluciones/despliega/501D0000000QXM0IAO" for 127.0.0.1 at 2012-08-
20 12:16:53 +0200
Processing by SolucionesController#despliega as JS
Parameters: {"id"=>"501D0000000QXM0IAO"}
Rendered soluciones/_despliega.html.erb (0.0ms)
Rendered soluciones/_despliega.html.erb (0.0ms)
Rendered soluciones/despliega.js.erb (15.6ms)
Completed 200 OK in 31ms (Views: 31.3ms | ActiveRecord: 0.0ms)
And in the firebug I can see the call with any error but nothing happends.
This is killing my head :) Thanks in advance for any help here.
Regards.
What's in _despliega.html.erb?
Try changing despliega.js.erb to this (and only this):
$('#customTopLeft').html("<%= escape_javascript(render :partial => 'despliega') %>");
Edit - oh, and make sure you have the jQuery libraries included ..
Related
I've followed this GoRails video to get Devise working with hotwire-rails. I'm at a loss as to why my login error messages do not work the same way as in the video. The error messages work great on the registration form but on the login form I get this error:
Processing by Devise::SessionsController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"elvis#example.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
Completed 401 Unauthorized in 2ms (ActiveRecord: 0.0ms | Allocations: 406)
As Chris suggests in the video I've edited my Devise initializer to look like this.
My package.json has #hotwired/turbo-rails": "^7.0.0-beta.5, but in Chris's source code for the episode it's beta.3 so that does not seem to be the problem.
What am I missing here?
I was having the same problem and it turns out there is no trigger to navigate. Appending :turbo_stream to this line in initializers/devise.rb solved it for me:
Devise.setup do |config|
[...]
config.navigational_formats = ['*/*', :html, :turbo_stream]
[...]
end
I got this working by unifying all of my flash errors / notices into one partial rather than having a flash notice partial and a separate block for form errors:
I put this in my layouts/application.rb:
<%= render "shared/notices" %>
That partial looks like this:
<% flash.each do |msg_type, message| %>
<div class="alert">
<div class="container">
<button class="close" data-dismiss="alert"><span>×</span></button>
<%= message %>
</div>
</div>
<% end %>
Then, my form_for in views/devise/sessions/new.html.erb looks like this:
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
My solution is that you don't use app/javascript/packs/your_application.js in the layout of sign_in page.
Instead, you can write a new js like app/javascript/packs/blank.js, in this blank.js, don't add import "#hotwired/turbo-rails".
And use the blank.js in the new layout file app/views/layouts/blank.html.erb for the Devise sign_in page
, like these:
<head>
<%= javascript_pack_tag 'blank' %>
<%= stylesheet_pack_tag 'blank' %>
</head>
I am trying to render a form in js.erb file when the condition is true.
Following is my create.js.erb file
<% if condition is true %>
$('#status-message').html("<%= escape_javascript(render 'form') %>")
<%else%>
#do something else
<%end%>
The create method in the controller is:
controller
def create
#code
respond_to do |format|
format.js # render app/views/phone_numbers/create.js.erb
end
end
Now when I checked in the console it is rendering the form
Rendered phone_numbers/_form.html.erb (4.2ms)
Rendered phone_numbers/create.js.erb (8.8ms)
Adding _form.html.erb as requested
<%= puts "Hello world"%>
<h3><%= Equipment.last.title %> </h3>
<h3><%= Equipment.last.price %> </h3>
But its not appearing on the browser. What am I missing ?
Resolved this in chat - the problem was that the #status-message element was inside a #status-box element that was hidden via css (with display: none;), so the fix was simply to add $('#status-box').show() to create.js.erb.
I'm building a Rails app which includes a map with markers (aka "plots" in the app). When a marker/plot is clicked an infobox appears next to the marker. I'm trying to add a feature where clicking an infobox renders a partial alongside the map with further details relevant to that specific plot.
I'm using Gmaps4Rails and the feature described above works fine when implemented with Google's standard Infowindows but if I try to use the InfoBox plugin, so I can have all the styling benefits of InfoBoxes, I get an error.
ActionController::UnknownFormat
Screen grab of rails error message here
Looking at the log it appears the request is being correctly sent as JS with Infowindows but for some reason when using the InfoBox plugin it is being sent as HTML.
Log message with Infowindow is this...
Started GET "/plots/1/plotdetails" for ::1 at 2016-05-21 13:50:02
+0100 Processing by PlotsController#plotdetails as JS Parameters: {"id"=>"1"} Plot Load (0.3ms) SELECT "plots".* FROM "plots" WHERE
"plots"."id" = $1 LIMIT 1 [["id", 1]] Rendered
plots/_plotdetails.html.erb (0.1ms) Rendered
plots/plotdetails.js.erb (4.0ms) Completed 200 OK in 17ms (Views:
14.4ms | ActiveRecord: 0.3ms)
Log message with InfoBox plugin is below...
Started GET "/plots/2/plotdetails" for ::1 at 2016-05-17 18:52:38
+0100 Processing by PlotsController#plotdetails as HTML Parameters: {"id"=>"2"} Plot Load (0.3ms) SELECT "plots".* FROM "plots" WHERE
"plots"."id" = $1 LIMIT 1 [["id", 2]] Completed 406 Not Acceptable in
3ms (ActiveRecord: 0.3ms)
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/plots_controller.rb:95:in `plotdetails'
I can't find a solution so am thinking about trying to implement this with JQuery AJAX instead but I can't help but feel this should be fixable. Any ideas would be much appreciated.
routes.rb
Rails.application.routes.draw do
resources :architects
resources :plots do
get :plotdetails, :on => :member
end
root 'plots#map'
plots_controller.rb (there is more code in the controller but this seems to be the relevant part)
def plotdetails
#plot = Plot.find(params[:id])
respond_to do |format|
format.js { render :layout => false }
end
end
views/plots/map.html.erb
<%= render 'partials/menu' %>
<div id="plotdetails"></div>
<div id="map"></div>
<script type="text/javascript">
buildMap(<%=raw #hash.to_json %>);
</script>
views/plots/_infobox.html.erb
<%= link_to plotdetails_plot_path(:id => plot.id), :remote => true do %>
<div id="infobox">
<div id="infobox-img">
<img src="<%= plot.img_link %>">
</div>
<div id="infobox-text">
<h3><%= plot.title %></h3>
<p>Architect: <%= plot.architect.name %></p>
<p><%= plot.address %></p>
</div>
</div>
<% end %>
views/plots/_plotdetails.html.erb
<div>
<ul>
<li>plot.title</li>
<li>plot.details</li>
</ul>
</div>
views/plots/plotdetails.js.erb
$( "#plotdetails" ).html( "<%= escape_javascript( render( :partial => "plotdetails", :locals => { :plot => #plot} ) ) %>" );
Since respond_to has been removed from rails 4.2 to a individual gem i will recommend you to use gem 'responders'
Please checkout the link given below for more details..
Why is respond_with being removed from rails 4.2 into it's own gem?
I want to make remote file upload via remotipart + carrierwave. The problem is that my upload works, but it seems that it is uploaded as usual html. Or it is uploaded via ajax, but appropriate js file is not called.
I have a form_for with remote:true, and a corrseponding controller method create. Since the form is submitted as js, create.js.erb is used.
<% if #syllabus.errors.present? %>
$('#syllabus-errors').html('<%= escape_javascript(render 'layouts/error_messages', object: #syllabus) %>');
<% else %>
alert("ds");
$('#modal-body').html(<%= escape_javascript(render 'layouts/thanks') %>);
$('#syllabus-errors').html("");
<% end %>
Logs show this:
(0.1ms) begin transaction
(0.1ms) rollback transaction
Rendered layouts/_error_messages.html.erb (1.0ms)
Rendered syllabuses/create.js.erb (12.0ms)
It is kinda strange that error_messages are rendered earlier that create.js.erb, error_messages are being rendered inside create.js.erb.
I resolved this problem by adding this if:
<% if remotipart_submitted? %>
<% else %>
<% end %>
don't know why it didn't work without it.
I'm trying to simply render a view using UJS and Rails 3, but it's not actually working. I've read many tutorials but can't seem to get this. Here are some code snippets:
index.js.erb
"<%= render user_path(#this_user) %>"
index.html
<%= form_tag users_path, :method => :get, :remote => true do %>
<%= submit_tag "Test", :name => nil, :class => "btn" %>
<% end %>
users_controller.rb
def index
#users = User.all
#this_user = User.find(1)
respond_to do |format|
format.html # index.html.erb
format.js
format.json { render json: #users }
end
end
Every time I click on "Test", I get the following output:
Started GET "/users?utf8=%E2%9C%93" for 127.0.0.1 at 2012-10-19
11:31:49 -0700 Processing by UsersController#index as JS Parameters:
{"utf8"=>"✓"} User Load (0.4ms) SELECT "users".* FROM "users"
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" =
$1 LIMIT 1 [["id", 1]] Rendered users/index.js.erb (1.8ms)
Completed 500 Internal Server Error in 6ms
ActionView::Template::Error (The partial name (/users/1) is not a
valid Ruby identifier; make sure your partial name starts with a
letter or underscore, and is followed by any combinations of letters,
numbers, or underscores.):
1: "<%= render user_path(#this_user) %>" app/views/users/index.js.erb:1:in
_app_views_users_index_js_erb__2145063269526192522_70203075823780'
app/controllers/users_controller.rb:9:inindex'
It appears it's looking for a partial; however I just want it to execute the view associated with the "show" action on the User controller. So a completely new page.
What am I doing wrong? First step is to render a completely new view, but I plan to render a partial view within the "show" page after I figure this out.
Thanks in advance for any help!
Look at index.js.erb:
<%= render user_path(#this_user) %>
That is telling Rails to render a partial with the name of the value of user_path(#this_user).
You view file index.js.erb needs to return JavaScript. So, the following would work:
alert('The Id of this User Object is: <%= #this_user.id %>');
In the future, you can render a partial (that contains JavaScript code) by doing the following:
$("#popup").html("<%= escape_javascript(render(:partial => 'show_sold_out_popup') %>");