Rails how to acess an param given in the url - ruby-on-rails

I created an link to an site with an extraparam "icd1":
<%= link_to "#{s.von} - #{s.bis} #{s.bezeichnung}", icd_show1_path(s, :icd1 => #icd1 ) %>
From this site i want to redirect, with the id given in #icd1:
<%= link_to "#{#icd2.von} #{#icd2.bis} #{#icd2.bezeichnung}", icd_show_path(#icd1) %>
But i get the error:
Couldn't find Icd1 with id=icd1
{"id"=>"icd1"}
How can i access my param?

You use the params object.
So something like this:
#icd1 = params[:icd1]
That should let you access it in your view.

Related

Ruby on Rails get paramenter from URL on View

I'd like to get the paramenter from the URL on my view/html.
For example, I'm showing an specific item from my data base, and the URL is like this: "http://localhost:3000/menus/index.%23%3CMenu::ActiveRecord_Relation:0x007f50ed153250%3E?id=6"
What I want is: when I click on the New Button of that specific item, the form opens with the URL "http://localhost:3000/menus/new", but I want somehow pass that id=6 to this NEW view.
I am using this code on the html to open the form: <%= link_to 'Novo', new_menu_path %>
I also tried: <%= link_to 'Novo', new_menu_path(#menus, :id) %>, but the id param is always null/empty.
How do I do that?
Thank you
To pass an extra param to an url when you define a link_to
<%= link_to 'Novo', new_menu_path(id: #menu.id, other_param: "hello") %>
will generate http://localhost:3000/menus/new?id=6&other_param=hello
this will add the following to the params hash
Parameters: {"id"=>"6", "other_param"=>"hello"}
well :id is just a symbol, you need to tell the route helper what to bind to it. For example
<%= link_to 'Novo', new_menu_path(id: params[:id]) %>
which should give you something like /menus/new?id=6

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.

Ruby on Rails get URL and use it within an if statement

I have a ruby on rails application, and want to display an image on the index page depending on what the URL is. If the url is: localhost:3000/products/multi_find I do not want to show the image, if it is anything else I do want it to be shown.
Is their a way I can retrieve the url and store it in a variable to run an <& if statement like: if url != localhost:3000/products/multi_find &><%= image tag "test.png", :size => "15x18" %> <% end %>
Thank you in advance.
None of those solutions worked, this did:
<% if request.original_url == 'http://localhost:3000/products/multi_find' %>
<%= image tag "test.png", :size => "15x18" %>
<% end %>
In Rails you must think in terms of routing rules, not URLs. An URL is nothing else than the result of a routing directive.
Therefore, /products/multi_find can be expressed in terms of action, controller and parameters.
With that in mind, you can use the current_page? helper to check if the current URL matches the route you expect, if it does then ignore the image.
You can even use rails "action_name" and "controller_name" helper methods to get the job done.
Try this:
<% unless current_page?('/products/multi_find') %>
<%= image tag "test.png", :size => "15x18" %>
<% end %>

Rails, link_to helper with an URL as a param

I want to generate the next html link:
http://url.com
To reproduce it using the link_to helper I have to write:
<%= link_to "http://url.com", "http://url.com" %>
What doesn't look DRY at all, I was expecting this to work:
<%= link_to "http://url.com" %>
But the above code generate a link targeting the actual request.url, not the one I'm sending in the param.
Am I missing something?
You're not missing anything --- the normal case is for the URL and the text that shows to the user to be different.
If you'd like, you could create a helper like
def link_to_href(link, args={})
link_to link, link, args
end
then, when you use it,
<%= link_to_href "http://url.com" %>
Will output
http://url.com
If you take a look at the source code of link_to you will see that at line 248 the a tag label is build with name || url.
That's why you have this behaviour and there is noway to do it like you're expecting.

Ruby on Rails: How to create a "recent_search_path" helper method?

I have implemented a search feature of my application. After submitting a search term, you are redirected to a url that looks something like this:
http://localhost:3000/search?utf8=✓&search=term
I want to be able to store this in the session and link to it later:
<%= link_to 'Back', recent_search_path %>
How might I achieve this?
Thanks to rc for referring me to a very similar question. This could be achieved by storing the current path in the session:
session[:recent_search_path] = request.url
...and retrieving it again in the view:
<%= link_to "Back to search results", session[:recent_search_path] %>

Resources