Remove a parameter from URL - ruby-on-rails

I have a link_to method in my Rails app:
link_to t('edit'), edit_building_path(#building, :hidden_action => params[:action])
How do I remove hidden_action from url?
Currently url looks like:
http://localhost:3000/buildings/2/edit?hidden_action=new
My issue is that I need to know from which page user is accessing this link_to.

In rails helper, it depends on you
def edit_the_building_url(building, you_want_the_params_or_not)
if you_want_the_params_or_not
edit_building_path(building, :hidden_action => params[:action])
else
edit_building_path(building)
end
end
then in your controller
link_to t('edit'), edit_the_building_url(#building, true)
or
link_to t('edit'), edit_the_building_url(#building, false)
if above doesn't profit you, just
url = url.chomp("?hidden_action=#{params[:action]}")

Related

How do i add an active class to a dynamic link?

I have a sidebar that holds a list of posts. I need the corresponding post on my sidebar to have an active class. What i have currently does not work so what is the best way to do this?
def is_active?(path)
current_page?(path) ? "active" : ""
end
<% #posts.each do |post| %>
<%= link_to post.title, post, class: is_active?(posts_path) %>
<% end %>
As I said in my comment, methods ending by ? should return a boolean value. If you decide to go against the convention this will make things harder for us.
I suggest you actually use an active_link_to like it is explained in that question.
However the main problem was that you didn't generate the URL properly for each post :
is_active?(posts_path)
The posts_path is the path to the index and not the individual post resource. You should use something like post_path(post)
You want to do something like this :
First your is_active? method, because it has a ? should return a boolean
def is_active?(path)
current_page?(path)
end
Then you can use it this way (you need to get the URL of the post using the post_path(post) helper)
<% #posts.each do |post| %>
<%= link_to post.title, post, class: ('active' if is_active?(post_path(post))) %>
<% end %>
EDIT : because is_active? does the same thing as current_page? you should simply replace the is_active? code by an alias declaration
alias :is_active? current_page?
I had to develop such a solution some years before. I implemented the following as helper methods to detect an active link. Be aware that this is not the best solution I think. I had to provide a full url. Fell free to edit the code to use a path or a parameter hash instead.
# Returns true or false if the page is displayed that belongs to the given url.
def link_selected?(url)
request_method = request.method.to_sym
begin
url_params = Revolution::Application.routes.recognize_path(
url, {:method=>request_method}
)
rescue Exception => e
{}
end
begin
request_params = Revolution::Application.routes.recognize_path(
request.url, {:method=>request_method}
)
rescue
{}
end
return true if url_params == request_params
return false
end
def is_active?(url)
return link_selected?(url) ? 'active' : nil
end

Issue with passing params in link_to rails

I'm having trouble passing a param through a link_to in Rails using the below code:
<%= link_to new_registration_path, {:workshop => #workshop.id } do %>
When I pry into the controller, the :workshop is not being included in the params (only controller and action).
Is this a strong params issue?
The workshop param has to be passed to the new_registration_path helper instead of passing it to link_to, like this:
<%= link_to new_registration_path(workshop: #workshop.id) do %>
If you want the URL to be like /something/123 instead of /something?workshop=123, you can change how your route is defined on routes.rb:
get something/:workshop
and then you can pass workshop: 123 to the URL helper.

Rails 4 - current_page? with query parameter

I've method in my application.rb
def current_page(path)
"active" if current_page?(path)
end
then in my view, lets say the request_uri is http://www.example.com/stories, i just can call
<%= link_to "All Stories", stories_path, :class => "#{current_page(stories_path)}" %>
BUT how about if the request_uri has something like http://www.example.com/stories?featured=true ? How do I call it in view?
Because current_page? method will compare passed-in path which doesn't have ? just with your current url deleting query params.
For example, if you're at http://www.example.com/stories?featured=true, and you have current_page(stories_path), you'll still compare http://www.example.com/stories(stories_path) with http://www.example.com/stories(your current url without featured=true).
I have a idea, but you'll have ? in the end of your normal url.You can change the code to <%= link_to "All Stories", stories_path + '?', :class => "#{current_page(stories_path + "?")}" %>
I'll update this if I come up with better methods.

how to use click event in rails 4

My code:
<%#categories.each_with_index do |d,index| %>
<%= link_to d.name ,{:action=>'index',:id=>d.id,:hotel_id=>d.hotel_id},class: "btn-orange" %>
<%end%>
In my Controller:
def index
#category = Category.where(:hotel_id => params[:hotel_id]).first
#menus2=Menu.where(:category_id=> #category.id).sorted
#categories=Category.where(:hotel_id=>params[:hotel_id])
#cart = current_cart
if params[:name]==params[:name]
#menus=Menu.where(:category_id=> params[:id]).sorted
else
end
end
I am new in rails.I want to use #menus2=Menu.where(:category_id=> 0).sorted this code when link_to is clicked.How can i do in my controller, is there any click event in ROR.
You can add a remote attribute to your link_to method:
= link_to d.name, controller_index_path, class: 'btn etc', remote: true
This will allow Rails to make an 'Ajax' call to your desired controller.
Note: when making a remote call such as this, Rails will receive/respond via .js. This means that you need to have an index.js.erb file that does what you need and reports back to the 'Ajax' caller whatever data is necessary.
This page in the Rails guide may help with additional details, etc.: Working with JavaScript in Rails

Rails 3: how to code a route path

I'm creating a rails app and I can't figure out or find answer for this problem.
What is the route for:
#example = Example.find_by_notId(params[:notId])
I want my route to look better then /example/1 and would rather have it read /notId (where notId would be title or some other non-ID int). Normally I would use show_path but in my html <%= link_to image_tag(pic), show_path(example) %> doesn't work. Here is my code and it works when I hard code it into the URL (localhost:3000/notId) but I need a way to route it through a link. Any ideas?
Show
def show
#example = Example.find_by_title(params[:title])
end
Routes
match '/:notId', to: 'example#show', via: 'get'
_example.html.erb
<div class="example">
<% Movie.all.each do |example| %>
<%pic = example.title + ".jpg"%>
<%= link_to image_tag(pic), show_path(example) %>
<% end %>
e</div>
You probably need to override the :id param in your routes:
resources :examples, param: :title
This will generate routes like:
/examples/:title
/examples/:title/edit
Then in your views:
example_path(example.title)
Alternatively, you can change the method to_param(used for building urls) in your model:
class Example < ActiveRecord::Base
def to_param
title
end
end
This allows you to keep using example_path(example) (without .title)

Resources