I currently used turbo-frame in a project which was using turbo-links, after I found that all the pages are disrupted by turbo-frames, such as the dropdown list became something looks like a input area (Sorry couldn't find the picture of that problem). But this was solved by replacing turbo-links with turbo-rails.
However, currently I found a new issue, there are some codes in the project looks like below:
def update
...
if #student.update(update_params)
redirect_to #student, notice: "update success!"
else
render :new, status: :unprocessable_entity
end
end
The problem occurs when it enters the "else" block, my dropdown lists on that page will behave wrongly, and the some dropdown are in the side bar, which means it affects the whole style. I'm using boostrap 5 and ruby on rails 6.
There are not much solution I can find on google (There is one similar question asked but no solution for that, and I didn't use lazy load), so I don't really know how to handle this problem, I only tried something like reinstall turbo-rails but can't actually solve the issue.
[EDIT] Adding <script>data-turbo-eval=false</script> to application.html not working
[EDIT] Adding <%= javascript_pack_tag 'application', 'data-turbo-eval': false %> to application.html not working
[EDIT] Adding data-turbo-eval=false to script tags is not working
Solved my own problem, I added Turbo.session.drive=false to the "application.js" and it now works fine. But I still have no idea why it will work by disabling the turbo drive. The project used Turbolinks and as far as I know, Turbo drive is just an updated version of it, but there was no issues when Turbolinks was used. Would be nice if someone could explain that.
Related
I have a project in Rails 3, with a partial _form_fields.html.erb that is rendered to a page and contains a remote link:
<!-- a few form fields, then... -->
<%= link_to '+', new_my_controller_path, remote: true %>
This gets processed by a controller, and appends this same partial below itself.
The controller:
def new
respond_to do |format|
format.js
end
end
And the related js.erb file:
$('.field-wrapper').append("<%= escape_javascript(render(partial: 'form_fields', layout: false)) %>")
All pretty standard, and the link loaded with the page works as expected, rendering the fields.
However, and here is my problem, when clicking this link on the newly rendered content, it submits as a regular link, ignoring remote: true.
Does anyone know the solution to this? I can't be the first person to have faced this, though haven't been able to find a solution - duplicates happily accepted if anyone can find one.
Thanks in advance.
Found the solution to this after a little debugging.
Looking at the terminal output when clicking the link, I noticed sometimes it would attempt to submit via JS, quickly followed by HTML; others, it would appear just to submit via HTML.
This suggested a race occuring between the default behaviour and that resulting from using remote: true.
The solution was to call e.preventDefault() on the element's click handler:
$('.my-element').click(function(e) { e.preventDefault(); })
Not ideal, and I presume fixed in newer Rails versions, though provides a little closure here :)
I use the gem pdfjs_viewer-rails for my Rails application and Carrierwave to store my PDF's on Amazon S3.
When i link to a PDF-url i with the mounted route specified by the gem:
# routes.rb
mount PdfjsViewer::Rails::Engine => "/pdfjs", as: 'pdfjs'
and my view:
#_johannes_writing.html.erb
<%= link_to pdfjs.full_path(file: johannes_writing.pdf.url) do %>
<h3>Read <%= johannes_writing.title %></h3>
<% end %>
i am redirected to the correct page but the design and function is totally off.
I have to reload the page to get the proper functionality (se image below )
BEFORE AND AFTER RELOAD
Now it get strange
If i go back in my browser when i'm on the pdf-view-page without reloading, the css is changed for my #_johannes_writing.html.erb view to the dark-background as the pdf-view.
If i set my layout to false in my controller, everything is working as it should. The PDF is showing the first time without reload and nothing is changed on a back. So somehow my layout CSS and HTML are interferring with the GEM even though the pdf-view is on its own route?
What can be the cause of this?
I tried different opportunities with Iframe integration on its own view (like the gems dokumentation suggests), but nothing helped.
Any help will be appreciated.
Best Peter
Link to project: http://inger-exner.herokuapp.com/johannes_writings
(push the word "Læs" under the button to go to PDF-viewer)
If any one has the same problem - i found a solution.
I tried with another gem: pdfjs_rails
And followed his instructions.
So my conclusion is: try this gem!
I also tried this gem and also faced with some problem so change it to iframe with Google pdf viewer like in answer https://stackoverflow.com/a/27685952/3884750
I have a link_to method in my Ruby on Rails application in one of the views, and when I click on it, the controller is set to do a whole bunch of things... But for some reason I'm seeing this GET request twice even when I click on the link one time.
Here's what the link looks like:
<%= link_to image_tag("excel.png"), spreadsheet_technical_report_path(report) %>
Which goes to /technical_report/id/spreadsheet and here's what it looks like in the controller:
def spreadsheet
spreadsheet = GenerateSpreadsheet.generate(params[:id])
send_file spreadsheet
end
I've even replaced the contents of that function with a binding.pry, and it hits it twice! This is so confusing. my whole GenerateSpreadsheet model does a variety of things and takes approximately a minute, and this second request does nothing but double that time.
Can someone please tell me what I'm missing here? I don't have a view set up for this since I want it to just send the user a download prompt (which it's doing) and not necessarily go to a view. I don't even know if not having a view is even relevant here.
JS
To add to the comments, the main issue here would likely be that you've bound some javascript to the page's a elements.
With an absence of remote: true and other hooks, the only thing which would likely cause a double-fire from your link_to is if Javascript is sending an ajax request too.
You mention that you...
removed //= require tree . from my application.js
... whilst good that this fixed the issue, you have to remember that nothing happens with computers without them being told to do it. IE your "link" wouldn't just double-click for the sake of it.
If your JS works when you remove the //require_tree ., you'll want to look at the other JS files you have. There will be one where you're binding to the $("a").on("click" event, which is likely leading to the double-firing of your link.
Thanks to chaitanya saraf's comment, I just removed the "//= require tree ." from my application.js to get this fixed. Once I got rid of this, I added this to my config/initializers/asset.rb file
Rails.application.config.assets.precompile += [/.*\.js/,/.*\.css/]
Got rid of this annoying problem nice and easily.
In my rails3 project, I had to implement a simple text field, a div and a autocomplete helper in my view file. auto complete works well in all browsers except IE. the div element in which the results fall in keeps changing its style attribute. here is the code.
.. #form related other code
<%= text_field_tag('location') %>
<div id="location_auto_complete" class="auto_complete"> </div>
<%= auto_complete_field('location', :url => locations_path(), :indicator => 'locations_indicator', :select => 'value') %>
it works perfect in all browsers. but in IE, the auto suggestions box dislocates itself to some other part in the page. style attribute is added dynamically to that div element everytime there is a response from server.
I am using the latest fork of rails autocomplete plugin https://github.com/fidel/auto_complete . Please help, I am stuck with this problem for hours.
I ran into this same problem. Apparently it is a problem with Prototype's getOffsetParent function and IE8:
https://prototype.lighthouseapp.com/projects/8886-prototype/tickets/618-getoffsetparent-returns-body-for-new-hidden-elements-in-ie8-final#ticket-618-9
Pretty embarrassing for Prototype actually that this still hasn't been fixed since that thread is from March 2009.
Anyway, as someone in that thread mentions, you can edit your prototype.js file and change the first line of getOffsetParent to this:
getOffsetParent: function(element) {
if (element.offsetParent && Element.visible(element)) return $(element.offsetParent);
...
The && Element.visible(element) is the new part. This fixed it for me. Make sure to do a hard refresh (shift-reload) in IE8 so it picks up the new JS after your changes.
In learning ruby on rails I've created a blog site. When the user adds a post via AJAX, the following rjs gets called:
page.replace_html 'posts', :partial => #posts
page.visual_effect :Highlight, 'post_' + #post.id.to_s
However, the highlight isn't happening.. neither is any sort of effect even a hide.
Clues:
It works if I just do an insert_html
for just the new post (but I want
to update the whole list of posts
at this time)
If I hard code the id to the next id in the sequence, it doesn't work on the post, but it does work on the next post. { ex.. hardcode post_100... no highlight on submit 100, highlight 100 on submit 101 }
Alert shows that 'post_' + #post.id.to_s is what is expected ( post_100, etc )
Any ideas or debugging suggestions?
Thanks,
Orlando
Can you alert the innerHTML of the $("post_#{#post.id}") before the visual_effect.
Does firebug give you an error when it gets to the visual_effect?
Can you do something else, like an alert after the visual_effect line?
Have you got the required js files included?
It's not really an answer to the problem, but I have since done away with reliance on rjs. Instead I'm following the pattern outlined here
http://blog.solnic.eu/2007/10/30/why-javascript-helpers-in-rails-are-evil
And now all effects are working as expected. Note that I did get the effect working when comments were added using effectively the same code that should have been working here, so I'm fairly convinced there was just some sort of weird operator error going on.