link_to_remote attributes/arguments in rails 3 - ruby-on-rails

I'm trying to upgade from rails 2.3 to 3.0 and I've found that link_to_remote in rails 2 should be changed to link_to in rails 3 with :remote => true attribute.
And unobtrusive javascript(UJS) for :before, :loading, :failure, :update
But I also have attributes like :url, :href, :title how am I supposed to change that ?
Here is the rails 2.3 code I'm trying to upgrade
<%= link_to_remote column.label,
{ :url => sort_params,
:before => "addActiveScaffoldPageToHistory('#{href}', '#{controller_id}')",
:loading => "Element.addClassName('#{column_header_id}','loading');",
:failure => "ActiveScaffold.report_500_response('#{active_scaffold_id}')",
:update => active_scaffold_content_id,
:method => :get },
{ :href => href ,
:title => column.header_info}%>
I've analysed lot of websites and Rails documentation but nothing has specified about these attributes for link_to

You can bind callbacks to remote links in Rails 3, the rest of the attributes can be assigned as options.
link_to column.label,
sort_params,
remote: true,
title: column_header.info,
id: 'my_remote_link',
data: {
href: href,
controller_id: controller_id,
column_header_id: column_header_id,
active_scaffold_id: active_scaffold_id
}
We'll use the data-attributes for the callbacks.
$('#my_remote_link').bind('ajax:beforeSend, function() {
addActiveScaffoldPageToHistory($('#my_remote_link').data('href'), $('#my_remote_link').data('controller_id'));
});
See http://docs.jquery.com/Ajax_Events for a description of the different ajaxEvents.

Related

Rails semantic logger tags stopped working after upgrade to 4.4.1

