RAILS3: link_to ( ...method: post...) and No route matches "/dropup" - ruby-on-rails-3

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.

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

Route not working in RoR

ok I am trying to invoke a DELETE HTTP command using embedded ruby.
SO my code is:
<li><%= link_to "Sign out", signout_path, method: "delete" %></li>
in my routes I got
#Note the use of via: :delete for the signout route, which indicated that it should be invoked using an HTTP DELETE request
match '/signout', to: 'sessions#destroy', via: :delete
but I get this error!
No route matches [GET] "/signout"
I wrote "method: "delete" !! so why does it give me a GET error? ?
guys applying what you told me, including application.js breaks my js code!!
Here is my head code:
</head>
<!-- Ruby Code -->
<%= stylesheet_link_tag "scaffold" %>
<%= stylesheet_link_tag "myCSS/home.css", :media => "all" %>
<%= stylesheet_link_tag "myCSS/JS_dropdown_menu.css", :media => "all" %>
<%= javascript_include_tag "myJS/jquery-1.7.js" %>
<%= javascript_include_tag "myJS/hoverIntent.js" %>
<%= javascript_include_tag "myJS/jquery.dropdown.js" %>
<%= javascript_include_tag "application.js" %>
<%= csrf_meta_tags %>
</head>
You're attempting to make a GET request to a route that would only respond to DELETE.
Why isn't this working? You probably haven't included application.js using this:
<%= javascript_include_tag :application %>
This would include the jquery.js and jquery_ujs.js files that would provide the method: "delete" functionality for your link.
The functionality of passing method: 'delete' relies on having a functioning UJS library. The first thing I would do is make sure that jquery and jquery_ujs JavaScript files are being included correctly.

Devise delete link for an object causes user to sign out

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 %>

Ruby on Rails simple_form_for all.each do

I am using Rails 3.0, Ruby 1.9.2 and the Plataformatec simple_form gem. This code works with a form_for but not simple_form_for:
<%= simple_form_for(#provider) do |f| %>
<% Car.all.each do |c| %>
<div>
<%= check_box_tag :car_ids, c.id, #store.cars.include?(c), :name => 'store[car_ids][]' %>
$<%= c.cost %> | <%= c.description %>
</div>
<% end %>
<div class="actions">
<%= f.submit "New" %>
</div>
<% end %>
How do I get it to work with simple_form_for?
Thanks in advance!
You can't use simple_form right the same way as form_for.
For example ther is no any check_box_tag method in simple_form gem. There is ONLY inuput fields that you can specify with :as option. So your check_box_tag will be converted to
f.input car_ids, ..., :as => :check_box
Checkout Usage, Rdoc and other useful stuff https://github.com/plataformatec/simple_form
The problem was in the controller code.
In the "new" controller action I can't simply perform:
#provider = Provider.new(params[:provider])
as one would normally.
Instead I have to process each parameter separately:
#provider.location = params[:provider][:location]
etc...
For the Car check boxes, I add each car_id from the car_ids parameter to the "has_many" cars model association one at a time:
car_ids = params[:provider][:car_ids]
car_ids.each do |cid|
#provider.cars << Car.find(cid)
end
Then I can call:
#provider.save!
And it saves correctly (my initial problem was that it wasn't saving the selected Cars).
For some reason, I was able to figure this out only after posting the question here. Funny how that works.
Thanks all for your replies!