How to get the current view name from layout template in Rails? - ruby-on-rails-3

Is it possible to get the name of the currently rendered view from inside layout?

I did something like this for css namespacing:
# config/initializers/action_view.rb
ActionView::TemplateRenderer.class_eval do
def render_template_with_tracking(template, layout_name = nil, locals = {})
# with this gsub, we convert a file path /folder1/folder2/subfolder/filename.html.erb to subfolder-filename
#view.instance_variable_set(:#_rendered_template, template.inspect.gsub(/(\..*)/, '').split('/')[-2..-1].join('-'))
out = render_template_without_tracking(template, layout_name, locals)
#view.instance_variable_set(:#_rendered_template, nil)
return out
end
alias_method_chain :render_template, :tracking
end
# application.html.erb
<body class="<%= :#_rendered_template %>" >

Use <% __FILE__ %> to get the complete file path of current view, but you can only use it from within the file itself without writing some helpers

The method active_template_virtual_path method returns the template as a name in the following form "controller/action"
class ActionController::Base
attr_accessor :active_template
def active_template_virtual_path
self.active_template.virtual_path if self.active_template
end
end
class ActionView::TemplateRenderer
alias_method :_render_template_original, :render_template
def render_template(template, layout_name = nil, locals = {})
#view.controller.active_template = template if #view.controller
result = _render_template_original( template, layout_name, locals)
#view.controller.active_template = nil if #view.controller
return result
end
end

I had a similar question. I found <%= params[:controller] %> and <%= params[:action] %> to suit my need, which was to add the controller name and action name as classes on the body tag.
Just in case that helps anyone. :)

I'm currently using a modified version of Peter Ehrlich's solution. The resulting string is of the form controller_name/view_name, e.g. users/new, which means it can be passed directly to render later on, or altered to suit other uses. I've only tried this with Rails 4.2, though as far as I know it ought to work all the way back into the 3.xes.
ActionView::Base.class_eval do
attr_accessor :current_template
end
ActionView::TemplateRenderer.class_eval do
def render_template_with_current_template_accessor(template, layout_name = nil, locals = {})
#view.current_template = template.try(:virtual_path)
render_template_without_current_template_accessor(template, layout_name, locals)
end
alias_method_chain :render_template, :current_template_accessor
end

For debugging purpose, you can use gem 'current_template' from here.
This gem inspects logfile and display file name of view/partial template.
For example:
Also, you can simply add this line
<%= "#{`tail log/development.log`}".scan(/\s[a-z]+\/\S+/) %>
to your layout/application.html.erb.

Related

rails 3 nested resource `new` path being switched

I don't quite get why this is happening, maybe someone of you does... here it goes.
I've created a nested resource:
resources :order do
resources :ordered_vehicles
end
I've added a link_to the new action and passed the order.id like so new_order_ordered_vehicle_path(order.id) the page is loaded nicely. The problem is after I press the button to submit the choice. He switches the path from http://localhost:3000/order/3/ordered_vehicles/new to http://localhost:3000/order/R076027535/ordered_vehicles and displays error Couldn't find Order with id=R076027535... go figure.
The error is being raised in the controller in this method
private
def find_order
#order = Order.find(params[:order_id])
end
Which is a before_filter.
the new.html.haml file looks like this
= form_for [#order, #ordered_vehicle], html: { multipart: true } do |f|
= #order.number
%br= #order.id
= f.fields_for :vehicles do |car|
.... #some fields here
= car.submit "Save your choice"
That id he can't find is the #order.number but I don't get why the switch.
EDIT:
Just to be thorough, I'll add the controller methods:
def new
#ordered_vehicle = #order.ordered_vehicles.build(params.slice(:order_id, :vehicle_id))
end
def create
binding.pry
#ordered_vehicle = #order.ordered_vehicles.build(params.slice(:order_id, :vehicle_id))
if #ordered_vehicle.save
flash[:notice] = "Save successful."
redirect_to account_path
end
end
POST request (I hope that's the one, still new to all this stuff):
"action_dispatch.request.formats"=>[text/html]},
#request_method="POST", #filtered_parameters={"utf8"=>"✓", "authenticity_token"=>
"Ar4vy8pqCSpA2ch0qG0qiJXAJUbNALYxm/FbuKbdzCc=", "ordered_vehicle"=>
{"vehicles"=> {"maker_id"=>"", "model_id"=>"", "year"=>"", "body"=>"", "capacity"=>"", "id"=>"1"}},
"commit"=>"Save your choice", "action"=>"create",
"controller"=>"spree/ordered_vehicles", "order_id"=>"R076027535"}, #method="POST",
#fullpath="/order/R076027535/ordered_vehicles">
As per request ;)
Well, it turned out to be a problem with Spree which I'm currently tweaking (I know I didn't mention it explicitly, but didn't want to just post too much information).
Bottom line:
In the Order model the method to_param was overwritten to pass the number column in to the params. Didn't overwrote it again, just left it there and adapted. In my find_order method I wrote:
def find_order
#order = Order.find_by_number(params[:order_id])
end
Also I've stored the order number in the table, there was a problem I believe with out that, but can't remember explicitly. Anyways, thanks for the help.
P.S. Sorry for the mess

