params appearing in params[:format] when passing to _path route helper - ruby-on-rails

I have a function which calls a _path helper function with several parameters:
v2_specific_path(user, filter_params)
This works great, and in the controller method that handles this path I see both user and filter_params and their values.
In fact if I try to print out params they don't show up, but if I print out either one of them then they print out fine. filter_params has a helper function which builds a hash from several params[:keys] so that might be the reason.
What I'm trying to do is add another set of parameters to the function call:
v2_specific_path(user, new_params, filter_params)
Since I'm trying to add some functionality which requires passing some information to the underlying controller that resolves this request.
The problem is that this doesn't work and seems to screw things over:
If I put them in the middle: v2_specific_path(user, new_params, filter_params), then all the other params work great, but new_params actually appear inside params[:format] as: params[:format]="param1=4&param2=hi
If I put them at the end: v2_specific_path(user, filter_params, new_params) then filter_params doesn't get parsed at all and appears in params[:format] but new_format does appear to be parsed and appear in params correctly
I can't seem to figure out why this is happening. I've made sure I have correct params.require/params.permit values in the controller.
Any help is appreciated, if anyone has any other alternatives to passing values from application_helper to a controller (via _path, or some shared variable) that would also help immensely
Thank you!

For anyone seeking a solution to this:
I fixed this by doing two things:
1) created proper new routes which suited my implementation (I tried to do something a bit too clever and decided to just create new relevant routes instead of doing something too dynamic
2) I added the params I needed in to the routes: /something/:param1/whatever/:param2 etc.
This worked for me

Related

How to create a custom route for show action in rails 5?

I have a model 'Item'. It all works fine, however am not completely satisfied with its show path. I want to use certain parameters from items table to construct a more SEO friendly url. So here is my question..
How can I change my Show action url
from
'mysite.com/items/1'
to
'mysite.com/items/item-name/item-city/item-id' where item-name, item-city, and item-id are dynamic for each specific item.
Is it possible to achieve this without a gem? if yes, how? If no, which gem would you suggest to achieve this in simplest way?
Thanks
One approach to this problem is to use route globbing:
http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments
You should be able to do something like this:
get 'items/*names/:id', to: 'items#show', as: :item_names_path
And put whatever you want in *names. This would take a little experimentation to get it right. I might add a method to item to create the names array.
def names
[city.name, name].uniq.compact
end
Then I believe you would call item_names_path(#item.names, #item)
You can do something relatively simple and stays true to Rails by adding a to_param method to your model like so:
def to_param
"#{id}-#{name}-#{city.name}"
end
What this does is every time you use a method like the item_path, it will use the #item.to_param method (this is what it does now, and returns :id). Generating the normal route, but replacing the :id param with the SEO friendly one.
And, on the other end, when you go to find(params[:id]) in the controller in your show, edit, delete, or update actions, it will to a to_i on it and turn it back into an id. This is what it does now, but to_i on an int is still an int.
Your urls will look something like
/items/56-boston-itemname
The other benefit to this, if you happen to change the item name or the city name, it will change all the urls appropriately, but old urls that were sent in email will still work.

Using parameter[:user_id] ,params[:user_id], params["userid"] in Rails?

While studying a Rails application I saw statements like:
parameter[:user_id]
params[:user_id]
params["userid"]
Can anyone tell me if there any major difference between them? Or, are all
fetching parameters only and can I use them interchangeably?
parameter[:user_id]
I don't think this is something official. However there's a parameters method on the current request object. See request.parameters in a controller action.
params[:user_id]
Using the params[:user_id] is the same as calling request.parameters[:user_id]. Also params[:user_id] is the same as params["user_id"]. See HashWithIndifferentAccess.
I am not sure if that's just a typo on your part, but params[:user_id] and params["userid"] are not the same, even with HashWithIndifferentAccess. The _ won't just go away so they can hold different values.
No, you need to look at the context in which each one is used. params is the normal parameter hash made available to methods in your controllers. It contains the parameters passed in as part of the request as GET or POST params.
So, given the following URL:
http://www.testsite.org/some_resource?user_id=13
The params[:user_id] would contain the value 13.
If the URL instead was:
http://www.testsite.org/some_resource?userid=13
You would need to use params[:userid] to get the value. So it all comes down to the way the URLs are made for the different controllers.
There's a third way, where you can map parts of the URL itself to params in the routes into your application. See config/routes.rb in your application. For instance with the following route:
match '/user/:user_id' => 'users#show'
You could pass in an URL like this:
http://www.testsite.org/user/13
And in your UsersController.show method you could access the user id by using params[:user_id] like normal.
The parameter hash is most likely a local copy of or reference to the params hash in a helper method or something.
params and parameters are the same. They return both GET and POST parameters in a single hash.
There is no such a thing as parameter.
ref: rails api

Why can't I use a param called "action"?

Is "action" as a input field name forbidden? Because everything works except the assignment of the "action" param.
because action, controller are prohibited words.
Look around debug params
--- !map:ActiveSupport::HashWithIndifferentAccess
action: index
controller: main
so you can't use those params. Because they will be REWRITED AUTOMATICALLY
I would suggest NOT using words like action, name, method as field names as they are all attributes of the form tag and are likely to get confused when the form is posted
I agree with jbeynon, I would also say anything that has to do with CRUD(Create, Read, Update, Delete) is protected also.
I don't see why this would be invalid. You'd want to avoid conflicting with existing class or method names (e.g. not a good idea to define a method called action on a controller).
everything works except the assignment
of the "action" param.
Does this generate an error? If so, what exactly?

Is there anyway to pull the current url inside a controller?

I'm doing some serious hacking. And at a certain point in my row of methods, all my params are lost, and there's no way for me to differentiate between two objects innapropriately sharing the same controller.
I thought I could do it by calling something like params[:foo] == "bar" , but at this point in the series of methods, there are no params available. That is because it is a method called before create.
create.before :create_before
However! They would have different URL's! So if I can just find a way to pull the params of the current url, I can solve this problem.
Any takers?
This is Rails 2.3.5
If you're actually in the controller, you can call request.path, which will return the currently requested path. However, given that params is not available, you may not actually be in a controller at that point. You have any code you can share?

Whiny Nils in Rails

So I generate my scaffold in rails, and it creates the usual CRUD files. In my View, I copy over the form found in new.html.erb and paste it over at index.html.erb, so I can create a new record from my index. When I do that, I get the following error consistently, no matter what I do.
Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id
I got tired of searching all over the web for answers, and just learned it's called a whiny nil (not much help). I tried renaming my instance variables, capitalization, using global variables, etc. but it's frustrating that Rails doesn't have an error documentation library. Can anyone help?
Do you have all the required objects present in your controller? Looks like it's calling something.id, but that something does not exist in your index action. Look at the whole error message - it should be saying what line is causing it, then check that line in source files for the missing variable.
In your controller, you need to add the code found in new into index (I guess it is something like #model = Model.new). Or better: Make a private method which has name expose_new or something like that, move the common code down there and add before_filter :expose_new, :only => [:index, :new].
Just a side note..
If I were you, I'd make a partial out of your form in new, and render that in both index and new (and quite possible edit if them all are equal) so you don't need to copy paste it in.
So, you'll end up with one _form.html.erb which includes the form, and in new and index, you have <%= render('form') %>

Resources