2

EDIT: This is Rails 4

Rails code in the users_controller.rb file

def showobjectdata
  @users = User.all
  @user = User.find_by(:username => params[:username])
  render :json => @user
  end

I have been trying lots of routes, but (add the "localhost" part to the beginning of this URL) /users/showobjectdata/existingusername in my browser returns null.

Please Note: I am able to render JSON data about all users or a specific user, if I look up the user some other way than passing in a parameter which is not an id in the browser's URL field. Like in the controller method I can specifically look up a user by a specific email address. And users/show/:id renders the JSON user data of that id, because I have defined the show controller method to render JSON user data (for now).

Here is an example of a route I tried in my routes.rb file: match 'users/showobjectdata/:username', to: 'users#showobjectdata', via: [:get, :post]

I tried various combinations with plain GET, plain POST, nested parentheses, etc. I always get null except for plain POST which doesn't work.

  • 1
    Did you add the `:username` key as allowed by your strong params configuration in the controller? (If Rails 4) – MrYoshiji Jan 06 '15 at 21:02
  • In which method of the controller, do you mean in the showobjectdata method? I only have params.permit as standard in the users/create method, and validates in the User model file. How do I do what you said? – angularjslearner66 Jan 06 '15 at 21:18
  • I tried mentioning the username parameter in the 'showobjectdata' method indirectly through a private parameters method. It did not work for me. – angularjslearner66 Jan 07 '15 at 01:52

2 Answers2

1

Try this

match 'users/showobjectdata/:username', to: 'users#showobjectdata', via: [:get, :post], param: 'username'
Anil Maurya
  • 2,298
  • 1
  • 22
  • 28
  • It did not work for me. I still get null. Do you think there is some reason I cannot pass in a username to the URL like that? Maybe because my create method uses params.permit and includes username there? Going to see how it works without the params.permit function. – angularjslearner66 Jan 06 '15 at 20:20
  • check generated routes , using rake routes – Anil Maurya Jan 06 '15 at 20:21
  • I see GET|POST /users/showobjectdata/:username(.:format) for users#showobjectdata. All the routes show up correctly, on the rake routes command. – angularjslearner66 Jan 06 '15 at 20:24
  • Also, commenting out the users/create method with the params.permit part did not make a difference. – angularjslearner66 Jan 06 '15 at 20:25
0

This is the right answer.

Basically, my username parameter (firstname.lastname) was not being passed as a full string. It is was being passed as firstname instead of firstname.lastname, with the Rails application considering "." to be where the format parameter started ('lastname' was considered a format input in the passed in parameters). I saw these passed in parameters appear in my browser ironically only when I got another error trying something new (basically my application was not responding to 'respond_to |format|' in the 'showobjectdata' method when I tried it pretty randomly - this of course led to these parameters showing up at the bottom of the screen and the googling of a solution. Yes after getting this insight on the parameters, I skipped the respond_to way and once again just rendered the json user object directly as before, without differentiating between the HTML and JSON formats).

So, basically this is the right route that worked for me:

    match 'users/showobjectdata/:username', to: 'users#showobjectdata', via: [:get, :post], :constraints => { :username => /[^\/]+/ }

The controller method as originally posted is fine!

Source for the ":constraints =>" part: Why do routes with a dot in a parameter fail to match?

Community
  • 1
  • 1