I've got the following route defined:
get 'invites/search', to: 'invites#edit', controller: :invites
Which I changed from:
get 'invites/search', to: 'invites#show', controller: :invites
The controller is as follows:
def show
#invite = Invite.find(params[:id])
end
...
def edit
# If the request is a search containing an 'invite_code'
if params.has_key?(:invite_code)
#invite = Invite.where(invite_code: params[:invite_code]).first
else
#invite = Invite.find(params[:id])
end
end
I get the following error:
ActiveRecord::RecordNotFound in InvitesController#show
Couldn't find Invite with 'id'=search
Extracted source (around line #7):
6 def show
7 #invite = Invite.find(params[:id])
8 end
def new
Which suggests the route hasn't updated?
Here's the rake route:
invites_search GET /invites/search(.:format) invites#edit
I've tried restarting the server, but I'm not really sure what to do now? How can I get the route to go to 'edit' not 'show'?
Thanks,
LM.
EDIT:
Here's the form from the index page:
<%= form_tag invites_search_path, method: :get do %>
<%= label_tag :invite_code, "#" %>
<%= text_field_tag :invite_code, nil %>
<%= submit_tag "Search", name: nil %>
<% end %>
EDIT 2:
See here for the explantion of what I'm trying to achieve with this code:
Ruby on Rails: Find a record by an attribute not an id
Why don't you try to create another name for that method because resources :invites always generate the complete CRUD so maybe it's a conflict
Try something like that in routes.rb
resources :invites do
resources :guests
collection do
get 'search'
end
end
Then you call it in the form like that
<%= form_tag search_invites_path, method: :get do %>
Related
I have a site-wide search form in my application.html.erb. Which is meant to go to the #search action in my controller. Instead, it perpetually attempts to go to #show. I've tried a bunch of hacks to understand why and they're all busts. Any guidance would be most appreciated.
Attempts:
I stripped everything back to research, and just render plain: "OK"
in the controller, going straight to the route in the browser and it
still presents the #show action.
I've restarted the server several times.
Revised the collect do syntax in routes.rb to a one-line hardcoded route get 'lists/search' => 'lists#search', as: 'search_lists'
Checked in the rails console to see if Searchkick works, List.search "*" returns the expected results
I randomly put the def search in the application/controller, not sure why, but I thought I'd see if that did it.
application.html.erb
<%= form_with(url: search_lists_path , method: 'get', local: true) do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag('Search') %>
<% end %>
search.html.erb
<%= content_tag(:h1, "Search Results") %>
<% #list.each do |list| %>
<%= list.name %>
<% end %>
routes.rb
Rails.application.routes.draw do
resources :lists
resources :lists do
collection do
get 'search'
end
resources :products
resources :comments
end
root 'lists#index'
end
controller
class ListsController < ApplicationController
def search
render plain: "OK"
end
def index
#list = List.all
end
def show
#list = List.find(params[:id])
end
Excerpt from rails routes
search_lists GET /lists/search(.:format) lists#search
I'm receiving this error message:
No route matches [POST] "/users/9"
I'm trying to figure out a way to have the update method of one controller and use it in the view of a show method of another controller. This is what my route file looks like.
routes.rb
Rails.application.routes.draw do
root 'dashboards#index'
devise_for :users
resources :users, only: [:show]
patch '/users/:id' => 'companyinfos#update'
put '/users/:id' => 'companyinfos#update'
resources :dashboards, only: [:index]
end
I'm finding the correct companyinfo through the users controller
users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#companyinfo = Companyinfo.find(current_user.id)
end
end
and I'm saying to use this method inside of my company_info controller
companyinfos_controller.rb
class CompanyinfosController < ApplicationController
def update
#companyinfo = Companyinfo.find(current_user.id)
if #companyinfo.update(companyinfo_params)
flash[:notice] = "Saved ..."
else
flash[:alert] = "cannot save"
end
render 'edit'
end
private
def companyinfo_params
params.require(:companyinfo).permit(:CompanyStage)
end
end
and finally I'm using form_for to update it on users_path
show.html.erb
<%= form_for :companyinfo do |f| %>
<%= f.text_field :CompanyStage %>
<%= f.submit "save", class: "btn btn-success" %>
<% end %>
Updating the information in the database works fine when I update it through companyinfo/:id/edit but if I try to update it on another view such as users/:id I'm getting the error message above. What is the proper way to go about this?
UPDATE
Actually if I just do what the error message is telling me it works. Miracle right? Another question. Is there another way to have multiple routes from different controllers pointing to the same location? Or is this the best way?
Error says: No route matches [POST]
Update routes:
post '/users/:id' => 'companyinfos#update'
Or form method:
<%= form_for :companyinfo, method: :patch do |f| %>
I'm new to the ruby. He saw such a mistake, he looked through a lot of articles, I do everything exactly as instructed, but nothing comes out. With Create everything is in order but on the update swears.
my action:
...
def edit
#person = Person.find(params[:id])
end
def update
#person = Person.find(params[:id])
#person.update_attributes(person_params)
if #person.errors.empty?
redirect_to #person
else
render 'edit'
end
end
private
def person_params
params.require(:person).permit(:number, :family, :name, :patronymic, :other)
end
my views:
<h1>Редактировать личность</h1>
<%= form_for #person, url: persons_path do |f| %>
<p>Номер</p>
<p><%= f.text_field :number %></p>
...
<p><%= f.submit "изменить базу" %></p>
<% end %>
and router:
Rails.application.routes.draw do
resources :persons
end
You don't need to pass , url: persons_path, since persons is resource, form_for #person will figure out that it has to go to update action.
<%= form_for #person do |f| %>
should be fine.
If you still do need to pass the url, it should be url: person_path(#person), html: {method: "patch"})
Read more about this topic in Rails Edge guide -
http://guides.rubyonrails.org/v4.1/form_helpers.html#relying-on-record-identification
Note: As #engineersmnky also pointed, your config/routes.rb should traditionally have resources :people instead. Resource is specified in pluralized form.
If you generate scaffold with Rails - rails g scaffold People name:string, you will see that you get:
model - person.rb
controller - people_controller.rb
route helper - resources :people
I am having the following form in my profile/edit.html.erb
<%= form_for #customer, url: {controller: :customer, action: :update} do |f| %>
<%= f.text_field :username, value: #customer.username%>
<%= f.submit 'Update' %>
<% end %>
And in my customer controller i have following method
def edit
#customer = Customer.find session[:customer_id]
end
def update
#customer.update(customer_params)
end
private
private
def customer_params
params[:customer]
end
When i submit the form getting route error
No route matches [PATCH] "/customer/update"
I have added following line in route.rb then too facing same error
resources :profiles
resources :customers
Your update action never defines #customer before you use the update method. Try something like this in your update action:
#customer = Customer.find session[:customer_id]
#customer.update(customer_params)
Also, your customer params is not set correctly. To get it to permit the items in the form, use the following:
def profile_params
params.require(:customer).permit(:username)
end
The easiest way to debug issues like these is to look at the output of 'rake routes' and see if the URL you are trying to call is present in the output.
If I have to take a guess...it seems like you will need to pass in the ID of the customer in the patch request..something like /customer/1/update where 1 is the id of the customer in your database
EDIT : actually just take out the url part of the form_for. I think that is what is throwing off rails. So your form_for should look like
<%= form_for #customer do |f| %>
change controller: :customer to controller: :customers
In 'app/views/users/reset.html.erb' file I have this code:
<%= form_tag( send_reset_users_path, :method => :post ) do %>
<%= text_field_tag :email %>
<%= submit_tag("Send") %>
<% end %>
In 'app/controllers/*users_controller.rb*' I have this code:
def reset
respond_to do |format|
format.html # reset.html.erb
end
end
def send_reset
...
end
In 'config/routes.rb' I have this code:
resources :users do
collection do
get 'reset'
get 'send_reset'
end
end
When I submit the form I get the error: "No route matches "/users/send_reset"" (browser URL becomes '.../users/send_reset'). What is wrong? How can I "map" URLs to Rails actions?
P.S.: I think the problem is in "config/routes.rb"...
the problem is here :method => :post and get 'send_reset', in my opinion you are trying to POST parameters when your conntroller expect GET method
You routes.rb declares the send_reset route as only available via get. You have to write post 'send_reset':
resources :users do
collection do
get 'reset'
post 'send_reset'
end
end