Before upgrade
I used rails_semantic_logger with tags for logging request id, and session id. I had in my logging initialzier and it worked adding tags with these ids:
config.log_tags = [
#:host,
#:remote_ip,
lambda { |request| "#{request.uuid}"[0..32] },
lambda { |request| "#{request.cookie_jar["_session_id"]}" },
]
Sample log row:
2019-04-07 13:54:03.563661 I [25929:puma 001] [["a2eb2610-9db1-475b-8d0a-547dc514d", "7713d31653d81aa6d9fe1882c282cc95"]] (499.8ms) DebugController -- Completed #show -- { :controller => "DebugController", :action => "show", :format => "*/*", :method => "GET", :path => "/debug", :status => 200, :view_runtime => 463.81, :rendered_format => :js, :variant => [], :db_runtime => 32.32, :status_message => "OK" }
Environment:
rails 5.0.4
ruby 2.4.2
semantic_logger 4.2.0
rails_semantic_loggger 4.1.3
After upgrade
Recently I upgraded my enviroment to newer versions of rails and gems (specific versions below) I can't setup automatic tags logging. I tried some combinations with named_tags like this:
config.log_tags = {
host: :host,
ip: :remote_ip,
request_id: :request_id,
session_id: -> request { request.cookie_jar['_session_id'] }
}
But it simply doesn't work:
2019-04-07 14:19:04.131479 I [23455:puma 001] (394.2ms) DebugController -- Completed #show -- { :controller => "DebugController", :action => "show", :format => "*/*", :method => "GET", :path => "/debug", :status => 200, :view_runtime => 384.13, :db_runtime => 6.99, :status_message => "OK" }
Environment:
- rails 5.1.7
- ruby 2.4.2
- semantic_logger 4.2.0
- rails_semantic_loggger 4.4.1
I created a sample app to picture the problem: https://github.com/camaeel/logger-error
Semantic logger from version 4.3 or 4.4 needs setting config.log_tags earlier. It need to be set in config/[ENV_NAME].rb or application.rb. Initializer in config/initializers/*.rb is too late as loggers for Rails & Rack are initialzied before.

rails form action missing route

Ruby 1.9.3 and using HAML.
Trying to build a form to go to a specific action, but having problem with routing. The code on the form is
%form#feedback_form{:action=>"give_feedback_account_path", :method => 'post', :style => "padding: 0 5px;"}
%input{:name => "authenticity_token", :value => form_authenticity_token, :type => "hidden"}
blah blah blah
.field
%input#feedback_submit{:type => "submit", :value => "give feedback"}
When I try to submit the form I get a 404 response, and looking at the server log gives me...
Started POST "/give_feedback_account_path" for 127.0.0.1 at 2014-06-03 10:07:12 +0100
ActionController::RoutingError (No route matches "/give_feedback_account_path"):
When I run rake routes to get the details I get
give_feedback_account POST /account/give_feedback(.:format)
{:action=>"give_feedback", :controller=>"accounts"}
What am I missing?
Your url is /give_feedback_account_path, which is not good. To fix it, you can use form_tag helper:
= form_tag give_feedback_account_path, method: 'post', style: 'padding: 0 5px;' do
// your form goes here
change this
:action=>"give_feedback_account_path"
to this
:action => give_feedback_account_path
give_feedback_account_path is a method. You want to call it, to get the actual path.

Any rails code inside haml tags generates blank/empty characters inside browsers html tags

Whenever a rails variable equals nil (or actually whenever i use rails code (see 3rd code sample)) i get a string of empty characters in my html.
new.html.haml
%h1.editable.editable_title_field{:contenteditable => 'true', :placeholder => "title"}
= #post_title //#post_title.present? returns false
%h4.editable.editable_subtitle_field{:contenteditable => 'true', :placeholder => "subtitle"}
= #post.subtitle
%p.editable.editable_intro_field{:contenteditable => 'true', :placeholder => "intro"}
= #post.intro
this results in:
i checked, and even:
%h1.editable.editable_title_field{:contenteditable => 'true', :placeholder => "title"}
- #post_title
and:
%h1.editable.editable_title_field{:contenteditable => 'true', :placeholder => "title"}
-
ends up the same as in attached image
because of those empty characters :empty css selector treats the element as if it was not empty and doesnt append content specified in :before
how do i get rid of those empty characters inside my html tags?
EDIT
screencast of this behavior:
http://quick.as/jqlaiorq
Try it with a < on the end of the %h1 line:
%h1.editable.editable_title_field{:contenteditable => 'true', :placeholder => "title"}<
= #post_title
See the whitespace removal info in the HAML documentation.

Rails 3.1.3 custom routing by date

How to make path helpers for this route?
resources :news
match 'news/:year/:month/:day' => 'news#show',
:constraints => { :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ },
:as => 'newsdate'
I tried it many ways, but it not works:
link_to news.created_at.strftime '%d.%m.%Y ', newsdate_path(:year => '2011', :month => '11', :day => '11')
I get app error for this line by GET http://localhost:3000/news:
ArgumentError in News#index
Showing /home/foxweb/work/dev/app/views/news/index.html.slim where line #6 raised:
wrong number of arguments (2 for 1)
How to make it right way?
P.S. http://localhost:3000/news/2011/11/11 works fine.
Oh, that's common mistake. You need to take strftime arguments in braces.
link_to news.created_at.strftime('%d.%m.%Y'), newsdate_path(:year => '2011', :month => '11', :day => '11')
That's all!

Apotomo: How do you render a state/view that's in HTML format from another view that's in another format?

For reasons beyond this problem, I need to respond with JSON when creating an image:
<%=
raw({
...
:url => #image.url(:width => #width, :height => #height),
:edit_link => render({:state => :image_edit_table_cell, :template_format => :html}, #image),
# btw, I've also tried passing :format => :html
:delete_link => link_to_destroy_image(#image, :remote => true, :path => url_for_event(:destroy, :image_id => #image)),
...
}.to_json)
%>
However, in that JSON, I want to include some pretty fancy HTML. To include that, I render a state/view (as you can see in the :edit_link portion). This is the image_edit_table_cell.html.haml view of the widget:
= link_to_edit_image #image, :remote => true
# ... doesn't really matter - as long as you know this is html.haml
However, when I create an image, and the JSON is rendered, I get a Missing template error:
ActionView::Template::Error (Missing template cell/rails/image_edit_table_cell with {:handlers=>[:builder, :rjs, :rhtml, :erb, :haml, :rxml], :locale=>[:en, :en], :formats=>[:json]} in view paths "/home/ramon/source/unstilted/app/widgets", "/home/ramon/source/unstilted/app/widgets/layouts" and possible paths image/table/image_edit_table_cellapplication/image_edit_table_cellapotomo/widget/image_edit_table_cellcell/rails/image_edit_table_cell):
6: :caption => #image.caption || "",
7: :text_tag => #image.text_tag(:width => #width, :height => #height),
8: :url => #image.url(:width => #width, :height => #height),
9: :edit_link => render({:state => :image_edit_table_cell, :template_format => :html}, #image),
10: :delete_link => link_to_destroy_image(#image, :remote => true, :path => url_for_event(:destroy, :image_id => #image)),
11: :success => true
12: }.to_json)
app/widgets/image/table_widget.rb:37:in `image_edit_table_cell'
app/widgets/image/table/new_image.json.erb:9:in `_app_widgets_image_table_new_image_json_erb___457172591_99648970_0'
app/widgets/image/table_widget.rb:16:in `add_image'
app/widgets/image/uploader_widget.rb:22:in `upload'
It seems like the problem is that since I'm rendering it from a JSON file, it doesn't know to look for an HTML file (thus the format it's looking for is json. That's why you see the :template_file => :html option (I got that from here) in the first block of code above.
I'm using these gems:
'rails', '3.0.10'
'cells', '3.6.2'
'apotomo', "1.1.2"
Apparently, this is a Rails issue, not an Apotomo issue. This answer did it for me! Key is setting the format in your view file before you render your html view:
<% self.formats = ["html"] %>

Resources