I have a site built using the railstutorial as a template. I have added a search controller to allow me to perform site searches and redirect the user to a search view. When I go to the path '/search' it is as expected (no results) but if I actually use the search input box I get logged out and have to log back in. What would be causing this?
My form:
<form action="/search" method="POST" class="navbar-search pull-right">
<input name="query" type="text" class="search-query" placeholder="Search">
</form>
My search controller:
class SearchController < ApplicationController
def index
unless params[:query].nil?
#results = ThinkingSphinx.search params[:query]
else
#results = []
end
end
end
My view:
<% unless #results.empty? %>
<table class="table">
<% #results.each do |result| %>
<tr>
<% if result.class.name == "Event" %>
<td><%= link_to result.name, organisation_event_path(result.organisation, result.slug) %></td>
<td><%= result.summary %></td>
<% end %>
</tr>
<% end %>
</table>
<% else %>
<p>No results found.</p>
<% end %>
My route:
match '/search', to: 'search#index'
It's related to CSRF issues. You can read the details here, or this SO question/answer.
Related
I'm working on Rails 3 app. Here a view displays all the excel files uploaded by user. A button to execute all the checkbox selected files was already present with delete for each file separately. Now I'm supposed to add a 'delete' button to delete the selected files. I've added the button n modified the function called by this form jus to display "in execute" and "in delete" for now to check if the second 'delete' button is functioanal. But every time delete is clicked it prints "in execute" only in cmd. I guess the AJAX related code written in the view 'list' is the problem.
Pls help!! Tell me why is it going to execute always?
The related code is here:
PS: I hav used if params[:commit]="Delete" & if params[:delete_button] also in controller.rb to but didnt help
list.html.erb (the view that displays all files)
<% if #files.length > 0 %>
<h2 id='comments'>Uploaded Excel Files are as listed below for Edit/Delete/Execution</h2>
<div id='checkone' class='hide'>Please check atleast one excel file to execute</div>
<% ajax_str = "new Ajax.Request('/account/execute_testcases', {asynchronous:true, evalScripts:true, onComplete:function(request){adjust_sidebar();Element.show('msg');Element.hide('waitid');Element.hide('disableexecuteid');Element.show('executeid');}, onLoading:function(request){Element.show('waitid');Element.hide('msg');Element.hide('executeid');Element.show('disableexecuteid');}, parameters:Form.serialize(this)}); return false;".html_safe %>
<%= form_for 'file_names', :url => {:controller => 'account', :action => 'execute_testcases'}, :remote => true, :html => {:name => 'frmExecute', :onsubmit => ajax_str }, :id =>'execute_tc' do |f| %>
<table>
<% if #file_count > 1 && #error_in_all_files == false %>
<tr>
<td>
<input type='checkbox' name='chkAll' onclick='checkAll();'>
<span class='text'>Check All/Decheck All</span>
</td>
</tr>
<% end %>
</table>
<table class='upload'>
<% for a in #files %>
<tr>
<td>
<% file_id = a.id.to_i %>
<% if(#excel_errors[file_id].nil? || #excel_errors[file_id].empty?) && a.file_type.to_i != 1 %>
<input type='checkbox' name = "excelfile[]" value="<%= a.excel_filename %>,<%= a.excel_filename_with_timestamp %>">
<% else %>
<input type='checkbox' name = "excelfile[]" value="<%= a.excel_filename %>,<%= a.excel_filename_with_timestamp %>" disabled=true>
<% end %>
<a href="open_excel_file/<%= a.id %>" title='Click to open' class='nodecoration'><%= a.excel_filename %></a>
</td>
<td>
<%= link_to(image_tag("/images/b_edit.png", :border => 0, :title => 'Edit'), :action => 'upload_file', :file_id => a.id) %>
</td>
<td>
<img src='/images/b_drop.png' border=0 title='Delete' onclick="return confirm('This will delete the related reports too. Are you sure to delete?');">
</td>
<td>
<%
if !#excel_errors[file_id].nil? && !#excel_errors[file_id].empty?
#joined_excel_errors = #excel_errors[file_id].join(', ')
%>
Error
<% end %>
</td>
</tr>
<tr id="excel_error_<%=file_id %>" style='display:none;'>
<td colspan=4>
<% if !#excel_errors[file_id].nil? && !#excel_errors[file_id].empty? %>
<div class="padder">
<% for error_value in #excel_errors[file_id] %>
<font color='maroon'><%= error_value %></font><br>
<% end %>
</div>
<% end %>
</td>
</tr>
<% end %>
<tr><td> </td></tr>
<tr>
<td>
<% if #error_in_all_files == false %>
<span class='executebutton' id='executeid'>
<%= f.submit "Execute", name: 'execute_button', :onclick =>"return checkSelected();" %>
</span>
<span class='deletebutton' id='deleteid'>
<%= f.submit "Delete", name: 'delete_button', :onclick =>"return checkSelected();" %>
</span>
<% end %>
<span id='disableexecuteid' class='executebutton' style='display:none;'>
<input type='submit' value="Execute" disabled="disabled">
</span>
<span id='waitid' style="display:none;" class='text'>
<br>Executing Test Cases...Please wait...<%= image_tag("/images/wait26trans.gif", :border => 0) %>
</span>
<span id='msg' style="display:none;" class='text'>
<br><br> Click here to <%= link_to 'View Test Results', {:controller => 'account', :action => 'recent_test_results'}, :class => 'brownlink' %>
</span>
</td>
</tr>
</table>
<span id='subject_list'>
</span>
<% end %>
<% else %>
No test case sheets found!
<br><br>
<%= link_to '>> Upload File', {:controller => 'account', :action => 'upload_file'}, :class => 'brownlink' %>
<% end %>
<% for i in 1..10 %>
<div> </div>
<% end %>
controller.rb
def execute_testcases
if !params[:execute_button].nil?
puts "in execute"
# file_names = []
# originalfile_filewithtime = []
# original_file_map = {}
# originalfile_filewithtime = params[:excelfile]
......
# SOME CODE HERE
......
# render :update do |page|
# page.replace_html :subject_list, :partial => 'show_output', :locals => {:new_file_map => #new_file_map}
# page.visual_effect :highlight, 'subject_list', :duration => 2
# flash[:display]=#execmsg
#end
# puts #execmsg
elsif !params[:delete_button].nil?
puts "in delete"
end
end
I realised it had to do something with the Form.serialize(this) of AJAX which serialises everything. ie It calls the function of first button in the view irrespective of no of submit buttons present
I have a problem refreshing a partial with fields_for inside.
Here is the code of the partial ('table_detalle')
<table class="table">
<thead>
<tr>
<th><%= t('.denominacion') %></th>
<th><%= t('.cantidad_ingreso') %></th>
<th><%= t('.importe') %></th>
</tr>
</thead>
<tbody>
<%= f.fields_for :operacion_detalles do |builder| %>
<tr>
<%= render 'table_detalle_operacion', f: builder %>
</tr>
<% end unless #operacion.nil? %>
</tbody>
</table>
<%= content_tag :h3, t('.total', :value=> number_to_currency(#operacion.R_IMPORTE)).html_safe, :class => 'pull-right', style: 'display:inline' %>
when the user change a value of a combo i would like to refresh the partial above (because the details objects must change and are editable)
Here is the javascript code:
$('#operacion_TIPOVALOR_ID').change(function(){
$.ajax({
url: '<%= cambiar_tipo_valor_movimientos_path %>',
data: {tipo_valor_id: $('#operacion_TIPOVALOR_ID').val()},
complete: function(){
$('#tipo_valor_loader').css('display','none');
},
beforeSend: function(){
$('#tipo_valor_loader').css('display','inline');
},
success: null,
dataType: 'script'
});
});
the controller code:
def cambiar_tipo_valor
#operacion = Operacion.new
denominaciones = TipoValorDenominacion.all_from_tipo_valor params[:tipo_valor_id]
denominaciones.each do |deno|
#operacion.operacion_detalles.build :tipo_valor_denominacion => deno, :I_CANTIDAD => 0, :R_IMPORTE => 0
end
end
as you can see the "operacion_detalles" change depending on the user selection.
the .js.erb code:
$('#detalle').html('<%= escape_javascript(render :partial => 'table_detalle') %>');
But, i get:
undefined local variable or method `f' for #<#<Class:0x45ae1c8>:0x5062338>
So, i need the f variable to render the partial. Is there any way to emulate the f variable?
Thanks in advance.
I had no other choice than resolve the problem by hand. What i mean?
Instead of a field_for => each_with_index:
<% #operacion.operacion_detalles.each_with_index do |detalle, index| %>
<tr>
<%= render 'table_detalle_operacion', detalle: detalle, index: index %>
</tr>
<% end -%>
The use the tags helpers like this:
<td>
<input type="hidden" value="<%= detalle.tipo_valor_denominacion.R_MULTIPLICADOR %>" class="multiplicador"/>
<%= hidden_field_tag "operacion[operacion_detalles_attributes][#{index}][TIPOVALORDENO_ID]", detalle.TIPOVALORDENO_ID %>
<%= detalle.tipo_valor_denominacion.CTIPOVALORDENO %></td>
<td>
<%= text_field_tag "operacion[operacion_detalles_attributes][#{index}][I_CANTIDAD]", detalle.I_CANTIDAD %>
<%= content_tag(:span, detalle.errors[:I_CANTIDAD].join(', '), class: 'help-block') if detalle.errors.has_key? :I_CANTIDAD %>
</td>
<td>
<%= hidden_field_tag "operacion[operacion_detalles_attributes][#{index}][R_IMPORTE]", detalle.R_IMPORTE %>
<%= content_tag :span, number_to_currency(detalle.R_IMPORTE), class: 'detalle-importe pull-right' %>
<%= content_tag(:span, detalle.errors[:R_IMPORTE].join(', '), class: 'help-block') if detalle.errors.has_key? :R_IMPORTE %>
</td>
In this case i had supply the names, so when this code is executed
$('#detalle').html('<%= escape_javascript(render :partial => 'table_detalle') %>')
It does not need the f variable.
Hope it helps.
pd: I know this is not a very good solution but it works.
I've used this site plenty of times to find my answer but im am unfortunately stuck now and need some help. On my main content page I am working on im running into an issue with creating a tables using CSS and div ids.
Overview
The table is compromised of 2 rows and 6 columns and then spilt into 12 boxes which each box 200px X 200px. Now the content of each box contains a new item (#books) which is generated from a separate controller(used the built in generate scaffold command). I am also using pagination to limit the items on the page (12) so older items just roll on back.
Issue
I am able to populate each of the 12 boxes with new content by using the following code on my controller and view
controller
def content
#books= Book.paginate(:page => params[:page], :per_page => 12)
respond_to do |format|
format.html # content.html.erb
end
view {content.html.erb}
<% #books.each do |book| %>
<div id="column1">
<tr>
<td><%= book.name %></td>
<td><%= book.title %></td>
<td><%= book.content %></td>
<td><%= link_to 'Show', book %></td>
</tr>
</div>
<% end %>
<br />
<div id="paginate">
<%= will_paginate(#books,
) %)
</div>
</div>
What happens when I generate the view is that everything is rendering perfectly but all of my books are using the same 'column 1'
<div id="column1">
<tr>
<td>Title</td>
<td>Name</td>
<td>Content</td>
<td>Show</td>
</tr>
</div>
<div id="column1">
<tr>
<td>Title </td>
<td>Name</td>
<td>Content</td>
<td>Show</td>
</tr>
</div>
I would like for each book to have its own column {2, 3,4,...12} so i can target each individually if i need to using jquery. The other problem is that i would like to use render partial for everything in the but when i use the render partial it places all the items within the 1 column {relisting of Book.all }so it just repeats all the contained information. I have a feeling im suppose to use another method or edit the current one to complete this but I'm unsure what goes were. Thanks!
greenz
Update
Hey Mike i really appreciate your effort in trying to explain to a newby whats going on. I basically went back to step one a applied each piece of code to see where it is going bonkers at. You are doing a wonderful job explaining the issue yet i can't grasp whats going on when rendering the code because the ruby doc's for rendering and layout are pretty basic and dont have any examples where i can see how div_for tag effects the rendering process (I'm a visual person). I wanted to show you the new code but comments don't allow it so i opened an answer :(
The first edit was removing the
<% #books.each do |book| %>
and replacing with
<table>
render #books
</table>
so now my code looks like this {content.html.erb}
<div id="maincontent">
<table>
render #books
</table>
<div id ="column1">
<tr>
<td><%= book.name %></td>
<td><%= book.title %></td>
<td><%= book.content %></td>
<td><%= link_to 'Show', book %></td>
</tr>
</div>
<% end %>
<br />
<div id="paginate">
<%= will_paginate(#books,
) %>
</div>
</div>
When this is applied i receive an unexpected end error when rendering the view pointing to the last line of my code.(When adding the end tag to the end it just threw another error) So i then changed the render page from _bookcontent.html.erb to _book.erb and inserted the partial between the column1 div as shown.
<div id ="column1">
<%= render :partial => "book" %>
</div>
<% end %>
I understand what this tag is now doing it telling my partial that for every "book" render the latest book id from the database. So now the render #books statement is keeping track of the books id for every render page i insert. (I could be mistaken but this is how i perceive it). So now onto the infamous
_book.erb that this page is rendering here is my code.
<% div_for(book) do %>
<tr>
<td><%= book.name %></td>
<td><%= book.title %></td>
<td><%= book.content %></td>
<td><%= link_to 'Show', book %></td>
</tr>
<% end %>
From my reading on the <% div_for(book) do %> is wrapping a unique div id around each book such as
<div id="book_123"> Book.name / Book.Title </div>
This div is now contained inside my column1 div ID from before which has all my styling for my column/boxes. From the lack of information on the div_for tag I am still running into errors and unexpected ends in my code. (Trying to debug the _book.erb with ruby -w doesn't work so i dont know how to use the end tags). These are the take aways i am trying to correct for my sanity.
Should I remove the <div id="column1"> from my content page and apply a new div id / class elsewhere on my content page?
Where do i insert <% end %> tags one in the partial and in my content.html.erb before my pagination? It seems as though when i have both the renderer complains about unexpected ends at the end of each page.
When substituting your code in place of mines i receive the above unexpected end but this isn't due to you im definitely missing a component in my code.
I could be approaching this the wrong way from a layout perceptive in the first place but my main goal is to have 2 rows of 6 boxes/columns w/ each box containing a different book. Then using pagination to scroll back to the earlier books and apply the same layout which it does fine now but i am using the same column id for each book and need to target all the boxes separately.
If I understand you correctly, this is just a case of it doing what you said, not what you meant.
Whatever controller has the content method should look like this:
def content
#books = Book.paginate :page => params[:page], :per_page => 50
end
Your view should look like this(old syntax but still should work fine).
content.html.erb:
<div id="maincontent">
<table>
<%= render :partial => 'book', :collection => #books %>
</table>
<br />
<%= will_paginate #books %>
</div>
Or try the new syntax. The new syntax is weird and I haven't tried it personally but I think this should work. So here is the alternate, more Rails 3 version of content.html.erb:
<div id="maincontent">
<table>
<%= render #books %>
</table>
<br />
<%= will_paginate #books %>
</div>
I think it chooses the partial based on the class name and whether it is enumerable.
The _book.html.erb partial:
<%= div_for(book) do %>
<tr>
<td><%= book.name %></td>
<td><%= book.title %></td>
<td><%= book.content %></td>
<td><%= link_to 'Show', video %></td>
</tr>
<% end %>
Div_for is the secret sauce.
The only end that is required is the one inside the partial to close the block opened when we called do after the div_for.
Now i am getting unexpected end errors when i render the page here is my view
view = content.html.erb
<div id="maincontent">
render #books
<div id="column1">
<tr>
<%= render :partial => "bookcontent" %>
</tr>
</div>
<% end %>
<br />
<div id="paginate">
<%= will_paginate(#books,
) %>
</div>
</div>
Here is my render partial = _bookcontent.html.erb
<% div_for(book) %>
<tr>
<h3><%= book.name %></h3>
<h3><%= book.title %></h3>
<h3><%= book.content %></h3>
<h3><%= link_to 'Show', book %></h3>
</tr>
<% end %>
I am also clueless as to if i should be using a div id or class. By default i always use just an id.
If I understand correctly, your code is working fine but you want to have:
column1
column2
column3
...
for each book. If that is the case, why don't you simply replace:
<div id="column1">
with
<div id="column#{#book.id}">
or use a counter
<%
book_counter=0
#books.each do |book|
book_counter = book_counter+1
%>
<div id="column#{book_counter.to_s}">
first off ive tried google and cant seem to find an answer i can understand what i would like to do is list my questions from an active record witch i have that much working then make the question its self a link so her is my page so far!
<h1>Listing faqs</h1>
<table>
<% #faqs.each do |faq| %>
<tr><td width="90px"><div id="right">Question : </div></td><td><%= faq.question %></td></tr>
<% end %>
</table>
<br />
<table>
<% #faqs.each do |faq| %>
<tr><td width="90px"><div id="right">Question : </div></td><td><%= faq.question %></td></tr>
<tr><td valign="top"><div id="right">Answer : </div></td><td><%= faq.answer %></td></tr>
<% end %>
</table>
<br />
<%= link_to 'New Faq', new_faq_path %>
im amusingenter code here i can stick something in before faq.question like a link_to or something or something but im not shure what
First of all, in Rails 3 forms should start with <%=, not <%.
<%= #faqs.each do |faq| %>
<tr>
<td width="90px">
<div id="right">Question : </div>
</td>
<td>
<%= link_to(faq.question, faq_path(faq.id) %>
</td>
</tr>
<% end %>
I have an action called 'list' and is defined like
def list
#subjects = Subject.order("subjects.position ASC")
end
And my list view looks like
<div>
<h2>Subjects</h2>
<%= link_to("Add new subject",{:action=>'new'},:class=>'action new')%>
<table class="listing" summary="Subject list">
<tr class="header">
<th> </th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<%= #subjects.each do |subject| %>
<tr>
<td><%=subject.position%></td>
<td><%=subject.name%></td>
<td class="center"><%=subject.visible ? 'Yes':'No' %></td>
<td class="center"><%=subject.pages.size%></td>
<td class="actions">
<%=link_to "Show",{:action=>'show',:id=>subject.id},:class=>'action show'%>
<%=link_to "Edit",{:action=>'edit',:id=>subject.id},:class=>'action edit'%>
<%=link_to "Delete",{:action=>'delete',:id=>subject.id},:class=>'action delete'%>
</td>
</tr>
<%end%>
</table>
</div>
So the problem is that besides getting an organized list of all the subjects nearby my link "Add subject" the page is printing all the information that #subjetcs brings from the controller.
Which of course is not a very good idea...I can't figure out why is that happening
Your problem is the line <%= #subjects.each do |subject| %>. Because you've got <%=, the return value of that line - which is #subjects - should be written into the document.
Use <% #subjects.each do |subject| %> instead to suppress the output.
<%= #subjects.each do |subject| %>
should be:
<% #subjects.each do |subject| %>
The equals sign tells ERB that the code should output something. You want to omit that in the iterator.
Remove the equals sign from the <%= #subjects.each do |subject| %> line.
ERB outputs everything within <%= ... %> tags to the page. So in this case, the return value of #subjects.each is written to the page. Thus, write your loop like this instead:
<% #subjects.each do |subject| %>
...
<% end %>
This applies to all statements that do stuff rather than output stuff to the page. For example, you would write an if statement as:
<% if some_condition %>
...
<% else %>
...
<% end %>
rather than as:
<%= if some_condition %>
...
<% else %>
...
<% end %>