Rails route not finding corresponding action - ruby-on-rails

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 %>

Related

No route matches [PUT] but I included "resources" in routes.rb

I'm coding a blog that has two types of articles: "drafts" and "published". I'm using the aasm gem for making the article transition from draft to published or viceverza. There are also three types of users: "regular readers", "editors" and "admins".
As users write articles, admins can evaluate whether to publish them or not. To accomplish this, admins have a view in which they can see both drafts and published articles.
The problem is that when I try to publish the articles I get the No route matches [PUT] "/articles" error, regardless I've added resources :articles in routes.rb.
The code I wrote is the following:
routes.rb
resources :categories
resources :articles do
resources :comments, only: [:create, :update, :destroy, :show]
end
devise_for :users
root 'welcome#index'
get '/dashboard', to: 'welcome#dashboard'
put '/articles/:id/publish', to: 'articles#publish'
articles_controller.rb
...
def publish
#article.publish! # Article transition from draft to published.
redirect_to #article
end
...
dashboard.html.erb
...
<% #articles.each do |art| %>
<h1><%= link_to art.title, art, method: :get %> | id = <%= art.id %> | user_id = <%= art.user_id %></h1>
<div>
<%= art.body %> - <%= link_to "Eliminar", art, method: :delete %>
<% if art.may_publish? %>
- <%= link_to "Publicar", '/articles/#{article.id}/publish' , method: :put %>
<% end %>
</div>
<% end %>
...
I can't see why I get this error if I included the article resource. If you need me to include more code don't hesitate to ask me.
Thanks in advanced.
You should remove your custom routes and put it into resources :articles like this :
routes.rb
resources :articles do
put 'publish', on: :member
resources :comments, only: [:create, :update, :destroy, :show]
end
and you should use this in your view :
<%= button_to "Publicar", publish_article_path(article), method: :put %>
It will generate a form.
The code seems to be correct, so try to reset the server and, by the way, restart the browser. It usually solves strange problems like this :) Let me know if it worked.

Couldn't find User with id=search

I want to a searching function for users. It does not work. So I simplified the method, I just want to refresh the index page when I hit the search button. But it still does not work, it said
ActiveRecord::RecordNotFound in UsersController#show, Couldn't find User with id=search.
please tell me Why
my controller
class UsersController < ApplicationController
load_and_authorize_resource :except => [:index]
def search
redirect_to users_path
end
end
My view
<%= form_tag users_search_path, :method => 'get' do %>
<td><%= text_field_tag :username, params[:username] %></td>
<%= submit_tag "Search", :class => "buttons buttons-rounded buttons-flat-action", :id => "button-new"%>
<br><br><br>
<% end %>
My Route
Procedures::Application.routes.draw do
devise_for :users
#### USER MANAGEMENT ####
resources :users do
resources :rateofpays # professional timesheet
resources :roles
resources :biographies
resources :qualifications do
collection do
put 'complete', :action => 'complete'
end
end
resources :supervisors
end
#### users search ####
get 'users/search' => "users#search", as: 'users_search'
The line
get 'users/search' => "users#search", as: 'users_search'
...is too far down in your routes. the resources :users appears first, and it has a match path that looks like users/:id and the users/search is incorrectly matching against that.
Just move the get 'users/search' to the top... or alternatively define it as a collection method under resources :users
resources :users do
collection do
get 'search'
end

Locale Switcher

I am busy going through PBP - Agile Web Development with Rails and implementing the locale switcher.
However when I try switch between english and spanish I get a error:
No route matches [POST] "/en"
My controller is as follows:
class StoreController < ApplicationController
skip_before_filter :authorize
def index
if params[:set_locale]
redirect_to store_path(locale: params[:set_locale])
else
#products = Product.order(:title)
#cart = current_cart
end
end
end
and an extract of the application.hmtl.erb that is being used;
<div id="banner">
<%= form_tag store_path, class: 'locale' do %>
<%= select_tag 'set_locale', options_for_select(LANGUAGES, I18n.locale.to_s), onchange: 'this.form.submit()' %>
<%= submit_tag 'submit' %>
<%= javascript_tag "$('.locale input').hide()" %>
<% end %>
<%= image_tag("logo.png") %>
<%= #page_title || t('.title') %>
</div>
the routing folder is as follows:
scope'(:locale)' do
resources :users
resources :orders
resources :line_items
resources :carts
resources :products do
get :who_bought, on: :member
end
root to: 'store#index', as: 'store'
end
Cant figure out what I did wrong. If I enter /en or /es in the url it works correctly. However choosing it in the drop down that is created I get the error mentioned
Found the issue, the form_tag was expecting a POST so I changed
<%= form_tag store_path, class: 'locale' do %>
to
<%= form_tag store_path, class: 'locale', :method => :get do %>
and it worked
You are missing an slash in the scope as guides states:
# config/routes.rb
scope "/:locale" do
resources :books
end
http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params

Form submission to different controller - Routing

I have this in my view:
<%= form_for #comment, :as => :post, :url => user_ticket_message_comments_path, :html => { :class => "add-comment", :id => "add-comment-" + #ticket.id.to_s } do |f| %>
<%= f.label :body, "Add comment" %>
<%= f.text_area :body %>
<%= f.submit "Add comment" %>
<% end %>
In my routes.rb:
resources :users do
resources :tickets do
resources :messages do
resources :comments
end
end
end
I get a routing error:
No route matches {:controller=>"comments"}
It looks like you are missing the user, ticket and message:
user_ticket_message_comments_path(#user, #ticket, #message)
You need those parameters, because the URL generated looks like this:
/users/:user_id/tickets/:ticket_id/messages/:message_id/comments
without the arguments, Rails doesn't know how to generate that URL.
Also, consider that the rule of thumb in Rails apps is that "Resources should never be nested more than 1 level deep."
This post by Jamis Buck suggests that instead of arbitrarily nesting resources, you only use one level of nesting like this:
resources :users do
resources :tickets
end
resources :tickets do
resources :messages
end
resources :messages do
resources :comments
end

Routing question in nested resources form for ".each do" structure - Rails 3

In my routes i have:
resources :accounts do
resources :transfers
put '/transfers/:id(.:format)' => 'transfers#accept'
end
In my model:
class Transfer
include DataMapper::Resource
belongs_to :account
belongs_to :alias_from, "Alias"
belongs_to :alias_to, "Alias"
class Account
include DataMapper::Resource
belongs_to :user
has n, :transfers
In my view:
<% #transfers_in.each do |income|%>
Amount: <%= income.amount%> <%= income.account.currency%>
<% form_for ([???, income]), :as => :transfer, :url => {:controller=>'transfers', :action => 'accept'} do |f|%>
Choose the account <%= f.collection_select :account, #accounts, :name, :name %>
<%= f.submit :value => "Accept" %>
<% end %>
<% end %>
How should I call for the account here, if here #transfers_in is called by other association?
#aliases = #owner.aliases.all()
#transfers_in = #aliases.transfers_in.all()
I've tried something like
<% #acc = Account.all()%>
<% #trs = #acc.transfers.get(:id => income.account)%>
<% form_for ([#trs, income]), ....
but that gave me
No route matches
{:controller=>"transfers",
:action=>"accept"}
In rake routes such route exists.
Would be thankful for any help.
In your routes, you should have better results using the macros Rails provides for routing. Instead of doing the old-style route map, try:
resources :accounts do
resources :transfers do
put 'accept', :on => :member
end
end
The router is really smart when it comes to RESTful routes, but when you start manually mapping things, it can get confused... especially when you're doing it inside of nested, RESTful routes.

Resources