undefined local variable or method `div' for #<#<Class:0x10923faf0>:0x109226b18> - ruby-on-rails-3

I am having trouble with this as I get started with Ruby On Rails. Any help would be great. Additional info:
Extracted source (around line #25):
</header>
<div class="container">
<% flash.each do |key, value| %>
<%= content_tag(div, value, :class=> "alert alert-#{key}") %>
<% end %>
<%= yield %>
</div>

The line should be <%= content_tag(:div, value, :class=> "alert alert-#{key}") %>
Notice I'm passing in a symbol :div instead of just div. The error just means its trying to look for a variable or method named div but content tag accepts a symbol or string I believe. I think it will work with "div" as well.

Related

Stuck on linking to a url in the database, Rails

I tried both these:
<% #candidates.each do |candidate| %>
<h4> <%=candidate.full_name %></h4>
<p>Links: <%= link_to "facebook", "#{candidate.link1}" %> </p>
<% end %>
<% #candidates.each do |candidate| %>
<h4> <%=candidate.full_name %></h4>
<p>Links: <%= link_to "facebook", "candidate.link1" %> </p>
<% end %>
With the first one, the string interpolation, I get returned an facebook, ignoring the second part of the link_to altogether.
With the second one, where the link1 is an attribute of the class Candidate, I get an error No Route matches candidate.link1.
What syntax do I need so the second part of the link_to is seen as a url in the database (in this case: "https://facebook.com/......") instead of a route?

How to check if an ActiveRecord table entry is empty

I am not sure if the title of this question uses proper jargon, but hopefully this description will help. If you need more information, please let me know.
I am taking Chinese text from a form and splitting it into a 2d array of sentences and words. I then want to define all the words using dictionary entries in my database. Some of the words aren't in the database, so I want to check for that. What I am trying isn't working.
Here is my current code:
<% #lesson.parsed_content.each_with_index do |sentence, si| %> #iterate 1st dimension
<% sentence.each_with_index do |word,wi| %> #iterate 2nd dimension
<% sentence = sentence.to_s %>
<div class="word blt" id="<%= wi %>">
<div class="definition blt">
<% definition = DictionaryEntry.where(:simplified => word) %> #search by simplified chinese
<% definition.find_each do |w| %>
<% if w.definition == nil %> # PROBLEM: this never returns true.
<%= word %>
<% else %>
<%= w.definition %>
<% end %>
<% end %>
</div>
<div class='chinese blt'> <%= word %></div>
</div>
<% end %>
<% end %>
How can I change <% if w.definition == nil %> to return true if there is no definition in my database?
This is a shot in the dark but first I would switch your code around when you are converting the variable sentence to a string and looping through it. (unless you have a reason for it being that way)
<% sentence = sentence.to_s %>
<% sentence.each_with_index do |word,wi| %> #iterate 2nd dimension
Second, depending on how your data was put inside the database it might be an empty string instead of nil. So I would change the condition from
<% if w.definition == nil %> # PROBLEM: this never returns true.
to
<% if w.definition.blank? %> # Checks to see if definition is blank
Blank will check if its false, empty, or a whitespace string.
Finally, indentation is helpful especially when running loops and conditionals. It's easier on the eyes and helps you understand what's going on.
<% #lesson.parsed_content.each_with_index do |sentence, si| %>
<% sentence = sentence.to_s %>
<% sentence.each_with_index do |word,wi| %>
<div class="word blt" id="<%= wi %>">
<div class="definition blt">
<% definition = DictionaryEntry.where(:simplified => word) %>
<% if definition.empty? %>
<% word %>
<% else %>
<% definition.find_each do |w| %>
<%= w.definition %>
<% end %>
<% end %>
</div>
<div class='chinese blt'> <%= word %></div>
</div>
<% end %>
<% end %>
Let me know the results.

Get simple search form to work across multiple models without Thinking Sphinx

I have seen a few posts here about searching across multiple models (here and here, in particular). However, I'm wondering if I can adapt Ryan's Railscast #37 to do this for three or four models without having to figure out Thinking Sphinx as I'm a beginner to all this.
As I said I have a few models, although I only reference two for brevity below, and I made a searches_controller with index and show actions.
On the models I included Ryan's model code:
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
And in the searches_controller I have the following:
def index
#profiles = Profile.search(params[:search])
#employers = Employer.search(params[:search])
end
def show
#profiles = Profile.search(params[:search])
#employers = Employer.search(params[:search])
end
In my searches/show.html.erb I have the following:
<%= #profiles.each do |profile| %>
<div class="question">
<div class="questionHeader">
<h5 class="questionTitle"><%= link_to profile.first_name, profile %></h5>
</div>
</div><!-- end question -->
<% end %>
<%= #employers.each do |employer| %>
<div class="question">
<div class="questionHeader">
<h5 class="questionTitle"><%= link_to employer.name, employer %></h5>
</div>
</div><!-- end question -->
<% end %>
When I perform the search, my show renders what looks like three empty arrays [][][].
Without ditching this and moving to Thinking Sphinx is there anything I can do to get this working using the simple form below?
<% form_tag searches_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "#", :name => nil %>
</p>
<% end %>

Rails: Form with configurable number of inputs

I wrote a form that generates a text input for each property.
The list of properties is configurable by the customer.
<% properties = ["refractivity_at_2kHz", "refractivity_at_5kHz"] %>
<% properties.each do |property| %>
<div class="property">
<%= f.label property %>
<%= f.text_field property %>
</div>
<% end %>
It fails with the error undefined method refractivity_at_2kHz.
What is the usual solution for this problem?
Should I add an array to my model, and use f.text_field myarray[property] ?
Is it a form_for(#model)?
Because then f.text_field(property) looks for that method/property on #model.
May be you want to change f.text_field(property) into text_field_tag(property)[1]
cheers
[1] http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag

Is it possible to use partials to be rendered as wrappers in Rails?

I would like to render structures like this:
<tag1>
<tag2 someattribute="somevalue">
<.. lot of things inside ..>
</tag2>
</tag1>
<tag1>
<tag2 someattribute="someothervalue">
<.. different inside things inside ..>
</tag2>
</tag1>
The tag1, tag2 are the same, they are just parametrized. The inner part of the code changes. I tried to implement the thing above like that (haml):
%div{id:['products', id]}
.products_content
%div{id:['products', id, 'content'], class:'products_mask'}
= yield
This was the partial _content_head.html.haml, which is called from a template:
= render 'shared/content_head', id: 'all' do
%h3= Title
%p= Body of the text.
My theory that yield inside the partial would lead to rendering of the passed block did not prove. Is there a way to use partials as code wrappers? Can you suggest me some solution how to reach this? Thank you.
This might be a good use of the capture method.
I'm only familiar with ERB, but here is the general idea:
<% structure = capture do %>
<h3>Title</h3>
<p>Body of text</p>
<% end %>
Then pass the variable into the partial:
<%= render 'shared/content_head', :structure => structure %>
And within the partial, spit out the structure variable:
<%= structure %>
Reset structure multiple times within the view as you render partials (or maybe more appropriately, in a helper?).
I've used the following (Rails 4, but I think it should work with Rails 3 too):
<%# app/views/users/_edit.html.erb %>
<%= render layout: 'modal_wrapping' do |f| %>
<%= f.input :email %>
...
<% end %>
.
<%# app/views/users/_modal_wrapping.html.erb %>
<div id='modal'>
<%= simple_form_for #user do |f| %>
<%= yield f %>
<% end %>
</div>