# 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
Related
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.
I am using a cached_counter to keep track of all comments for a user.
My model relationship look like this:
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
comment belongs to user, counter_cache: true
end
and the user table holds the counting variable.
In Activeadmin i have a column :comments_count which shows the amount of comments for each user.
So far so good.
Now i would like to modify it a bit. I would like to add a link which directs to a page where all comments are listed. How can do this?
I am checking the AA [live demo][1] since they do something similar there.
My idea was to create a partial view and link_to it. But i m struggling implementing it and i dont know if this is best practise anyways.
I have this query
Comments.where(:User_id => :id)
but how to i embed it into a column?
Thank for any advice .
You can handle this situation by linking to the admin index page for Comment with a filter pre-set for the given user_id. This requires drastically less code then a custom page, and gives you direct access to scopes, filters, etc.
Here's how:
index do
# make sure you set sortable, so you can click to sort!
column :comments_count, sortable: 'users.comments_count' do |user|
link_to user.comments_count,
admin_comments_path(q: { user_id_eq: user.id })
end
end
Edit:
The ActiveAdmin index controller uses Ransack to handle searching and filtering. Ransack accepts query options in the form of a hash that obeys a sort-of DSL (the user_id_eq bit above is an example). Now if you open any ActiveAdmin index route and start playing around with filters, you'll see those parameters tacked on to the end of the url using the same convention. The ?q=... part gets passed directly to Ransack in the index controller, and that's how your models get filtered. Our code above simply links to the index page with the id filter pre-set. You can add other filters, sort orders, or even scopes as well.
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.
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
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.