variable / dynamic content is changing based on <div> -- why? - ruby-on-rails

I have some unexpected behavior in a partial, which is rendered 9 times in users/show.html.erb. (Visit www.nowdoing.com/users/2 to see what I mean.) Here is the relevant part of the partial (it's verbose for debugging purposes):
<%= "object 1 = #{object.content}" %>
<div id="float_have" style="display:none;">
<%= "object 2 = #{object.content}" %>
<%= render :partial => 'nowposts/floatpost', :locals => { :object => object.content } %>
</div>
<%= "object 3 = #{object.content}" %>
Have! |
In box #1, :object => "#nowreading book"
In box #2, :object => "#nowlistening song"
Despite this, I'm seeing the following when I load my webpage:
object 1 = "#nowreading book"
object 2 = "#nowreading book"
object 3 = "#nowreading book"
That makes sense for box#1. But the following does NOT make sense for box#2:
object 1 = "#nowlistening song"
object 2 = "#nowreading book" *** this is wrong
object 3 = "#nowlistening song"
Can someone explain to me why including putting the "#{object.content}" within the facebox DIV causes the content to change? I find this behavior very peculiar. Your help is much appreciated!
--- EDIT ---
In users/show.html.erb:
<%= render :partial => 'nowposts/nowpost',
:locals => {:object => #nowreading,
:object_link => #reading_link,
:object_mode => "reading",
:doing_img => #reading_img,
:doing_url => #reading_url_text
} %>
<%= render :partial => 'nowposts/nowpost',
:locals => {:object => #nowlistening,
:object_link => #listening_link,
:object_mode => "listening",
:doing_img => #listening_img,
:doing_url => #listening_url_text
} %>
In nowposts/_nowpost.html.erb:
<div class ="status bl br">
Fresh on <%= object.updated_at.strftime("%b %d, %Y") %><br \>
<% if user_signed_in? %>
<%= "object 1 = #{object.content}" %>
<div id="float_have" style="display:none;">
<%= "object 2 = #{object.content}" %>
<%= render :partial => 'nowposts/floatpost', :locals => { :object => object.content } %>
</div>
<div id="float_wanna" style="display:none;">
<%= "object 3 = #{object.content}" %>
<%= render :partial => 'wishlists/floatpost', :locals => { :object => object.content } %>
</div>
Have! |
Wanna!
<% end %>
</div>
In application.html.erb:
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox({
loadingImage : '../images/loading.gif',
closeImage : '../images/closelabel.png'
})
})
</script>
The partial nowposts/_floatpoast.html.erb
<h2>Wanna!</h2>
<h3>You wanna do this? Great, put it on your bucket list. </h3>
<%= form_for(:wishlist, :as => #wishlist, :url => {:controller => "wishlists", :action => "create", :user_id => current_user } ) do |f| %>
<p><%= f.label :content %><br />
<%= f.text_field :content, :value => object, :size => "80" %></p>
<p><%= f.submit "Post!" %></p>
<% end %>
As mentioned in my comment below, this code was working in my last commit (a few days ago), before I implemented caching of an API result and these nested routes:
resources :users
resources :nowposts, :only => [:have_create, :create, :new, :show]
end
But I don't think that is the problem. I just ran another test, and object.content only changes when I have it inside this div:
<div id="float_have" style="display:none;">
I tried another random div:
<div id="status">
<%= "object 2 = #{object.content}" %>
</div>
and this displayed correctly. This makes me think that the problem is the Facebox script? But if so, I wonder why it worked a few days ago.
Let me know if you need anything else.

I don't understand your code at all, it is too ambiguous. 'object' is a pretty terrible name for a variable, and content is a really bad name for an attribute. Also, your locals for the partial are rendering here doesn't make much sense ({:object => object.content}????). If you would like help, please try to clarify what this means.
That being said, can you change each debug statement from
<%= "object 1 = #{object.content}" %>
to
<%= "object 1 = #{object.inspect}" %>
<%= "object 1 content = #{object.content.inspect}" %>
and display the results.

Related

link_to with block in Rails

I have seen this code in this repository:
<%= link_to(:controller => 'account', :action => 'select', :account_id => account.customer_id) { account.customer_id.to_s } %>
(<%= account.login %> / <%= account.company_name %> )
This actually converts to the following HTML:
<a account_id="8282277272" action="select" controller="account">8282277272</a>
( loginname / companyname )
I am wondering how would you pass a block to link_to in order to make this work?
I think this is what you're looking for. The stuff inside the "do..end" will be put inside the a tag.
<%= link_to(:controller => 'account', :action => 'select', :account_id => account.customer_id) do %>
(<%= account.login %> / <%= account.company_name %> )
<% end %>
It should produce
<a href="<path to controller with account_id parameter>">
(username / Company, Inc.)
</a>
What was happening in your original code was that the expression { account.customer_id.to_s } was being passed as the block to link_to. If you want the customer id to be displayed along with the "login" and "company_name", put it inside the block.
<%= link_to(account_select_path(:account_id => account.customer_id.to_s)) do%>
<%= account.customer_id.to_s %>
<%end%>
(<%= account.login %> / <%= account.company_name %> )
and in your config/routes.rb, in the beginning of the file, add
match 'account/select' => "account#select", :as => :account_select

Unable to override an existing partial using Deface

In "spree_application.html.erb" file I need to override an existing partial
Get the complete code from below link :
https://github.com/spree/spree/blob/v2.1.2/frontend/app/views/spree/layouts/spree_application.html.erb
sample contents :
<%= render :partial => 'spree/shared/header' %>
<div id="wrapper" class="row" data-hook>
<%= breadcrumbs(#taxon) %>
<%= render :partial => 'spree/shared/sidebar' if content_for? :sidebar %>
<div id="content" class="columns <%= !content_for?(:sidebar) ? "sixteen" : "twelve" %>" data-hook>
<%= flash_messages %>
<%= yield %>
</div>
<%= yield :templates %>
</div>
Here I need to replace following partial using Deface:
<%= render :partial => 'spree/shared/header' %>
my Deface file : header_modification.rb :
Deface::Override.new(:virtual_path => 'spree/layouts/spree_application',
:name => 'header_modification',
:replace => ?
:partial => 'spree/shared/spree_application'
)
What should be written in :replace part / any other action can be used to override that partial using Deface ?
You can test your rake selector using the following command:
rake deface:test_selector['spree/layouts/spree_application','erb[loud]:contains("spree/shared/header")']
Querying 'spree/layouts/spree_application' for 'erb[loud]:contains("spree/shared/header")'
---------------- Match 1 ----------------
<%= render :partial => 'spree/shared/header' %>
So this should work:
Deface::Override.new(:virtual_path => 'spree/layouts/spree_application',
:name => 'header_modification',
:replace => 'erb[loud]:contains("spree/shared/header")',
:partial => 'spree/shared/spree_application'
)
using <code erb-loud> render :partial => 'spree/shared/header' </code> should be working. There is a useful tool by the way which tells you that: http://deface.heroku.com/

Rails 3 - Not able to pass local variable to a partial resursively

++ edited to change the question to call partial recursively ++
I am writing this question after a lot of days of frustration about this simple piece of code which isn't working. I have read numerous issues with passing partials on stackoverflow and tried all the solutions I could but it doesn't work.
Calling the partial works for the first loop but from second onwards it bombs with an error
"undefined local variable or method `count' for #<#:0x2f12670>"
Notice here that the error contains a class within a class, so something fishy is happening when the partial is being called recursively.
I have a partial called _form.html.erb which calls another partial _rule_action_fields.html.erb. I need to pass a variable from the form partial to the rule_action_field partial and here is the code:
_form.html.erb: (Please note that the builder object here is being passed from code I have not pasted)
<% #folder_count = 1 %>
<% #rule.rule_actions.each do |ruleaction| %>
<%= f.fields_for :rule_actions, ruleaction do |builder| %>
<%= render( :partial => 'rule_action_fields', :locals => { :f => builder, :count => #folder_count } ) %>
<% end %>
<% #folder_count = #folder_count + 1 %>
<% end %>
_rule_action_fields.html.erb
<p id="folder_fields">
<%= count %>
<label class="form_label">Move the files to </label>
<%= f.select(:folder_id, current_user.folders.collect { |p| [p.name,p.id] }, {:include_blank => true } ) %>
</p>
Unfortunately I cant call this partial with a collection of rule_actions because of some logic which is partially in the calling code and partially in the rule_action_fields partial.
Other syntax I have tried:
<%= render :partial => 'rule_action_fields',:f => builder, :locals => { :count => folder_count } %>
<%= render :partial => 'rule_action_fields', locals => { :count => folder_count } %>
<%= render 'rule_action_fields', :count => folder_count %>
Any ideas?

Rails 3: Can't add correct route to legacy code

Believe you can help me.
I'm trying to add new functionality to legacy code (Typo). But it seems that there is some problem about routing.
In the project routes are generated the following way:
%w{advanced cache categories comments content profiles feedback general pages
resources sidebar textfilters themes trackbacks users settings tags redirects seo post_types }.each do |i|
match "/admin/#{i}", :to => "admin/#{i}#index", :format => false
match "/admin/#{i}(/:action(/:id))", :to => "admin/#{i}", :action => nil, :id => nil, :format => false
end
My functionality is about merging articles. For that I've added new action in the /admin/content controller:
def merge
#some code here
end
A piece of a view partial (_form.html.erb) added by me:
<% if current_user.admin? and !#article.id.nil?%>
<div class=''>
<h4><%= _("Merge Articles") %></h4>
<%= label_tag :merge_with, 'Article ID' %><%= text_field_tag :merge_with, nil, :size => 20 %>
<%= button_to 'Merge', admin_content_merge_path(:id => #article.id) %>
</div>
<%end%>
This partial is rendered by another partial (_edit.html.erb)
<%= form_tag(form_action, :id => "#{form_type}_form", :enctype => "multipart/form-data", :class => className) do %>
<%= render :partial => "form" %>
<% end %>
And finally _edit.html.erb is rendered by view new.html.erb
<%= render "admin/shared/edit", { :form_type => "article", :form_action => { :action => "new", :id => #article.id , :class => ('autosave')} } %>
The problem is how to write a correct route for the controller action above which will allow me to render an edit page containing newly merged article. I wrote:
match "/admin/content/merge/:id" => "admin/content#merge",:as => 'admin/content/merge'
rake routes output:
admin_content_merge /admin/content/merge/:id(.:format) {:controller=>"admin/content", :action=>"merge"}
But the new or edit action is being invoked as I can see.
Apparently, my route is wrong, isn't it?
Could you please help me with this.
Thanks in advance!
Update
Up-to-date new.html.erb:
<% #page_heading = _('New article') %>
<%= render "admin/shared/edit", { :form_type => "article", :form_action => { :action => "new", :id => #article.id , :class => ('autosave')} } %>
<% if current_user.admin? and !#article.id.nil?%>
<%= form_tag "/admin/content/merge/#{#article.id}" do %>
<h4><%= _("Merge Articles") %></h4>
<%= label_tag :merge_with, 'Article ID' %>:
<%= text_field_tag :merge_with %><br />
<%= submit_tag "Merge" %>
<% end %>
<% end %>
Read the hint from the course:
HINT:Nesting is invalid in HTML.
That means that you can't nest form tags, don't put the form tag in another form tag, your nested form wont be able to do a correct action.
Since you have to put your code at the end of the page, try and see how to do it with having your merging form tag below the main edit article form tag. So basically you can find where the big form tag ends and put it below it.
Try to see if you can figure it out, and if not, don't hesitate to ask :)
Btw. I think everybody had some problem with this

Ajax form not showing up with ruby on rails form_remote_tag

This is all new territory for me, but I am working through a Rails book that was written before start_form_tag was deprecated and I am running into problems with the books example code using remote_form_tag. I have the other forms working but can't get this one up and running. Here's the code:
<h1>Categories</h1>
<ul id="category_list">
<%= render :partial => 'category', :collection => #categories %>
</ul>
<br/>
<p id="add_link"><%= link_to_function("Add a category", "Element.remove('add_link');
Element.show('add_category')") %></p>
<div id="add_category" style="display:none;">
<%= form_remote_tag(:url => {:action => 'new'},
:update => "category_list",
:position => :bottom,
:html => {:id => 'category_form'}) %>
Name: <%= text_field "category", "name" %>
<%= submit_tag 'Add' %>
<%= end_form_tag %>
</div>
This is exactly how it appears in the book, and doesn't compile. I've tried changing to match the form_tag blocks but the form enclosed by the "add_category" div never shows up.
Thanks!
UPDATE: Just found that it doesn't appear that the prototype script is not getting loaded.
both the remove and show methods on Element are showing up as undefined in Firebug. I am not sure why it is not showing up though.
I left out the = sign when including the javascript libraries in standard.html
Line should be this:
<%= javascript_include_tag :defaults %>
Vote to close this question?

Resources