I have a User model wich has a controller index and an '.all' scope
ex: User.all
Is there a decent way to switch to a different scope when you clic on a view link?
I want to display all user when a user clic an 'all' link or a scoped version when he clic on an other link.
Do i have to use a gem like has_scope?
Thanks
You can use scoped method, for example:
#users = User.scoped
#users = #users.your_scope if params[:your_scope_param]
all you have to do is to bind appropriate param to your "scoping" link, it should be like:
<%= link_to 'Scoped users', users_path(your_scope_param: true) %>
Related
In my Rails app I have a users_controller with an action that displays a users profile, but I would like it to only display a users profile if the user has a specific value set in the database called is_rec set to true.
The below is my attempt but it doesn't work. I used a scope for the index and that worked well but I wasn't able to get a scope to work when I was working with the show action
def rec_profile
#user = User.find(params[:id])
#user if #user.is_rec == 'true'
end
I could do something like this in the view but I wanted to see if there was a way to do it in the controller instead?
<% if #user.rec == 'true' %>
<% show page here %>
<% end %>
You can use find_by and pass 1 or more attributes to look for in your database. So you could pass the id and is_rec attributes and get your user.
User.find_by(id: params[:id], is_rec: true)
Or you can use where to get all users where is_rec is true and then filter for the user whose id matches with params[:id]:
User.where(is_rec: true).find(params[:id])
Which eventually can be converted to a scope for easy access:
scope :with_rec, -> { where(is_rec: true) }
For being used then as:
User.with_rec.find(params[:id])
with_rec is just an example name as scope, you could use whatever you want.
I have a /articles page that I can access via the articles_path route helper.
Let's say I have 2 tabs on that page (e.g. something like this) that the user can click back and forth on, but it doesn't leave the page.
I have logic that allows the user to deep link to a specific tab, so either of the following url's are valid and will open the page on the specified tab directly.
/articles?tab=foo
/articles?tab=bar
Is it possible to define two new custom routes with the above urls that include the query parameter? I'd love to have a helper like articles_foo_tab_path and articles_bar_tab_path that incorporate those query parameters directly.
Thanks!
Create those helper methods:
module ArticlesHelper
def articles_foo_tab_path(article)
article_path(article, tab: 'foo')
end
def articles_foo_bar_path(article)
article_path(article, tab: 'bar')
end
end
And use them in your views:
<%= link_to #article.title, articles_foo_bar_path(#article) %>
The helper method is one solution. Alternatively you can add a route which maps the tab param to the url e.g. articles/foo or articles/bar
get "articles(filter/:filter)", to: "articles#index", filter: /.*/
I'm trying to create a simple Rails app that will retrieve API data. My original intention were not to save the search to the database, which is why I used form_tag. My search works when I run in the console, but when I call #results or #first_match, it give me nil. Many thanks in advance.
Actor Controller Method
def index
#results = API::Topic.search([:actor])
#first_match = #results.values.first
end
Actor Form
= form_tag 'actors/show', method: :get do
= text_field_tag "Actor"
= submit_tag "Show me"
Routes
RailsApp::Application.routes.draw do
resources :actors
end
Update
Since I am routing in the show method in the form. I was able to retrieve the variables in my show method in the controller. I don't know if this is the best way though.
def show
#results = API::Topic.search([:actor])
#first_match = #results.values.first
end
I think, you need to use API::Topic.search(params[:actor])
I have a listing page, then a form, then a thank you page. I need to put a link on the thank you page that takes the user back to the page they were on before the form which always varies. I've tried using this:
= link_to "Back", :back
But this only takes them back to the previous page, so the form.
Try this
<%= link_to 'Back', url_for(:back) %>
# if request.env["HTTP_REFERER"] is set to "http://www.example.com"
# => http://www.example.com
here is more details.
Well, you can set a method in the form page to collect that url. The basic idea is to use a custom session variable to store previous url and keep it to next session.
Suppose your form's action is SomeController#new, then
class SomeController < ApplicationController
after_action "save_my_previous_url", only: [:new]
def save_my_previous_url
# session[:previous_url] is a Rails built-in variable to save last url.
session[:my_previous_url] = URI(request.referer || '').path
end
end
Then in the thank you page, you can get this my_previous_url by
session[:my_previous_url]
This should be able to suit your case, the previous url two pages ago.
Disclaimer: This is not verified. Idea only.
Add
Session belongs to controller. It is not a helper you can use directly in view. You need to define an instance variable in controller and then you can use it in view. Like this
# Controller
#back_url = session[:my_previous_url]
# View
<%= link_to "Back", #back_url %>
You can use the example from Rails API:
<%= link_to "Back", :back %>
Rails API Doc for link_to
Using a :back Symbol instead of an options hash will generate a link to the referrer (a JavaScript back link will be used in place of a referrer if none exists).
Since you saying,it might be different page before form, probably request_url can help you. you can save your request_url in a param and redirect to param_url if there is.
here is a source that you can take for reference.
http://programming-tut.blogspot.com/2010/06/ruby-on-rails-request-url.html
If use in Controller, you can direct use like this:
def some_action
# some code
redirect_to :back
end
This works for me:
In controller from previous view:
cookies[:original_referrer] = request.orignal_url
to set a cookie on the browser with the URL of the originating page
In the controller from the current view:
redirect_to cookies[:original_referrer]
I'm using this code to highlight currently active menu tab with Twitter Bootstrap:
def nav_link_to(link_text, link_path, options = nil)
class_name = current_page?(link_path) ? 'active' : ''
content_tag(:li, :class => class_name) do
link_to link_text, link_path, options
end
end
This of course makes the link active, only if the given link IS the current page.
How can I change this function, such that it would return 'active' for any links below current controller?
In other words, for all actions for Posts controller, the links would be active?
You can use controller_name to get the name of the current controller.
This could be solved with:
Rails.application.routes.recognize_path(link_path)[:controller]
params[:controller] returns controller name with its namespace.
If you want to get controller name only you should use controller.controller_name.
class Admin::Posts; end
# from the view
params[:controller]
=> 'admin/posts'
controller.controller_name
=> 'posts'
Not sure if I read this correctly, but you shouldn't use a get param to see the current controller.
Have you tried using request.path ? In other words, check if any of the links below include 'request.path' in their path...