Rails params not receiving variable passed from view - ruby-on-rails

I have defined a CRUD resource called items, which I've added a collection route to called similar_items.
In the view/show, I have: link_to similar_items_items_path(), :similar_to => #item
In my similar_items controller method, what's the best way to get the item that was passed from link_to? The params hash does not contain similar_to which I've clearly passed as shown above. Why is it not being passed into params?

You should pass parameters inside the path helper:
link_to 'Your link label here', similar_items_items_path(:similar_to => #item)

Related

is this the right link_to rails syntax?

<%= link_to "Profile", #user %>
# => Profile
if i use the above code replacing "Profile" with "Category" and #user with #category/#subcategory what do I then point the html link a href etc to?
Check the documentation for this method here.
The first parameter of the link_to method is the displayed text.
Secondly, you can pass in a single instance of an object which will generate a link to the objects #show action.
You may however pass a link explicitly (most common in my opinion).
This can be done by using the Rails path-helpers (user_path(#user)) or by passing in a string.
In your example, if you exchange #user with #category it would link to the categories #show action instead (Assuming you have a Category model and #category isn't nil.
Again, have a look at the documentation of the link_to method and get familiar with it.

What does a link_to helper expand to when passed an array with a symbol and object in Rails?

In Rails, (specifically in ActiveAdmin) what does a link_to helper with an array passed as the second parameter do? Where's the documentation for this?
I have this code:
index pagination_total: false do
column :name, :sortable => :first_name do |user|
link_to user.full_name, [:admin, user]
end
What does that do? What does [:admin, user] do here?
One confusing thing here is that my ActiveAdmin panel is defined as:
ActiveAdmin.register Promotions::User, as: "User" do
So the link actually comes out prepended with the module like so:
admin_promotions_users_path which I don't want. This causes an error since the as: overwrites the ActiveAdmin path_helper. What I want is:
admin_users_path.
In short, I think the error here is that I don't know what a link_helper expands to when passed an array with a symbol an object.
According to Ruby on Rails - link_to method, the method signature is link_to(body, url, html_options = {}). The url parameter can take:
A String like "/users/john-smith"
An Object like #user
An Array like [:admin, user]
A path like admin_user_path(user)
If the url parameter is a String, it just passes that to the href for the link. If the url parameter is an Object, an Array or a path, it passes the value to the url_for method behind the scenes.
url_for will take an Object, determine that the class type is User, and look for a corresponding user_path(#user) route method. With an array, it first takes any symbols and use it to construct the name of the url helper, then it will take any objects and pass it as values. This is how link_to user.full_name, [:admin, user] gets converted to admin_user_path(#user) which finally becomes John Smith
Update
According to this Stack Overflow answer (which links to url_for and polymorphic_url) url_for takes the input and passes it up to the parent method which for some reason adds the current scope. link_to uses url_for by default. polymorphic_path is the method you require if you want to "unscope" it. I tested this concept in a similar situation in my own app and got the same results as #Jwan622.
url_for([:admin, user]) # Does not exist
#=> admin_promotions_users_path
polymorphic_path([:admin, user]) # Exists
#=> admin_users_path
Therefore, you should either use this:
link_to user.full_name, admin_users_path
Or this (if you need to generate the link dynamically):
link_to user.full_name, polymorphic_path([:admin, user])

Rails - Passing parameters to create filled in form

I want to pass some parameters with link_to method to create method, so that the form will shown prefilled to a user.
I wrote this code to pass the parameters,
<%= link_to "Buy", new_transaction_url(:friend_id => #friend.id, :t_type => 2) %>
And in transactions_controller's new method, I have:
#transaction = Transaction.new
#transaction.t_type = params[:t_type]
It didn't work as well.
That would be great if you can help me.
Thanks.
Try change params[:t_type] to params[:transaction][:t_type].
Usually new_transaction_url would call the #new action in your controller, not the #create action. Watch your log file while you do it to see exactly what controller action is being called and what parameters are being passed.

How to Pass ActiveRecord Objects as Parameters to Helper Methods in Rails

Many helper methods, such as redirect_to, link_to, and url_for, can take an ActiveRecord object as a parameter instead of a hash that specifies the controller and action. I've seen the parameter passed different ways in different documentation. It sometimes gets passed as a symbol, sometimes as an instance variable, and sometimes as a local variable.
I'm confused about how the different parameter styles get expanded to return urls. I know that following REST conventions should create a url constructed of a controller and action but am unsure when Rails needs a specific parameter style to construct that url. Please help me understand the use cases for passing the ActiveRecord object as a symbol, an instance variable, or a local variable. Are there different requirements based on the method call? Or are there underlying differences in url construction?
Here are some examples:
From the API docs:
link_to "Profile", #profile
redirect_to post
<%= url_for(#workshop) %>
<%= form_for :person do |f| %> (this is described as the “generic #form_for”)
From the Ruby on Rails Guides:
<%= link_to 'New book', new_book_path %>
redirect_to(#book)
form_for(#article)
From the Rails 3 Way:
'link_to' "Help", help_widgets_path, :popup => 1
redirect_to post
url_for(timesheets_path)
form_for offer do |f|
Note: Upon further research, it seems that form_for is able to accept a local variable in the case where the calling view template passes a :locals hash as a parameter. The keys are the locals that can be used in the partial and the values are the instance variables from the template. Is that the correct understanding?
You can pass objects to link_to and url_for
You can also pass an object to a path helper
Both #post and post are objects. #post is an instance variable, and post is either a local variable or a method that will return a post
The only "weird" one is the form_for :post variety. This is old school Rails syntax, and will change this to the form_for #post syntax under the hood.

Rails pass :id with params

Using the rails button_to helper I'm trying to run the update method in a controller. I need to set the ID of the object being updated manually. The code I have I think should be right, but rails keeps trying to put the ID as part of a route.
in the view:
button_to ">", :controller=>'people', :action=>'update', 'person'=>{:team=>team_leader.team}, :id=>currently_viewing_person
in the controller:
def update
#person = Person.find(params[:id])
#...rest of method
end
The controller update method is never executed. The error on the web browser is:
Unknown action
No action responded to 3. Actions: create, index, new, search, show, and update
'3' was the value of currently_viewing_person
What's the correct way to pass :id so update can extract from params[:id]
button_to uses POST by default. For update you need a PUT, so pass in the method along with the other parameters:
button_to ">",
{ :controller=>'people', :action=>'update',
'person'=>{:team=>team_leader.team},
:id=>currently_viewing_person },
:method => :put
Note that the method has to be passed in as a separate hash
Here's to reading the documentation again, and understanding that in a REST architecture, update is sent via PUT, not POST. The correct view code is:
button_to ">", {:controller=>'people', :action=>'update', 'person'=>{:team=>team_leader.team}, :id=>currently_viewing_person}, :method => :put
Oddly, if I view source on this page the form created by button_to shows POST being used:
<form method="post" ...
But the rails log confirms that PUT was indeed used:
Processing PeopleController#update (for 127.0.0.1 at 2010-07-15 00:10:09) [PUT]

Resources