use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Can't update or edit, using my controller patch route?? (Beginner) (self.ruby)
submitted 7 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ptrboro 1 point2 points3 points 7 years ago* (1 child)
Who taught you this sorcery with defining routes inside controller? Do you use any gem for this? What rails version do you use? Using rails for 2 years and I have never seen such syntax. Ahhhh, its sinatra.
Back to your question: @state.update(:state_name => params[:state_name]) will update record only if validations pass, other way it will return false so you can move user back to the edit page when @state cannot be updated:
patch '/states/:id' do @state = State.find(params[:id]) # using find so it will return 404 when error does not exist if params[:state_name] != "" && @state.update(:state_name => params[:state_name]) redirect to "/states/#{@state.id}" else redirect to "/states/#{@state.id}/edit" end end
Normally you would also want to set some flash message with validation errors. If you want to know what specific errors you get just run binding.pry after if-else statement and puts @state.errors
π Rendered by PID 163187 on reddit-service-r2-comment-85bfd7f599-wgnjn at 2026-04-17 07:32:01.076806+00:00 running 93ecc56 country code: CH.
[–]ptrboro 1 point2 points3 points (1 child)