Devise delete link for an object causes user to sign out - ruby-on-rails-3

I am having a weird issue with Devise. I have lists with tasks on them. When you delete a task all of the sudden I get a template error having to do with the User.
It seems like the delete method causes the session to logout and therefore can't find the User when trying to load the template.
I have no idea why this is happening:
_task.html
<div class="tasks">
<%= div_for task do %>
<%= link_to 'Delete', task_path(task), :method => :delete, :class => "delete", :remote => true %> | <%= content_tag(:div, task.task, :class => "task-body") %>
<% end %>
</div>
destroy.js.erb
$("#task_<%= #task.id %>").fadeOut();
ERROR AFTER RELOADING PAGE, THIS IS FOR THE ACCOUNT, LOGOUT ETC in HEADER
No route matches {:action=>"show", :controller=>"users"}
Extracted source (around line #5):
2: <div id ="login">
3: <ul>
4: <li><%= link_to "Sign out", destroy_user_session_path %></li>
5: <li><%= link_to "Account", user_path(current_user) %></li>
6: <li><%= link_to "Things", user_things_path(current_user) %></li>
7:
8: </ul>
Thanks in advance!

After a little more searching I found the answer here on stackoverflow, so credit should go to justsee from this previous question
The request will not include the required CSRF data, and as of
Rails 3.0.4 the session is silently reset instead of throwing an
ActionController::InvalidAuthenticityToken error.
To fix this include the following in your layout:
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

Related

I am new on rails i not sure what is going on my code but I want to filter data from database by searching using id how can i do it

I am trying generate a code that tracks for documents that revolve within the organisation. I have done other codes for adding employees, adding document types and logging in now I am struggling on creating a document form and search from the document that I have created. This code is for searching:
controller#show
def show
#generate_documents = GenerateDocument.where('Reciever LIKE?',"%#{params[:search]}%")
# #generate_documents = GenerateDocument.all
end
views/show
<%= form_tag generate_document_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search" %>
</p>
<% end %>
<!-- end of seaerch form -->
<!-- loading data from database and displaying then in a list format BEGIN -->
<ul>
<% #generate_documents.each do |generate_document| %>
<li>
<%= link_to generate_document.Reciever, edit_generate_document_path(generate_document) %>
</li>
<% end %>`enter code here`
</ul>
<!--
END LISTING -->
<%= link_to 'New Generate Document', new_generate_document_path %>
Even Iam new to rails, but i can help you with basics that you need to pass the parameters from the view to the controller, therefore you can use:
:url => {:controller => "name_of_your_controller", :action => "action_to_be_perforemed", :id => "whatever id you want"}
or else you can also pass the object within the url like we do while edit request
<%= link_to "Edit", edit_post_path(#edit)%>
which will give the :id of the particular post.
Hope this may help you.

Checkbox symbols wrong number of arguments

I am having a problem with the tutorial of codelearn
See here
I have a form
<%= form_for :complete, :url => "/todos/complete", :method => :post do |f| %>
<% #todo_items.each do |t| %>
<%= f.check_box :todo_ids[], t.id %>
<%= t.todo_item %>
<% end %>
<%= f.submit "Complete todos", :class => "btn btn-success" %>
<% end %>`
I've got a problem with the symbol todo_ids[]. I get the error "wrong number of arguments (0 for 1..2)" at the line where it is written.
I tried another way with form_tag but that does not change a thing, I still get the error.
What I don't understand is that they don't have this problem in the tutorial.
Please do you have any idea ?
Many thanks

Interaction between controllers

On my web app I have a login page which is in the Pages Controller called welcome.html.erb
Inside it has a simple form for login and a simple for for sign up, both on the same page.
If the user signs up with wrong credentials (like password confirmation wrong, or length of password and etc) the controller that handles this is the new method in the Users Controller.
Inside the new method it checks if a user is created, and if not I'd like it to return to the welcome method in Pages Controller, passing to it the errors that were created during the user's creation.
It seems that if I do a
redirect_to root_path
The error count of the signup is reset when returning to the root page. If instead I call some other action of the users controller I do see the errors.
But since all of the html code is in the welcome view I don't want to replicate the code in the users controller views as well..
Is there a way to pass that errors data to the pages controller?
The partial for the error is
<% if object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(object.errors.count, "error") %> prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved:</h2>
<p>There were problems with the following fields:</p>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Which I call using
<%= render 'shared/error_messages', :object => f.object %>
redirect_to welcome_path
(Or whatever the actual path is.)
From the Users controller I called
flash[:errors]=#user.errors
and then I changed the partial to be
<% if object.any? %>
<div id="error_explanation">
<h2><%= pluralize(object.count, "error") %> prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved:</h2>
<p>There were problems with the following fields:</p>
<ul>
<% object.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Which I called using
<%= render 'shared/error_messages', :object =>#user_errors %>
That solved the issue for now..

RAILS3: link_to ( ...method: post...) and No route matches "/dropup"

I'm migrating from rails2 to rails3 and I came across the following error:
Unknown action
The action 'show' could not be found for SessionLocalesController
that's because I have a link_to with a method: post inside.
My previous research indicates that I should use button instead or add <%= javascript_include_tag :defaults %> line to my code, because rails3 is unobtrusive javascrtipt:
1.Button is not a solution because the same problem is happening elsewhere so making this work as it is will solve it as well.
2.The <%= javascript_include_tag :defaults %> seems to work, but it give this error instead:
Routing Error
No route matches "/dropup"
I'm stuck now. Thanks in advance for your time.
As asked, here's the code:
<% for locale in (I18n.available_locales) -%>
<%= link_to image_tag("flags/" + locale.to_s + ".png", :title=>locale.to_s) + " " + locale.to_s, session_locale_path(:new_locale => locale.to_s), :method => :post %> <%=link_to t('language'), 'dropup', {:id=>"language_select", :class=>"language_select",:title => t('translate.change')} %>
<ul id="language_menu" style="display:none">
<% for locale in (I18n.available_locales) -%>
<li><%= link_to image_tag("flags/" + locale.to_s + ".png", :title=>locale.to_s) + " " + locale.to_s, session_locale_path(:new_locale => locale.to_s), :method => :post %> </li>
I've solved this. That same problem has been solved before here in stackoverflow many times, but only after finding the solution I noticed it. Well, here it is>
http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/
Get jQuery and add the following to your layouts:
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
More info on the link I supplied.

Rails 3 form only returning last element

I'm trying to build a form that will list all users and allow you to check the ones that you want to add to a team. Here's my first cut at the form:
<div id="add_team_mates">
<%= form_tag do %>
<%= will_paginate #users %>
<ul class="users">
<% #users.each do |user| %>
<li>
<%= gravatar_for user, :size => 30 %>
<%= link_to user.name, user %>
<%= check_box_tag("add", user.id) %>
</li>
<% end %>
</ul>
<%= submit_tag "Add Team Mates", :action => "add_team_mates" %>
<% end %>
</div>
And, right now this is all that I have in the controller:
def add_team_mates
end
The problem is that if I check multiple users, I only get the last user.id rather than multiple is as I'd expect. Here's some example from the log:
Started POST "/teams/5" for 127.0.0.1 at 2011-04-14 15:28:13 -0700
Processing by TeamsController#add_team_mates as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"IHFDevfKES8NibbCMlRa1t9qHn4/ZMKalK1Kjczh2gM=", "add"=>"3", "commit"=>"Add Team Mates", "id"=>"5"}
Completed in 12ms
Any help on this would be greatly appreciated. Thanks!
All your checkboxes have the same name, change the line to
check_box_tag("add[]",user.id)
In the controller your parameters will be like so:
params[:add] = ['foo','bar','baz']