On 25 Aug 2010, at 10:42, Jeff Chen <jeffch...@gmail.com> wrote:
>>
>>
>
> I write the config.gem 'will_paginate' in the inside of
> environment.rb, but got below error msg:
>
> undefined local variable or method 'config' for main Object
> <NameError>
>
> How to fix it? Thanks.
>
You're probably putting it in the wrong place. What does your environment.rb look like
Fred
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
My code
CONTROLLER
def index
@contract = Contract.new(params[:contract])
page = (params[:page] ||= 1).to_i
@contracts = Contract.search_index({:page => page})
@legal_entities = LegalEntity.all(:select => "CD_PESSOA_JURIDICA,
NM_PESSOA", :joins => [:person])
@persons = Person.all
respond_to do |format|
format.html # index.html.erb
end
end
def list
@contract = Contract.new(params[:contract])
page = (params[:page] ||= 1).to_i
@contracts = Contract.find_by_params(params[:contract])
@legal_entities = LegalEntity.all(:select => "CD_PESSOA_JURIDICA,
NM_PESSOA", :joins => [:person])
@person = Person.all
render :index
end
VIEW
<% form_for(@contract, :url => { :action => "list"}, :onKeyPress =>
"submit();" ) do |f| %>
<!-- Início da Tabela de Filtro -->
<table class="Cabecalho" border="0">
<thead>
<tr>
<td colspan="4" class="titulos">Empresa:<br/>
<%= select(:contract, :CD_PESSOA_JURIDICA,
@legal_entities.collect{ |p| [
p.NM_PESSOA, p.CD_PESSOA_JURIDICA]},
{ :include_blank => true }) %>
</td>
</tr>
<tr>
<td width="30%" class="titulos">Número do contrato:<br/>
<%= f.text_field(:CD_CONTRATO, :style=>"width: 90%;",
:maxlength=>"7", :onKeypress => "apenasnum(this);")%>
</td>
<td width="20%"class="titulos">Ano:<br/>
<%= f.text_field(:NO_ANO, :style=>"width: 90%;", :onKeypress =>
"apenasnum(this);")%>
</td>
<td width="40%"class="titulos">Objeto do contrato:<br/>
<%= f.text_field(:DS_OBJETO_CONTRATO, :style=>"width: 80%;",
:onKeypress => "apenastex(this);")%>
</td>
<td width="9%" align="right" style="padding-right:35px;">
<%= image_submit_tag("lupa.png", :title => "Pesquisar")%>
</td>
</tr>
</thead>
</table>
<br/>
<div class="tab_botao">
<%= link_to ( image_tag("incluir.jpg",:style=>"width: 16px;
heigth:16px; border:0", :title =>"Incluir Novo"))+' Incluir Novo',
new_contract_path %>
</div>
<% if @contracts.empty? %>
<div class="div_registro">
<p>Nemhum contrato foi encontrado</p>
</div>
<% else %>
<table cellpadding="0" cellspacing="1" class="Cabecalho">
<thead>
<tr class="Cabecalho_bg">
<th align="left" style="padding-left: 5px;">Numero Contrato</th>
<th align="left" style="padding-left: 5px;">Ano</th>
<th align="left" style="padding-left: 5px;">Empresa</th>
<th align="left" style="padding-left: 5px;">Objeto</th>
<th align="left" style="padding-left: 5px;">Termo Aditivo</th>
</tr>
</thead>
<tbody class="zebra">
<% @contracts.each do |contract| %>
<tr>
<td style="padding-left:5px;"><%= link_to
contract.CD_CONTRATO, contract %></td>
<td style="padding-left:5px;"><%= contract.NO_ANO %></td>
<td style="padding-left:5px;" ><%=
contract.legal_entity.person.NM_PESSOA %></td>
<td style="padding-left:5px;"><%=
contract.DS_OBJETO_CONTRATO %></td>
<td style="padding-left:5px;"><b><%=
"#{contract.additiv_contracts.size} aditivos" %></b></td>
</tr>
<% end %>
</tbody>
<tr class="Cabecalho_bg">
<td colspan="6" class="Result">
Total de <b><%= @contracts.total_entries %></b> ítens.
</td>
</tr>
<tr>
<td colspan="6" class="paginacao">
<%= will_paginate @contracts %>
</td>
</tr>
</table>
<% end %>
<% end %>
Cheers
--
Posted via http://www.ruby-forum.com/.
Seems like this should all be controller work (well, not quite, and this
is based on the mislav-will_paginate gem)...
You could use a generic
@contracts = Contract.paginate :conditions => cond, :page =>
params[:page]
where cond is your scoping criteria, such as:
cond = ['name LIKE ?', some_params_entry] or
cond = ['1=1'] for the everything case
The only difference in your two methods is the Contract.find...
Not knowing what the search_index or find_by_params methods do
precisely, could you create a single build_conditions method in the
Contract model that returns 'cond' and covers all your search cases to
simplify your logic?
index and list are awfully similar... do you need both?
I don't need both, I tried but I had no success using generic
I think I've done something wrong in model.
Using generic i have to modify my model?
I'm quite confused with that, because I usually use will_paginate
without filter.
Cheers
Skinny controllers, fat models.
Given scoping criteria that may be coming in params, the model should
know how to respond to those params for limiting return sets, and can
adapt its 'cond' to account for those. Controller just knows to use the
received cond in its paginate call.
Model:
class Contract
def build_conditions(params)
# the default returns
cond = ['1=1']
page = (params[:page] ||= 1).to_i
per_page = 15
if params[:partial_contract_name]
# return a scoping condition here, this can be as complex
# as you want/require
cond = ['name LIKE ?%', params[:partial_contract_name]
end
return cond, page, per_page
end
end
Controller:
def index
@contract = Contract.new(params[:contract])
cond, page, per_page = @contract.build_conditions(params)
@contracts = Contract.paginate :conditions => cond, :page => page,
:per_page => per_page
@legal_entities = LegalEntity.all(:select => "CD_PESSOA_JURIDICA,
NM_PESSOA", :joins => [:person])
@persons = Person.all
respond_to do |format|
format.html # index.html.erb
end
end
should be a nice generic implementation using will_paginate. Again, I
don't know what was happening in your search_index or find_by_params
methods.
The correct way is config.gem 'will_paginate'
but if the will_paginate is already installed should have worked.
config.gem must go inside the initializer.run block, not just before the end.
Post your environment.rb if you think you have it correctly, also the output of
gem list --local
and the full error that you are getting.
Colin
Please don't top post it makes it more difficult to follow the thread Thanks
>...
That looks OK. When you run script/server I presume you do not get an
error about missing gems? Or any other error?
>
> 2. The output of gem list --local:
> will_paginate <2.3.14>
OK
>
> 3. The full error MSG:
> NoMethodError in ManageController#index
>
> undefined method `paginate' for #<Class:0x679f940>
What is the path and filename of that controller? Can you post the
code for the index method?
Colin
Did you note the first request in my previous email - not to top post?
That means not to post your new message before the quotes of the
previous message, but to insert your reply into the previous message.
Now I have scroll up and down this email looking at what I asked and
back up to see your response. It also makes it less likely that you
will forget to answer a question.
In particular you have not confirmed that you do not get any errors
when you start the server.
Also please can you confirm that if you remove the calls to paginate
and just fetch all records that all works as expected.
Further comments below.
>
> class ManageController < ApplicationController
>
> def index
> list
> render :action => 'list'
> end
Again I am having to cut and paste from below, if you had not top
posted I would not have had to do this. You previously said the error
is
>> > NoMethodError in ManageController#index
>> > undefined method `paginate' for #<Class:0x679f940>
I don't see any call to paginate so that is a bit odd. Please confirm
exactly what is happening and make sure the code you post matches the
error message. Post the full error trace for the message please.
And once again please don't top post.
Colin
I don't know but try, check the will_paginate version and write in
evironment.rb between the "Rails::Initializer.run do |config|" and the
last end this
config.gem 'will_paginate', :version => 'your version'
and restart the server and check
Right so the error is in list, that is why I asked for the whole error message.
> app/controllers/manage_controller.rb:4:in `index'
> c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
> c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
> c:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
> c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
> c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
> c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
> c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
> c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
> c:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
> c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
>
I see that the error is from Item.paginate. You have not told us
anything about the Item class. Can you show us the code for that
class (delete methods that are not relevant before posting) and
confirm the path and filename to that class.
I am running out of ideas however.
Colin
No, what you have looks ok.
Only one more suggestion I think, I seem to remember some-one having a
similar problem and it went away when they used Mongrel instead of
Webrick, though I do not understand why. I can only suggest you try
that, so start the server with
ruby script/server --mongrel
and see what happens. I always use mongrel as for me it loads much faster.
Colin
I don't know, it just worked for me, in fact it defaults to mongrel on
my Ubuntu install. Have you tried googling for rails install mongrel?
Colin
Can you post the complete output seen in the terminal when you start
mongrel and enter localhost:3000/manage in the browser.
>
> then I run the localhost:3000/manage then got the error msg:
> undefined method `paginate' for #<Class:0x60f629c>
>
> I checked the system status of mongrel, which is empty. which means
> the system is not active when I run above commands.
What do you mean by the system status of mongrel? If mongrel is not
running I do not see how you get any output at all in the browser.
Also please post complete output of
gem list --local
Colin
You have not provided this information.
Could the fact that you have paginate and will_paginate installed be
an issue? It might be worth removing this one.
Colin
So what happened to the problem running mongrel?
Colin