I can't update one user model with my custom route (without resources) and one subdomain.
My form is:
<%= form_for :user, :url => services_users_update_url(#user), :html => { :method => 'put', :multipart => true } do |f| %>
while my subdomain is "services".
My routes file:
constraints :subdomain => "services" do
scope :module => "services", :as => "services" do
match '/users/update/' => 'users#update', via: [:put]
end
end
When I display my form page, the url generated in my form is:
http://services.website.dev:3000/8296/users/update
When I confirm my form, the error message is:
No route matches [PUT] "/8296/users/update"
What's wrong in my routes ?
If I try:
match '/users/:id' => 'users#update', via: [:put]
the form generate a route error too.
I also tried:
match '/users/update/:id' => 'users#update', via: [:put]
without success.
EDIT: I want to add more infos.
My form page is located in the subdomain 'cloud' and my update action is located in the subdomain 'services'.
According to your url:
http://services.website.dev:3000/8296/users/update
^^^^/^^^^/^^^^^
:id/users/update
Your routes should be:
constraints :subdomain => "services" do
scope :module => "services", :as => "services" do
put ':id/users/update' => 'users#update', as: :services_users_update
end
end
Read documentation
You can use the built in resources method to generate your route:
constraints subdomain: "services" do
scope module: "services", as: "services" do
resources :users, only: [:update]
end
end
Related
I have two routes (defined in my config/routes.rb)
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report
I would like to know how I can tell my controller to render the view based on my route, without adding a parameter on my URL.
Based on this Question&Answer I Managed to get the result I want with
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare, :defaults => { :render => 'compare' }
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report, :defaults => { :render => 'full_report' }
And in my controller I defined my action as:
def search_by_id
render :action => (params[:render] || "full_report")
end
But is this a Good Practice or is there a better way to do it?
Instead of creating different routes for each category you are making for simplifying you can write it like:
# config/routes.rb
get ":category", to: "front_office#search_by_id", as: 'front_office', constraints: {category: /(compare|full_report)/}
the above routes looks for /compare and /full_report and this will call search_by_id action in front_office controller.
then inside the controller do as follows:
def search_by_id
render params[:category]
end
params[:category] will hold the slug values which we passed through the URL
My routes are redirecting to the same controller even when I specified different properties inside my routes.rb file.
These are my routes.
match ':clube_id' => 'clubes#show', :as => 'clean_cluble', via: [:get]
match ':project_id' => 'projects#show', :as => 'clean_project',via: [:get]
These are the links that I am using.
=link_to 'Project', :controller => "projects", :action => "show", :project_id=>'xxxxx'
=link_to 'Clube', :controller => "clubes", :action => "show", :id=>'cccc'
The link for projects works well, but the linl for clubes is redirecting to projects controller. that is the problem.
The URLs that I spect are:
http://host_name/project_name
http://host_name/clube_name
You didn't specify different properties, both routes look's identical for Rails. The match method expect any string(or id) in the ':clube_id' or ':project_id', for example:
host_name/soho_project or host_name/1
How is Rails can recognize for a which model it's related? It can be a Project or Club. I suggest add something like the anchor to a match method.
match 'club/:clube_id' => 'clubes#show', :as => 'clean_cluble', via: [:get]
match 'project/:project_id' => 'projects#show', :as => 'clean_project',via: [:get]
and helpers:
= link_to 'Project', clean_project_path(:project_id=>'xxxxx')
= link_to 'Clube', clean_cluble_path(:clube_id=>'cccc')
Read more about routes from the Rails guides.
I am facing this error while running my application:
No route matches [PUT] "/accounts/1/payment"
This is my form syntax;
<%= semantic_form_for(#account, :url => payment_path, :html => { :method => :put }) do |f| %>
......
<% end %>
routes.rb:
get "/accounts/:id/payment" => "accounts#payment", :as => "payment"
controller method:
def billing
#account = Account.find(params[:id])
if request.put?
// some code here
end
end
Replace
get "/accounts/:id/payment" => "accounts#payment", :as => "payment"
with
match "/accounts/:id/payment", to: "accounts#payment", :as => "payment", via: [:get, :put]
and let me know if this works.
EDIT
If your controller action is billing, then you may have to do
match "/accounts/:id/payment", to: "accounts#billing", :as => "payment", via: [:get, :put]
your form is trying to find a route matches method put while your route has a get method.
Use put in routes instead of get
put "/accounts/:id/payment" => "accounts#payment", :as => "payment"
In your form you are telling this form to be treated as PUT request but your routes says it is GET request. You should use same request type in both routes.rb and your form definition.
Try this
put "/accounts/:id/payment" => "accounts#billing", :as => "payment"
I would like to have a custom route querystring based, to access a specified resource. For example:
/opportunities/rent/san-miguel-de-tucuman?id=45045
That route should map to the action OpportunitiesController#show, with the resource id 45045.
Thanks in advance!!!
Updated
This are my current routes:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id', to: "opportunities#show"
get 'oportunidades/alquiler/san-miguel-de-tucuman', to: "opportunities#index"
So, if I navigate to the /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 route, it go to the Opportunities#index action.
P/S: sorry, I forget to mention that I have a similar route for the index action.
Make your custom routes as:
resources: opportunities, except: :show
get '/opportunities/rent/san-miguel-de-tucuman/:id' => 'opportunities#show', :as => 'opportunities_show'
and pass your 'id' as opportunities_show_path(id)
EDIT
Change your routes as:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id' => "opportunities#show", :as => 'opportunities_show'
get 'oportunidades/alquiler/san-miguel-de-tucuman' => "opportunities#index", :as => "opportunities_index"
Now when you want to access your show page just use opportunities_show_path(:id =>123456 )
And for index page use opportunities_index_path
Use this
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :via => :get
and pass a object to the path so created. Eg:-
something_path(#object), here #object is object that with id which will be passed in routes
Option 1
Query string parameter
// /opportunities/rent/san-miguel-de-tucuman?id=45045
match '/opportunities/rent/san-miguel-de-tucuman', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
Option 2
Add id like new parameter. More friendly.
// /opportunities/rent/san-miguel-de-tucuman/45045
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
In both cases, you need to call the route like this:
show_opportunity_url(:id => 45045)
In your controller you will get the id in params[:id]
I have a model called Post. In config/routes.rb, I defined its route as:
resources :post
Everything works fine with the default paths. I can create a new post at the following url:
/posts/new
I need to pass additional parameters so that the new url becomes:
/posts/new/:year/:month/:day
If I do the following, it assumes a post_id should exists:
resources :posts do
match '/new/:year/:month/:day',
:to => 'posts#new',
:constraints => {:year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/},
:as => 'new_post'
end
For the above, rake routes give me:
/posts/:post_id/new/:year/:month/:day(.:format)
How can I configure the default new path to pass additional parameters?
...
match '/new/:year/:month/:day', :on => :new
...