Issue with passing params in link_to rails - ruby-on-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.

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

No route matches [POST] "/" when posting a form

I'm trying to set up a form on my index page that will pass a param myform, to the same index page via a GET request. I thought this would be relatively simple. but I'm getting an error.
I generated a controller, RecipesController, with a method called index:
class RecipesController < ApplicationController
def index
#search = params[:myform]
return #search
end
end
In this method I'm trying to get back what the user types into a textbox when a button is pressed and the GET request is fired.
Here is my view:
<h1>Recipe Finder</h1>
<%= form_tag(controller:"recipes",method:"get") do %>
<%= label_tag(:myform, "Search") %>
<%= text_field_tag(:myform) %>
<%= submit_tag("search") %>
<% end %>
Here are my routes:
Rails.application.routes.draw do
root 'recipes#index'
This shows up fine when I visit localhost:3000, but when I press the button I'm expecting the controller index method to just return whatever text i typed into the textbox. Unfortunately, I only get:
No route matches [POST] "/"
I know setting the root to recipes#index is causing the failure as my #search variable is not set when the page is opened initially.
I'm wondering if I should have a separate route and method for the GET request and should I just open the main page with the call to localhost:3000 without running any code in the controller? Is this possible?
The problem is that form_tag accepts a Hash as both its first and second argument. If you don't use brackets, it's going to interpret all of it as part of the first argument, and your method: "get" is not applied properly.
Because of this, it defaults to doing a POST request, for which there is no route.
Try this. Because the first argument is no longer a Hash, it should work properly:
<%= form_tag("/", method: "get") do %>
Alternatively, using your code, you can try this:
<%= form_tag({ controller: "recipes" }, { method: "get" }) do %>

Non resourceful routing and dynamic segmentation

I have this in my route file
get '/registrations/:student_id/:subject_id' => "registrations#show", :as => 'custom'
Now I want to use this in link_to helper so that I can send student_id and subject_id to show action of the controller
<%= link_to "Custom" .... %>
Assuming you have instance variables #student_id and #subject_id availeble to your view, then this should work:
<%= link_to "Custom", custom_path(#student_id, #subject_id) %>
looks like similar to this question, the same solution can be applied. Custom dynamic routing and get ids from url

Rails how to acess an param given in the url

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.

Ruby on Rails: How do I pass variables with observer_form?

I have
<%= observe_field :tb_search,
:frequency=>0.5, :update=>'reports',
:url=>{
:action=>'filter_reports',
:only_path=>false,
:user=>#user.id},
:with=>"'search='+encodeURIComponent(value)" %>
but on the controller side, params[:user] is nil.
I did a
puts "NTHDEONUTHDOEDEUU#{params[:user].nil?}||"
in the responding method in controller to prove to myself that it was nil.
any ideas?
With this helper I believe you need to pass all params through the :with option. Something like:
<%= observe_field :tb_search,
:frequency=>0.5, :update=>'reports',
:url=>{
:action=>'filter_reports',
:only_path=>false},
:with=>"'search='+encodeURIComponent(value)+'&user='#{#user.id}"

Resources