Ruby: Display Attributes - ruby-on-rails

I have the following in a view file:
<%= render :partial => "medium_deal", :collection => category_locales, :as => :deal_locale %>
In my partial template file, I want to print out all attributes for deal_locale. How can I do that? (Basically, a var_dump in PHP)

I'm assuming this is just for debugging purposes? If so
<% logger.info "#{deal_locale.inspect}" %>
should do the trick.
Or, if you really want to see it in your browser, just
<%= "#{deal_locale.inspect}" %>

Related

link_to_function does not react to click in rails 3.1.0 & simple_form

Here is the code in rfqs/_form_new.html.erb to add a selection box for standard.
<%= simple_form_for #rfq do |f| %>
<div id="std">
<%= render :partial => 'standards/standards', :collection => #rfq.standards, :locals => { :f => f } %>
</div>
<%= link_to_function "Add Std", nil do |page| %>
page.insert_html :bottom, :std, :partial => 'standards/standards'
<% end %>
<% end %>
The html source code has # after href and cause no reaction for clicking the link.
Add Std
The _standards.html.erb partial looks like:
<%= f.association :standards, :collection => Standard.active_std.all(:order => 'name'), :label_method => :name, :value_method => :id %>
Any thoughts about missing link after href? Thanks.
I don't believe 3.1's link_to_function works the same way as older versions; I don't see anything in the source that utilizes a block.
This seems in keeping with using unobtrusive JavaScript. The Rails pulls related to accepting a block seem more related to link text rather than injecting JS.

Javascript inside view in rails 3

I need to put some javascript inside a view. Basically I am having a play with the Recurly.js library.
Here is an example form: http://pastie.org/3142536
I have tried adding the JS to a partial: filename _recurly.js.erb
<%= render :partial => 'recurly.js', :locals => { :company => #company } %>
But its just outputting the JS to the page.
Is there a better way of doing this passing vars to JS for output and how can I get it to render the JS ?
Hope you can advise.
You need to use the function escape_javascript.
so your code should be:
<%= escape_javascript(render(:partial => 'recurly.js', :locals => { :company => #company }) %>
have you tried:
<%= raw(render :partial => 'recurly.js', ...) %>
Normally ERB will escape the output. Not sure if this is a good solution in the long run, even if it works, though.
why don't you use normal way of including javascript in your ERB view.
<%= javascript_include_tag "filename_from_public_javascripts_folder" %>

Issue working with partial in rails

I am using rails 3.1. I have a view products/show.html.erb and I call a partial like this
<%= render 'productrelationships/relatedproduct',:collection => #product.relatedproducts %>
and i access it in this way inside my partial (productrelationship/_relatedproduct)
<% logger.error 'Related Products ' + relatedproduct.inspect %>
The inspect returns a nil. But if I try the same inside my show.html.erb, it is not nil. There is some mistake in passing the value. What am I doing wrong?
Found the answer. It started working when i added :partial while rendering
<%= render :partial => 'productrelationships/relatedproduct',:collection => #product.relatedproducts %>
Need to specify the local variable.
<%= render :partial => 'productrelationships/relatedproduct',
:collection => #product.relatedproducts,
:as => :relatedproduct %>

Rails partial view set value of form field

Okay so I am quite new to Rails and am trying to do the following without success:
I have an Object (from my Active Record) containing a project, which contains n sub-projects, which contain n tasks. Now for each of these I want a partial view.
So I render from the project view the sub-project with the following code:
<%= render(:partial => 'subproject', :collection => #project.sub_projects) %>
Within my sub-project partial view called _subproject.rhtml (adding the code to a good ol Rails 1.2.3 project), so I can access the data like this:
<%= subproject.name %>
That will print out the name alright but when I try to generate a textfield this won't work:
<%= text_field 'subproject', 'name' %>
But this will:
<%= text_field 'subproject', 'name', :value => subproject.name %>
What am I doing wrong?
Edit: Changed title due to my problem is not passing the value but displaying it within a form field.
Edit2: As requested my controller code:
#project = Project.find(params[:id])
You can write this:
<%= render(:partial => 'subproject', :collection => #project.sub_projects) %>
as
<%= render :partial => #project.sub_projects %>
This will render every sub project with the sub_projects/_sub_project.html.erb partial. A little shortcut.
This:
<%= text_field 'subproject', 'name' %>
Says create a text_field called: subproject[name], but doesn't give it a value. You need to pass the value you want to set (the code that works).
The more idiomatic way of doing this now is with form_for:
<% form_for #subproject do |f| %>
<%= f.text_field :name %>
<% end %>
Or if you're using formtastic (https://github.com/justinfrench/formtastic), which is fantastic, you'd write:
<% semantic_form_for #subproject do |f| %>
<%= f.input :name %>
<% end %>
I hope this helps!

RoR: Replace_html with partial and collection not functioning

I am trying to create a tabbed interface using the prototype helper method "replace_html." I have three different partials I am working with. The first one is the 'main tab' and it is loaded automatically like so:
<div id = "grid">
<% things_today = things.find_things_today %>
<%= render :partial => "/todaything", :collection => things_today, :as =>:thing %>
</div>
...which works fine. Similarly, I have a _tomorrowthing partial which would replace the content in the 'grid' div like so:
<%things_tomorrow = things.find_things_tomorrow%>
<%= link_to_function('Tomorrow',nil, :id=>'tab') do |page|
page.replace_html 'grid' , :partial => '/tomorrowthing',:collection => things_tomorrow, :as => :thing
end %>
If I click on this tab nothing happens at all. Using firebug, the only errors I find are a missing ) after argument list which is contained in the Element.update block where the link_to_function is called. What am I doing wrong?
Hey Jack i try to reproduce the same but i can't i never used link_to_function before but
Following code may help to achieve the same you want
<%= link_to_remote "Today Thing", :url=>{:action=>"things", :id=>'today'}%>
<%= link_to_remote "Tomorrow Thing", :url=>{:action=>"things", :id=>'tomorrow'}%>
<div id = "grid">
<% #things = things.find_things_today %>
<%= render :partial => "/todaything", :collection => #things %>
</div>
in controller
def things
#things= (params[:id]=="today")? things.find_things_today : things.find_things_tomorrow
render :update do |page|
page.replace_html 'grid', :partial => (params[:id]=="today")? "/todaything" : '/tomorrowthing' , :objects=> #things
end

Resources