Rails - Passing Parameters through link_to - ruby-on-rails

I want to create an Add to Watchlist column + link after the description field ,so the selected movie to be stored on another rb. page from the admin section called "Watchlist".
The concept is similar to a Shopping Cart,passing the data from one page to another.
How's that possible in ActiveAdmin?
I would appreciate your hints and advices.

I'm not familiar with ActiveAdmin, but if your question is how to pass parameters through link_to, try this:
link_to 'Somewhere', some_path(param_1: 'foo', param_2: 'bar')
When you click through, the params should be present in the query string and in the params hash that Rails provides. You can pull out the values with:
params[:param_1]
params[:param_2]

Related

Getting url params into controller

On my website I load a _form made by rails scaffold onto my page, which is under my home_controller.
It only has one text input box for name, but the table im inputting this entry into also requires a bigint value, called course_id.
I pass this course_id value into that page by passing it as a url param.
then in my mod_controller i have #mod.course_id = Course.find(params[:course_id]).id
in my def create function.
any suggestions to help this would be appreciated.
Maybe you want to do this in your Model instead of your Controller? (I am not sure I understand your question though)

How to add addition condition to ransack after submiiting the form

I have a book library rails app using ransack. Basically, after searching for book title and category. I want to have additional condition like in Amazon page. So I use link_to with parameter of ransack. But the problem is that the page will render the result will only base on the parameter which I sent in link_to. Is there a way that allow me to insert a condition in current search session of ransack. Thanks in advance.
You'll need to merge the new condition into the existing filter and pass that hash to the path helper.
Something like this:
books_path(q: params[:q].merge(additional_filter_eq: value))

Store hash values in url

Rails 5.0.0.rc1
Ruby 2.2.5 (can update to latest)
I'd like to think this is possible. I'm making a get request that when a field is selected then the user presses the next button, it goes to another page and that page's url looks like:
http://localhost:3000/food/r/new?utf8=%E2%9C%93&id=2&food=Apple&commit=Next
This looks ugly, to me. Could it be more nicer to look like this:
http://localhost:3000/food/r/new/<some_random_short_string/<name-of-page>
I'd imagine the some_random_short_string would be a hash then in the controller would have something like:
hash = params[:some_random_short_string]
hash[:food] #=> "Apple"
etc...
Not sure how to go about this. Any pointers, please?
Rather than deleting my question, I'll answer it as I will come back to it one day.
'get' exposes data in the url so I went for post. No need for a hash just use params to store the values.
Once page is submitted, the url will look like: http://localhost:3000/food/r/new. In the controller, you use your params:
#selected_food = params[:food]
new.html.erb:
<%= #selected_food %> #=> Apple

Rails 4 new form defaults from params

I am using form_for in the _form.html.erb view in order to create my form for both the edit and new actions, as per a standard scaffold.
I have a model Owner which has_many pets.
I would like to put an html link on my views/owners/show.html.erb to create a new pet for said owner. This link will point to the new action of pets_controller.rb which when accessed will render the view in pets/new.html.erb
What I want to happen is for the the owner_id to be passed with the link in the url to the new action of pets_controller.rb and then be used as the default for a collection_select in pets/new.html.erb
So I have a link to create a new pet but because that link was on a specific owner page, I want the form to create a new pet to have that owner already set, so the user does not have to select from the list.
This has to be done without changing the behaviour of the edit action/view in pets.
I know that I can pass GET arguments then access them in the controller via params, then create variables in the action which are passed to the view. I can then manually check for a default and set it in the view. I do not need assistance in coding if this is the only solution.
Is there is a better way to do this? A format with which I can pass the params such that the view will just pick them up? Without manually editing my controllers and views?
While my personal inclination would be to do as you said and pass a parameter in the link helper and then access the params array in the pets view, you may find that this is the perfect opportunity to explore Nested Resources. Essentially, you could declare owners/:owner_id/pets/:pet_id route with:
resources :owners do
resources :pets
end
You could then link to this route, and reference :owner_id without having to append the query string to the URI (making somewhat cleaner for reuse).
This is likely more work for you, but also potentially more extensible (and certainly more inline with the Rails way of doing things).
REVISION
Added the following regarding link helpers to the comments, but wanted to reflect it in the answer as well.
To show a pet should be:
<%= link_to owner_pet_path( owner_variable, pet_variable) %>
To view pets' index index should be:
<%= link_to owner_pet_path( owner_variable ) %>
The answer given to this question is fantastic.
As #ConnorCMcKee suggests it would be wise to consider nesting your routes. However, if you are a beginner as myself I found that it helped my learning to simply nest my second controller into the first (i.e. nest PetsController into OwnersController) as a first step. Then afterwards I would continue with the routes.
The method would be something like:
1./ In owners/index.html.erb:
Links to PetsController index action
The key to make this work is to send the :owner_id in your link parameters. Then that Pets index action will have access to that :owner_id and know which :owner_id called it.
2./ In PetsController you would then be able to find that Owner using that id, like so:
params[:owner_id]
Then your actions can start to take advantage of knowing what Owner called them. Remember though that all your redirects inside your PetsController need to preserve params[:owner_id]. That is because once you are inside that nested structure you have to maintain it and stay inside it and always know which :owner_id you are working with.

custom action for a nested resource without adding to routes

This is probably really simple but I have a nested resource lets say:
map. resources :book, :has_many => :pages
I write an action called "turn" that increases page.count by 1. How do I call this action using a link_to? Thanks a lot in advance.
It's hard to tell where your page.count comes in. In Railish, you would find pages.count (note the 's'). Further, count (and also size) is a read-only attribute on arrays and hashes et.al. provided by ruby that returns the number of elements. You don't set count.
Next, I'm not sure where your turn action is supposed to live, on the Book or the Page? And what is supposed to happen after it does what it does? Finally, a route is what makes an action an action -- without it, it's just a function.
For a moment, we'll assume you are trying to store the number of times a Page in a Book has been visited. It would be a better idea to have an instance variable called reads or times_viewed etc. in your Page model. Assuming your Book model is using restful routing, in Book's show action, you create an instance variable of the Page model being viewed and increment its reads attribute before rendering the view.
If you are trying to make a link sort of how 'Like' works in Facebook, meaning you want to update a record in a table without sending the user to a different page, you'll need to use link_to_remote* and some javascript. In that case, I'd just refer you to the Railscasts on that subject.
*I think as of Rails 3, link_to_remote became just link_to with :remote => true.

Resources