1

I'm very new to rails so please be patient with me.

In short I'm trying to create a form in which guests to a wedding can enter a simple code (invite_code) and then RSVP. The from should take the invite_code and then take the use straight to the correct invites#show view.

So far so good, but I'm stuck trying to get rails to find a record by something other than and id, I want to find by invite_code. Say I've got an Invite with an id of 4 and an invite_id of 1234, the form is finding the correct record when I enter '4' into the from but not '1234'. Here's some code to explain:

routes.rb

get 'invites/search', to: 'invites#show', controller: :invites 

form

... <%= form_tag invites_search_path, method: :get do %> <%= label_tag :invite_code, "#" %> <%= text_field_tag :invite_code, nil %> <%= submit_tag "Search", name: nil %> <% end %> ... 

invites_controller

... def show if params.has_key?(:invite_code) @invite = Invite.find(params[:invite_code]) else @invite = Invite.find(params[:id]) end end ... 

rake routes output

 Prefix Verb URI Pattern Controller#Action info_index GET /info/index(.:format) info#index invites GET /invites(.:format) invites#index POST /invites(.:format) invites#create new_invite GET /invites/new(.:format) invites#new edit_invite GET /invites/:id/edit(.:format) invites#edit invite GET /invites/:id(.:format) invites#show PATCH /invites/:id(.:format) invites#update PUT /invites/:id(.:format) invites#update DELETE /invites/:id(.:format) invites#destroy 

invites_search GET /invites/search(.:format) invites#show root GET / info#index

URL example

.../invites/search?utf8=%E2%9C%93&invite_code=1234 "utf8"=>"✓", "invite_code"=>"1234", "id"=>"search" 

The application seems to be ignoring the invite_id part of if statement in the controller...

Any help appreciated, it's taken me a long time to get this far...

3 Answers 3

4

You've got a couple of options. find_by_invite_code will return you the first match:

Invite.find_by_invite_code(params[:invite_code]) # First match or nil 

While where will give you all the matches as an Array

Invite.where(invite_code: params[:invite_code]) # Array of matches. May be empty 

You can also use the following syntax for find_by:

Invite.find_by(invite_code: params[:invite_code]) # First match or nil 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for this. Do you happen to know why if I change the route to invite#edit and switch the 'edit' action in the controller I get a ActiveRecord::RecordNotFound in InvitesController#show error? It doesn't make sense as I seem to have changed the applicable code to direct to 'edit' not 'show'...
@lkemitchll - Did you restart the server? I believe routes are only parsed at boot (BICBW).
Yes, I still get the same exception " def show [at]invite = Invite.find(params[:id]) end". Which would suggest it's still somehow routing to 'show'.
Looking at your URL example, I think Rails is matching /invites/search?stuff against GET /invites/:id. It's probably related to the order of your routes file. If you can't figure it out, open another question.
Ok, Here's the new question: stackoverflow.com/questions/27861734/…
1

find uses id field by default, use where instead

if params.has_key?(:invite_code) @invite = Invite.where(invite_code: params[:invite_code]).first 

Comments

0
... def show if params.has_key?(:invite_code) @invite = Invite.find_by(invite_code: params[:invite_code]) # find_by argument: value # returns first match or nil # same as find, where find searches by id # Invite.find_by_invite_code params[:invite_code] is deprecated else @invite = Invite.find params[:id] end end ... 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.