how to append/replace alt/title attribute in all image tags?

say I have an action template like this
# home/index.html.erb
<%= img_tag "logo.gif" %>
if I want to add alt/title attribute to it, I can just do
# home/index.html.erb
<%= img_tag "logo.gif", alt: "alt!!", title: "title!!" %>
but I have 1000 image tags and I don't want to change every each one of them.
I then thought of using rack middleware and modify image tags before outputting from server.
http://railscasts.com/episodes/151-rack-middleware?view=asciicast
doc = Nokogiri.HTML(#response.body)
doc.search("img").each do |tag|
[:alt, :title].each{|attribute| tag[attribute] = "changed!!" }
end
but when I follow the railscast episode, it appends entire body on the top of the original rather than replacing it.
Am I doing it wrong in the rack, or is there a smarter way to do this?
Updated answer:
# /config/initializers/image_tag_helper.rb
module ActionView
module Helpers
module AssetTagHelper
def image_tag(source, options={})
options[:src] = path_to_image(source)
options[:alt] = "Default Alt" unless options.has_key?(:alt)
options[:title] = "Default Title" unless options.has_key?(:title)
tag(:img, options)
end
end
end
end
This overrides the image_tag helper method to set default alt and title attributes.

Helper function not returning string

So I'd like to generate a random background-color based on an array:
def panel_color
a = ["#E5E0AE","#A4D349","#F1427B","#F09137","#792060"]
return a.sample
end
Simple enough. This is to be used in my disc#index.erb view, so I call it there:
...
<div class="panel" style="background-color: <% panel_color %>;">
...
Since this is a helper method for the view, I placed the function in helpers/disc_helper.rb
module DiscHelper
def panel_color
a = ["#E5E0AE","#A4D349","#F1427B","#F09137","#792060"]
return a.sample
end
end
Which, to my surprise, returns nothing to the view, but does not error, either. I'm thinking I missed something very obvious here, but I'm not quite sure what. Latest rails here.
You're just executing it, not displaying it. Use <%= ... %> instead:
<%= panel_color %>
def panel_color
["#E5E0AE","#A4D349","#F1427B","#F09137","#792060"].sample
end

saving file after passing parameter

Here is the parent question: save string to file
I want to pass the parameter which will be saved in file(.csv) after clicking button.
#bigtable is a table with strings in each row.
Here is the code in my show.html.erb:
...some code here...
<%= form_tag do %>
<% text_field_tag, id = "bigtable", value = #bigtable.to_s %>
<%= submit_tag 'Zapisz' %>
<% end %>
and my controller method:
def savefile
#bigtable = param[:bigtable]
#bigtable.join("\n")
File.open("path/to/file", "w") { |file| file.write #bigtable.join("\n") }
end
But mine code doesn't work :/
I want to save #bigtable strings to file. Each row record of the table is a new line of the file. And I want to save file without redirecting current page anywhere but completely don't know why:( Please help.
okay, I know why it doesn't work - I shoud add some new route to initialize savefile method - but how do it without redirecting/refreshing current page with results? plz help
Use <%= form_tag(url, :remote => true) do %> to make the call with Ajax, so your page will not be redirected. Use your server logs to see if the request is executed (if you want to get the result of the ajax call in your page, look at http://www.alfajango.com/blog/rails-3-remote-links-and-forms/).
I've found a solution - to not write double post here is the link to the topic with the answer: saving variable to file and downloading it

Rails 3: Will_paginate's .paginate doesn't work

I'm using newes Rails 3 version with will_paginate.
#videos = user.youtube_videos.sort.paginate :page => page
I also added the ##per_page attribute to my youtube_video-model.
But it just won't paginate it. I get always all items in the collection listed.
What have I done wrong?
Yours, Joern.
Why are you calling sort here? That seems unnecessary, and probably would result in it finding all videos and calling pagination on that rather than paying any attention to any variable defined in your Video model. Instead, move the sorting logic into the Video model by using a scope or use the order method.
Here's my solution, my own answer, for all other's having trouble with will_paginate and reading this issue:
Create an ApplicationController method like this:
def paginate_collection(collection, page, per_page)
page_results = WillPaginate::Collection.create(page, per_page, collection.length) do |pager|
pager.replace(collection)
end
collection = collection[(page - 1) * per_page, per_page]
yield collection, page_results
end
Then in your Controller, where you got the collection that should be paginated:
page = setup_page(params[:page]) # see below
#messages = Message.inbox(account)
paginate_collection(#messages, page, Message.per_page) do |collection, page_results|
#messages = collection
#page_results = page_results
end
And in your View:
<% #messages.each do |message| %>
<%# iterate and show message titles or whatever %>
<% end %>
<%= will_paginate #page_results %>
To get the page variable defined, check this:
def setup_page(page)
if !page.nil?
page.to_i
else
1
end
end
So page = setup_page(params[:page]) does the trick, with that simple method.
This WORKS!