I made custom REST action called makedefault:
class PicturesController < ApplicationController
def makedefault
...
end
end
But I get No route matches [POST] "/pictures/12" error.
In routes I've tried:
get "/pictures/:id" => "pictures#show"
But it didn't help. How can I make my own REST action and route it properly?
Since you want to create a POST route (this is what I understand from the question), you should try this:
post "/pictures/:id" => "pictures#makedefault"
The second argument after # must be pointed to your custom action.
Related
I am trying to redirect from a show action to a custom collection action, but the id param is being carried over, causing the routing to fail. A minimal example:
routes.rb:
resources :first_models, only: [:show]
resources :second_models do
get 'custom_action', on: :collection
end
first_models_controller.rb
class FirstModelsController < ApplicationController
def show
redirect_to controller: 'SecondModelsController', action: 'custom_action'
end
end
second_models_controller.rb
class SecondModelsController < ApplicationController
def custom_action
# Do something
end
end
After setting that up, navigating to /first_models/2 results in an error:
No route matches {:action=>"custom_action", :controller=>"SecondModelsController", :id=>"2"}
I cannot figure out how to strip out the id param from the original request so that the routing matches.
The reason why this happens is that you call redirect_to with a Hash argument. Internally Rails uses url_for to build the final location, which in turn uses default_url_options which uses the ID of the current resource. From the API docs:
Missing routes keys may be filled in from the current request's parameters (e.g. :controller, :action, :id and any other parameters that are placed in the path).
See: http://api.rubyonrails.org/v5.1/classes/ActionDispatch/Routing/UrlFor.html
Solution: Use a named path helper.
Run bundle exec rake routes on the command line to get a list of all your routes and named path helpers. Pick the one that you need and use it as follows:
redirect_to my_named_path_helper_path
It is not the param the problem:
class FirstModelsController < ApplicationController
def show
redirect_to controller: 'second_models', action: 'custom_action'
end
end
You can type rails routes and see all your routes and how rails recognize them.
This should work. However you can be more explicit and use:
redirect_to custom_action_second_models_path
I am trying to create a small rails blog, and have run into an error. I think I've messed up the naming convention of something here... but I can't find anything specific enough to help me and give me the answer.
I have a route of
resources :blog
and a controller which has the following
class BlogController < ApplicationController
def index
#blogs = Blog.all
end
def show
#blog = Blog.find(params[:id])
end
def new
#blog = Blog.new
end
end
When I try to set up a form on the new.html.erb page, it links to routing which states
undefined method `blogs_path'
My route is blog, not blogs... where am I going wrong? I would like the route to be /blog.
Following the conventions, rename your controller file to blogs_controller.rb and the class inside of it to BlogsController. Then, in your routes.rb: resources :blogs. It should all work fine.
As already said, it should be :
resources :blogs
And I think the controller should use the plural too : BlogsController
To find what's wrong with urls you can use rake routes command
Values in first column Prefix are names of helpers you can use as prefix_url or prefix_path
I am having some difficulty figuring out how to route static pages in rails 4. I have created a controller called PagesController and so I also have a views folder called pages with the oakville.html.erb file in it.
My controller looks like this:
class PagesController < ApplicationController
def our_mission
end
end
My routes file looks like this:
get "oakville", :to => "pages#oakville"
I am assuming that I should be able to get to this page by going to localhost:3000/oakville ??
Yes, but you need to add a controller action for oakville
class PagesController < ApplicationController
def oakville
end
end
Also, you will need to create oakville.html.erb and put this into your views/pages directory
The methods in your controller are called actions, and for each static page that you want to be able to navigate to you will need a corresponding controller action. When a person (or link) navigates to yoursite/oakville your routes file needs to know which controller action to perform for the oakville branch of the url.
In the routes that you have shown, get "oakville", :to => "pages#oakville" you are asking the controller to render the oakville action. But there is no oakville action in your controller. Add one and problem solved:
class PagesController < ApplicationController
def our_mission
end
def oakville
end
end
The route you've shown and the action you've shown are completely unrelated.
If you want to route a url like http://www.example.com/oakville to an action called our_mission on the Pages controller, the route looks like this:
get 'oakville' => 'pages#our_mission'
What you've written indicates you expect an action called oakville to exist, and according to the code you've provided, it doesn't.
I'm creating a new webpage were my users can have profile pages, and I can't seem to find the way to make a url like this:
webpage.com/profile/username
To go to my profile_controller action index and use the variable that comes on /username.
But I can't seem to find the way.
profile_controller.rb
class ProfileController < ApplicationController
def index
profile_info = Profile.find(params[:username])
end
end
And I've tried to work it around with the routes.rb but couldn't make it...
This route in your routes.rb should map get requests on /profiles/username to the index action of your profiles controller and pass the username value in params[:username]
get '/profiles/:username' => 'profiles#index'
In Rails the default path for a post 'show' action would be /post/:id.
How do I change it something like /post/:pid?
I have read about overriding the to_param method
def to_param
self.pid
end
That makes passing the pid of the post instance, but it would be available in the action as
params[:id] which is not apt.
What I would like is params[:pid] in any action would give me the pid of the post instance.
Thanks in Advance.
Try to put this in the routes:
match "/post/:pid" => "post#show"