I am creating a website with a blog module. A blog post can either be a draft or published.
A published post can no longer be edited, and a draft cannot be viewed (only edit)
I currently have a resource defined as
resources :posts, :path => "blog" do
collection do
get 'drafts'
end
end
I can access the drafts list using blog/drafts, creating new ones posts using blog/new, and editing drafts through blog/:id/edit.
However, I'd like new drafts to be created using blog/drafts/new and edited using blog/drafts/:id
I need to define the new, create, edit and update routes to use this new scheme. The new and create routes seem quite simple. However I do not know how to handle the edit route in order to remove the action name part.
Also, while looking at the default routes definition, I found in actionpack-3.2.9/lib/action_dispatch/routing/mapper.rb the following :
member do
get :edit if parent_resource.actions.include?(:edit)
get :show if parent_resource.actions.include?(:show)
[...]
end
I do not understand how rails differentiates the :edit and the :show routes, and map the urls accordingly.
Thanks
You can use the following routes. Keep in mind that it requires different file hierarchy, rake routes should be your friend in this.
namespace :blog do
resources :drafts, :controller => :posts, only: [:new, :edit]
resources :posts, only: :show
end
Related
My app has a Company model and User models, I've been working on the path/url structure and have managed to get it working how I wish using nested resources as shown in the code below.
I've used FriendlyID to added company_names and usernames to the models which all works fine.
The path now look how I want they to look:www.mydomain.com/company_name/username
routes.rb
resources :companies, :path => '/', only: [:show] do
# constraints has been added for usernames that include a '.'
resources :users, :path => '/', only: [:show], :constraints => { :id => /.*/ }
end
PROBLEM: I still need the ability to add a new company record but it will no longer work. I understand this is due to changing the routes but I don't understand how to remedy this as it keeps viewing
www.mydomain.com/companies/new as an unknown user with the username 'new'.
I'd be grateful if anyone can point me in the right direction or give me a hard with this.
You have set only: [:show], which it means only show method is allowed.
To create a company, need to add :new and :create.
Something like this only: [:new, :create, :show]
Note:
Always after adding or changing smth in routes file, make sure to use rake routes, Rails 5 supports rails routes also.
You can see available routes!
I'm new to Ruby and Ruby on Rails. I've got an installation of Refinery CMS in which I've created two extensions that are related. I pretty much followed this guide. I've got it all working, except that for my second resource (event_types), I'm not able to view them from the front end. The link to view the Event Types goes to localhost:3000/events/event_types, and that makes the events viewer think I'm trying to look at an event with ID "event_types", and I get a RecordNotFound error. What is set up wrong? It seems like it's something wrong with routes, but I don't know what to change.
Can someone point me in the right direction?
Rails routes are matched in the order they are specified so you have to take care about order in config/routes.rb file.
For example given in guide, in file vendor/extensions/events/config/routes.rb you should put event_type resource routes above event resource route.
# Frontend routes
namespace :events do
resources :event_types, :only => [:index, :show]
end
# Frontend routes
namespace :events do
resources :events, :path => '', :only => [:index, :show]
end
To better understand Rails routes you can check http://guides.rubyonrails.org/routing.html
i am rewritting a rails app I did some 5 years ago using rails 1.something.
When I try to browse localhost/companies/search_updates/ I get this error... I know this is a routing error because when I remove the resources :companies from router.rb the thing works fine... How can this be fixed? And do I need to manually add routes for every action I create?
Error when I try to access localhost/companies/search_updates/
The action 'show' could not be found for CompaniesController
Controler
class CompaniesController < ApplicationController
def index
#companies = Company.all
end
def search_updates
# Execute code to search for updates
# Redirect to results
end
end
Routes
resources :accounts
resources :companies
get 'companies/search_updates' => 'companies#search_updates'
search_updates.html.erb
Hello Updates!
The action 'show' could not be found for CompaniesController
Rails routes are matched in the order they are specified, so if you
have a resources :companies above a get 'companies/search_updates' the show action's
route for the resources line will be matched before the get line. To
fix this, move the get line above the resources line so that it is
matched first.
resources :accounts
get 'companies/search_updates' => 'companies#search_updates'
resources :companies
And
Resource routing allows you to quickly declare all of the common
routes for a given resourceful controller. Instead of declaring
separate routes for your index, show, new, edit, create, update and
destroy actions, a resourceful route declares them in a single line of
code.
If you have only index method (default CURD rails) on your controller, you can specific routes for it.
resources :accounts
get 'companies/search_updates' => 'companies#search_updates'
resources :companies, :only => [index]
I am playing with Rails 4 in a test application. I have an arbitrary URL that isn't standard like a resources :foo type URL. Ideally the end result I'd like is to be able to go to:
/contests/:id/enter
In views, it would be great if I can then set a link using a named helper such as:
edit_contests_enter(:id)?
What would be the best way to define the route above so I can use the helper path with an arbitrary URL like the one above? It doesn't necessarily have to be edit_contests_enter(:id) but as long as the helper path leads to the URL as suggested above, that would be fantastic.
I assume that your contest is a resource, and when your visitor goes to /contests/:id/enter you want them to create an object user <=> contest. Let's call it participation.
Now participation is exactly like any other resource in your Rails app, so you'd have a routes.rb file looking like
resources :contests do
resources :participations
end
You don't want people to do anything other than create a participation, like edit or destroy them. And perhaps you want a nice URI like /contests/:id/enter. All you have to do is
resources :contests do
resources :participations, :only => [:new, :create]
get "enter" => "participations#new"
end
Doing such will give you a routes helper named contest_enter. In your participations#new form, you'll POST as usual to /contests/:id/participations.
If you have a resources block for :contests, you could just define a new "member" route on the ContestsController using:
resources :contests do
member do
get :enter
end
end
And that would automatically generate you a named member route, the name of which you could find by running rake routes.
Since I used scaffolding to create my Deposit model,
resources :deposits
generates a bunch of routes. i want to be able to delete some of those routes for particular models. For example I do not want a "deposts/23/edit" route. I know I can do a redirect in the controller or do a
match "deposits/:id/edit", :to => "deposit#new"
which just shows the new deposit page but does not change the url on the browser.
is there a way to completely remove a certain action through declaring something in the rails routes.rb file. so that particular actions are just not accessible.
I reccomend looking over the Ruby on Rails Guide for Routing it discuses important core concepts and presents you with some good information on how routes are wired the way they are.
If you still wish to remove the edit route you will find a useful part of the guide here the code you need is listed below:
resources :deposits, :except => :edit
resources :deposits, :only => { :create, :new }
resources :deposits, :except => :edit