"No route matches [PATCH] "/authors/posts" - ruby-on-rails

When I tap the update button after doing an edit to a post, I get
From the Chrome Browser
"No route matches [PATCH] "/authors/posts"
From the Command line
"ActionController::RoutingError (No route matches [PATCH] "/authors/posts"):"
Here is the Routes file
Rails.application.routes.draw do
devise_for :authors
root to: 'blog/posts#index'
# /author/posts
namespace :authors do
resources :posts
end
scope module: 'blog' do
get 'about' => 'pages#about', as: :about
get 'contact' => 'pages#contact', as: :contact
get 'posts' => 'posts#index', as: :posts
get 'posts/:id' => 'posts#show', as: :post
end
end
Here is the edit.html.erb file:
<% provide(:page_title, "Edit #{#post.title}") %>
<% provide(:author, 'active') %>
<h1>Editing Post</h1>
<%= render 'form', post: #post, url: authors_post_url(#post) %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', authors_post_path %>

It looks like your posts model is nested inside authors model so shouldn't your routes should be like
resources :authors do
resources: posts
end

Related

No route matches [POST] "/article/1/like"

I got an error
No route matches [POST] "/article/1/like"
My articles_controller.rb is.
def like
#article = article.all.find(params[:id])
Like.create(user_id: current_user.id, article_id: #article.id)
redirect_to articles_path(#article)
end
This is my index page.
<% if article.liked?(current_user) %>
<%= button_to "like", like_path(article), methode:"put", desabled: true %>
<% else %>
<%= button_to "like", like_path(article), methode:"put" %>
<% end %>
and routes.rb is
Rails.application.routes.draw do
get 'static_pages/landing_page'
get 'static_pages/dashboard'
devise_for :users
resources :users
resources :articles do
resources :comments
end
put '/article/:id/like', to: 'article#like', as: 'like'
root "articles#index"
end
I am writing this code from a website given below.
enter link description here
The idomatically correct way to add additional actions to a resource is:
resources :articles do
put :like
resources :comments
end
This creates the route /articles/:article_id/like(.:format) - not that articles is plural.
<%= button_to "like", article_like_path(article), method: :put, disabled: article.liked?(current_user) %>
def like
#article = article.all.find(params[:id])
Like.create(user_id: current_user.id, article_id: #article.id)
redirect_to articles_path(#article)
end
in like method use params[:article_id] instead of params[:id]

No route matches {:action=>"show", :controller=>"report"

I have a report#show view which I would to link_to, but I'm unsure about how to set up the routing.
In my packages#show view:
<% link_to 'Report', package_report_path(#package) %>
Here's my routes.rb:
Rails.application.routes.draw do
devise_for :users
resources :packages do
resources :sales, only: [:new]
resources :report, only: [:show]
end
root "packages#index"
end
If I do rake routes:
package_report GET /packages/:package_id/report/:id(.:format) report#show
The route is set up correct, but you need to pass both #package and #report to the package_report_path, like:
<% link_to 'Report', package_report_path(#package, #report) %>
Your report resource is nested under the package resource. So, you have to pass both #package and #report to the package_report_path helper method:
<% link_to 'Report', package_report_path(#package, #report) %>

RoR link to specific controller in routes.rb

I use Ruby 2.0 and Rails 4.2.0. I can't create link for simple_form_for in _form.html.erb. My code:
routes.eb
Rails.application.routes.draw do
namespace :admin, path: 'uceadmin' do
root :to => 'dashboard#index'
namespace :newsletter, path: 'newsletter' do
resources :lists, controller: 'newsletter_lists' do
resources :subscribers, controller: 'newsletter_subscribers'
end
end
end
end
app\views\admin\newsletter_subscribers_form.html.erb
<div class="row">
<%= simple_form_for([:admin, :newsletter, #list, #newsletter_subscriber]) do |f| %>
rake routes:
And what I have in result:
undefined method `admin_newsletter_newsletter_list_newsletter_subscribers_path' for #<#<Class:0xd1d5af0>:0xdc2a7b8>
I guess you should use the :url option to pass URL for submitting. In your case it is admin_newsletter_list_subscribers_path, as I can see.
<%= simple_form_for #newsletter_subscriber, url: admin_newsletter_list_subscribers_path(#list) do |f| %>

Why do I get NoMethodError in Games#new for my game form but the analog for my Users#new

I am creating a a site in RoR and and I have built the user signup and login forms. Everything works great. The thing is, I went to create another object called games, which functions almost identically to users, but when I try to interact with it I get an error. I built the forms almost exactly the same and the routing I congruent.
Here is my user new.html.erb:
<!DOCTYPE html>
<html>
<body>
<% provide(:title, 'Sign up') %>
<h1 class="heading1" >Sign up</h1>
<br>
<div>
<%= form_for(#user, :html => { :class => 'form' }) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<%= f.label :email %>
<%= f.text_field :email %>
<br>
<%= f.label :username %>
<%= f.text_field :username %>
<br>
<%= f.label :password %>
<%= f.password_field :password %>
<br>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<br>
<br>
<%= f.submit "Create my account", class: "submit" %>
<% end %>
</div>
</div>
</body>
</html>
and my users controller new and create methods:
def create
#user = User.new(user_params)
if #user.save
sign_in #user
redirect_to #user
else
render 'new'
end
end
def new
#user =User.new
end
private
def user_params
params.require(:user).permit(:name, :email, :username, :password,
:password_confirmation)
end
end
and my game new.html.erb:
<!DOCTYPE html>
<html>
<body>
<h1 class="heading1" >Create Game</h1>
<br>
<div>
<%= form_for(#game, :html => { :class => 'form' }) do |i| %>
<%= i.label :title %>
<%= i.text_field :title %>
<br>
<br>
<%= i.submit "Create Game", class: "submit" %>
<% end %>
</div>
</div>
</body>
</html>
and my game controller:
def create
#game = Game.new(game_params)
if #game.save
redirect_to root_url
else
render 'create'
end
end
def new
#game = Game.new
end
private
def game_params
params.require(:game).permit(:title)
end
end
and my routing file:
Rails.application.routes.draw do
resources :sessions, only: [:new, :create, :destroy]
resources :users
match '/new_game', to: 'games#new', via: 'get'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'home#home'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
The rails server error page reads:
NoMethodError in Games#new
Showing /Users/Karen/Desktop/BR2/app/views/games/new.html.erb where line #7 raised:
undefined method `games_path' for #<#<Class:0x007fbfd6bdb260>:0x007fbfd6bd8948>
Extracted source (around line #7):
4
5
6
7
8
9
10
<h1 class="heading1" >Create Game</h1>
<br>
<div>
<%= form_for(#game, :html => { :class => 'form' }) do |i| %>
<%= i.label :title %>
<%= i.text_field :title %>
<br>
Rails.root: /Users/Karen/Desktop/BR2
Application Trace | Framework Trace | Full Trace
app/views/games/new.html.erb:7:in `_app_views_games_new_html_erb___3427370169918602482_70230959128880'
Request
Parameters:
None
I really appreciate all and any help. if there is any more information I can provide please say so.
Thank you
The best option is to add games specific RESTful routes in routes.rb
resources :games
and remove match '/new_game', to: 'games#new', via: 'get' route.
Doing this will give you the following Restful routes:
Prefix Verb URI Pattern Controller#Action
games GET /games(.:format) games#index
POST /games(.:format) games#create
new_game GET /games/new(.:format) games#new
edit_game GET /games/:id/edit(.:format) games#edit
game GET /games/:id(.:format) games#show
PATCH /games/:id(.:format) games#update
PUT /games/:id(.:format) games#update
DELETE /games/:id(.:format) games#destroy
So upon form submission your application would route to create action (games_path) by HTTP Post request.
Currently you have just defined a single route for games resource which routes to new action with
match '/new_game', to: 'games#new', via: 'get'
But there is no route for create action which is why you receive the error as undefined method 'games_path' on the form
If you don't wish to use the RESTful routes(resources :games) then you would have to define a route as:
match '/games', as: 'games', to: 'games#create', via: 'post'
for create action.
You will find that when you do bundle exec rake routes in your console, you have not actually created named routes for your games paths.
If you're using match and you want to name a route (so you have something like games_path available), you'd have to do this:
match `/games`, as: 'games', to: 'games#index', via: :get
A much easier way is to use resources for most of your routes, and just go with the default RESTFUL paths:
resources :games
# Now you have access to '/games/new', '/games/:id',
# '/games', etc, as well as names such as `games_path`.
# Check `bundle exec rake routes` for all of them.
See Rails Routing for more information

Rails route not finding corresponding action

Inside app/views/participants/index.html.erb:
<%= form_tag bulk_add_participants_program_path do %>
<%= wrap_control_group do %>
<%= text_area_tag :bulk_add_participants, :size => "60x3" %>
<% end %>
<%= submit_tag "Import Participants and Users" %>
<% end %>
But notice that the controller and route pertain to the Program model (for good UI reasons). And I think that might be related to the problem. When I render that view I get this error message:
No route matches {:action=>"bulk_add_participants", :controller=>"programs"}
Which is weird because in app/controllers/programs_controller.rb:
def bulk_add_participants
puts "yay!" # because i am troubleshooting
end
And my config/Routes.rb is:
RepSurv::Application.routes.draw do
root to: 'programs#index'
devise_for :users, path_prefix: 'devise'
resources :users
resources :programs do
resources :participants do
resources :rounds do
get 'survey' => 'rounds#present_survey'
put 'survey' => 'rounds#store_survey'
end
end
resources :questions
resources :rounds
member do
get 'report' => 'reports#report'
get 'bulk_add_participants'
end
end
end
It's not finding the route because you have programs defined as a plural resource:
resources :programs do
When you do that and reference a member route like your bulk_add_participants, it expects a :program_id parameter in your case. (Try running rake routes, and you'll see a path like /programs/:program_id/bulk_add_participants.)
So your form_tag call should perhaps look like this:
<%= form_tag bulk_add_participants_program_path(#program) do %>

Resources