Routing Error, receive [Get] instead of [DELETE] - ruby-on-rails-4

I'm trying to make a link to destroy an row in a table but the problem is that I receive [GET] instead of [DELETE].
Here the error:
No route matches [GET] "/clubs/1/club_accounting/2"
My route is :
club_club_accounting_delete_path
DELETE
/clubs/:club_id/club_accounting/:id(.:format)
club_accounting#delete
My link :
<%= link_to 'Supprimer', club_club_accounting_delete_path(:id => activity.id), method: :delete %>
Any idea ?

The method: :delete option in the link_to helper adds the attribute data-method="delete" to the resultant <a> tag. This is then handled by the jquery_ujs javascript file. Make sure it's included correctly and that it shows up in your page source. If it's there, open the javascript console in your browser's developer tools to see if there's any javascript errors when you click on the link.
Other than that, your posted code looks in order.
Minor style suggestion though: you don't need to pass :id => activity.id to the path helper, just use: club_club_accounting_delete_path(activity).
If you encounter any error messages, post them back in your answer.

Related

Getting same style in another page when click to login

I am having a login page for user generated by devise gem.where I removed the registerable from model for only login not for signup.I have also generated an admin which gives default login credentials for user in seed.rb file.I have done some css work in login page. next I have generated an employee page for further process for users.
Here my doubt is the styling part what I have done in login page is also coming in employee page. I dont want that so I wrote some condition in application.html.erb
<% unless user_signed_in? %>
<%= render 'layouts/heading' %>
<% end %>
The method is defined in application.controller.rb
def user_signed_in? %>
render :layouts => 'application'
end
I tried a lot by changing conditions but it is still displaying or showing errors.
You said that you're using "device gem", but since you're talking about authentication, I assume you mean "devise"?
The user_signed_in? method is a helper which should be provided automatically by devise, and you should be able to use it in your views. You shouldn't need to define it in the application controller.
You probably want to make sure you do "before_filter :authenticate_user!" in the relevant controller (you can use the ":only" option if you only want this to apply for certain methods).
It's difficult to give any more information because you haven't said what errors you are getting. I assume you're getting some kind of syntax error on the definition of the "user_signed_in?" method, since you have an invalid "%>"on the end of the line, which I guess you copied-and-pasted from an ERB file.

rails 4 route matching order

# Individual Page
get '/manage/:type', to: 'manage#profile', as: :profile
# Detail Page
get '/manage/:id', to: 'manage#detail', as: :detail
In the code above, even if I create a url such as
<%= link_to "Detail Page", detail_url(:id => 2) %>
it still goes to profile action of the manage controller (even though it asks for type and not id).
Is this expected behavior in rails 4.1, or am I doing something wrong?
I thought that, if I have a named route, I can pass the appropriate parameters (:id => 2), and have it skip past the first route, simply because it does not match the route name.
Of course, things work as intended, if I reverse the order, and put "Detail" route before the profile route.
It's all about the pattern matching.
Add some constant text to disambiguate them -- e.g.,
get 'manage/profile/:type', to: 'manage#profile', as :profile
get 'manage/detail/:id', to: 'manage#detail', as :detail

How to auto set a form value on the next page using link_to? (Rails 4)

I am new to rails and am looking to do something very simple but don't know the correct syntax. I have 2 buttons one for creating a new page and one for creating a new post. Both a page and a post save in the same database table and are controlled through a boolean field called 'static'. A page therefore has a static value of 1 and a post 0. What I want to do is simply auto set this value in a form (and hide it) when I click new page or post. I imagined the link to create a new page would work something like this:
<%= link_to 'New Page', new_page_path(:static => "1") %>
This doesn't work so I tried to create a new_static page action and a new_post page action with correcting routing (for displaying only pages I created a show_static action used the following link_to and it works fine):
<%= link_to "Pages", show_static_pages_path(#pages), :method => :get %>
The problem is when I created the new_static page action it expects an id for some reason.
new_static_page GET /pages/:id/new_static(.:format) pages#new_static
I would prefer to not mess around with new actions and routing and simply set the value with link_to. Any tips would be greatly appreciated.

dots in rails URL

I'm trying to add a show page to my devise users but can't seem to get the id value passed properly, how do I create a link to the show page? For some reason rails is doing this to URL
/show.1
users controller:
def show
#user = User.find(params[:id])
end
user/show.html.erb
<h1><%= #user.username %></h1>
link to user profile also gives me issues id no method
<%= link_to #post.email, users_show_path(#user.id) %>
routes.rb
get "users/show"
get "pages/faq"
get "pages/about"
devise_for :users
resources :posts
You have defined the show route as:
get "users/show"
Notice that there is no dynamic segment in this route, like :id. That means your route is not expecting any value to be passed. BUT while linking to this route you passed #user.id
<%= link_to #post.email, users_show_path(#user.id) %>
which the route was not expecting. So, Rails assumed that you are passing the format (like .html, .json, etc.) which is why you see the url formed as users/show.1.
To fix this, I would suggest you to add a dynamic segment to your route to capture the id of a user correctly.
Change
get "users/show"
With
get "users/show/:id" => "users#show", as: :users_show
For a user with id = 1, when you click on the user profile link the url generated would be http://yourdomain/users/show/1

Rails 4 not using form method PATCH, defaulting to POST

I have the following line which generates a form tag;
<%= form_for :stream, url: stream_path(#stream), method: :patch do |f| %>
It generates the following;
<form method="post" action="/streams/52b02d267e3be39d3da5aa609f1049d7" accept-charset="UTF-8">
If I change it to :put it still has it has post but if I write method: :get it will change it to get
Does anyone have any idea why it would be doing this and what I can do to prevent it?
Here is the output from rake routes;
Prefix Verb URI Pattern Controller#Action
streams GET /streams(.:format) streams#index
POST /streams(.:format) streams#create
new_stream GET /streams/new(.:format) streams#new
edit_stream GET /streams/:id/edit(.:format) streams#edit
stream GET /streams/:id(.:format) streams#show
PATCH /streams/:id(.:format) streams#update
PUT /streams/:id(.:format) streams#update
DELETE /streams/:id(.:format) streams#destroy
The background is this is a simple edit form, all I want it to do is his the update method of the controller.
Follow up
In my layout file I am bringing in csrf_meta_tags and my javascript_include_tag links to a file called "stream" which has the following
//= require jquery
//= require jquery_ujs
method: (:get|:post|:patch|:put|:delete)
from the documentation:
"in the options hash. If the verb is not GET or POST, which are natively supported by HTML forms, the form will be set to POST and a hidden input called _method will carry the intended verb for the server to interpret."
source: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
is //= require jquery_ujs in your application.js?
also make sure
<%= javascript_include_tag "application"%>
<%= csrf_meta_tag %>
are present in your layout file.