link_to method post is not working Rails 2.3.9 - ruby

I'm trying to implement some link_to's using post method.
However, it always puts all the information as a query string in the browser's bar (it behaves as a GET).
Why?
Here is my code:
<%= link_to hotel[:name], {:controller => "gds_hotels", :action => "hotel_details",
:dest => #destination,
:ci => #check_in,
:co => #check_out,
:hotel => hotel,
:rooms => hotel[:rooms]}, :method => "POST" %>
Thank you! =)

You should use button_to
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

Related

link_to_remote equivalent in rails 4

I am upgrading from rails2.3.5 to rails4 and i am facing difficulty to convert link_to_remote into rails 4
link_to_remote("Retire", :url => { :action => :delete, :id => user.id },
:condition => "confirm_retire(#{user.id})", :html => {:style => 'color: #CC0000;float:right;margin-right:8px;'})
I know that we need for this :remote => true with link_to like
link_to('Retire', {:action => :delete, :id => user.id},
:condition => "confirm_retire(#{user.id})", :remote => true,
:class => 'retire')
But i am facing problem with :condition => "confirm_retire(#{user.id})",
how to handle this ?, i did lot of research but not found any solution
The condition option isn't supported by link_to. You could perform that check in a js file using a data attribute on the link element to provide a value for the condition.
link_to('Retire', {:action => :delete, :id => user.id},:remote => true, :class => 'retire', :data-user-retire => "your_condition")
$('.retire').on('click', function(){
if ($(this).attr('data-user-retire') == "your condition") {
return true;
} else {
$(this).append("some message")
return false;
}
});
This is just an example, you'll need to amend it for your requirements.

Better way of Getting Back to Application Controller under Namespaced Controllers under the "link_to" Tag

I am trying to call an action from a controller 2 steps up the folder structure namespace
<%= link_to( "Reset Search" ,
{:controller => "../../application",
:action => 'reset_search',
:reset_search_redirect => reset_search_redirect},
class: 'tiny button') %>
The "../../application"works! however; i dont think it's rails-y. Is there a better way?
Here is the href link when I just put :controller => "application"
/human_resources/settings/application/reset_search?reset_search_redirect=%2Fhuman_resources%2Fsettings
Which does not work.
Here are my routes:
get 'human_resources/' => 'human_resources#index'
namespace :human_resources do
get 'settings/' => 'settings#index'
namespace :settings do
get 'constants/' => 'constants#index'
resources :constants
end
end
resources :test, only: :index
root to: 'home#index'
match ':controller(/:action(/:id))', :via => [:get, :post]

Rails, route mapping not matching the correct action

I'm trying to route the following URL:
/shop/{category_id}/{product_id}/
to a controller action called 'product'
I've got this in my routes:
match "/shop/:category/:id" => "shop#product"
and i've got link_to's as follows:
link_to_unless_current "#{t('murals')}", url_for(:controller => 'shop', :category => 'walls', :id => 'murals')
When the urls are output, they are:
http://0.0.0.0:3000/shop?category=walls&id=murals
instead of the desired:
http://0.0.0.0:3000/shop/walls/murals
if i switch the route, so it goes to:
match "/shop/:category/:id" => "shop#index"
this works, but it's not the right action.
Any help would be appreciated.
try this
<%= link_to_unless_current "#{t('murals')}", custom_path(:category => 'walls', :id => 'murals') %>
match "/shop/:category/:id" => "shop#product", :as => :custom

Devise route not respected on Devise view pages

I have implemented devise and set up the routes to prettyify the urls as so (in the routes file):
devise_scope :user do
get "/login" => "devise/sessions#new"
get "/logout" => "devise/sessions#destroy"
get "/register" => "devise/registrations#new"
end
I have global menu in my application.html.erb file, however, which now inserts devise/controller/action on every link that is on the login or register pages, such as
<%= link_to "Upload Video", {:controller => "videos", :action => "new"} %>
becomes devise/videos/new
Any ideas on how to fix this? I can hack around it but I'm pretty sure it is a simple fix.
Cheers,
s
Take out the devise_scope and try something like this:
devise_for :users,
:controllers => {:registrations => 'devise/registrations', :sessions => 'devise/sessions'},
:path => '/',
:path_names => {:sign_in => 'login', :sign_out => 'logout'}

rails 3 destroy action problem

i am trying to delete some rows on my database but when i call destroy action, update action works.
this is delete.html.haml
=form_for #post, :url => {:action => 'destroy', :id => #post.id} do |f|
this is route
resources :post
root :to => "post#index"
match '/delete/:id/', :to => "post#delete"
What's the problem? Anybody does understand?
The :action should be delete, to match the name of the action in your controller:
= form_for #post, :url => {:action => 'delete', :id => #post.id} do |f|
If you are using rails 3 then you need to make sure you have <%= csrf_meta_tag %> in your header. I put mine after my <%= javascript_include_tag... %> tags. Also make sure that you include the rails.js. (This will be on your layout files).
More details here: http://railsforum.com/viewtopic.php?id=38460 Look toward the bottom.
The root entry in your routes should be placed after the matchblock. Plus: seems like you mixed up the controller action and the http verb: use delete in the form action and destroy in the controller action call:
=form_for #post, :url => {:action => 'delete', :id => #post.id} do |f|
resources :post
match '/delete/:id/', :to => "post#destroy"
root :to => "post#index"

Resources