RoR link to specific controller in routes.rb - ruby-on-rails

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

Related

"No route matches [PATCH] "/authors/posts"

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

will_paginate confusing urls with devise authenticated root routes

I have the following in my routes.rb:
...
authenticated :user do
root to: 'game_shelves#index', as: :authenticated_root
end
unauthenticated do
root to: 'editions#index'
end
...
resources :editions, :path => "games" do
collection do
get 'to_review'
post 'review'
get 'existing_work'
end
member do
put 'split'
end
resources :expansions do
end
end
...
will_paginate is like this:
<%= will_paginate #collection, renderer: BootstrapPagination::Rails %>
And the result links are like this:
http://localhost:3000/?page=2
If I make unauthenticated root be other thing than editions#index, the links are correct, like this:
http://localhost:3000/games?page=2
I've tried using this:
<%= will_paginate #collection, :params => {:controller => '/editions', :action => 'index'}, renderer: BootstrapPagination::Rails %>
But it does not force will_paginate to use the right route. And if I try this:
<%= will_paginate #collection, :params => {:controller => editions_path }, renderer: BootstrapPagination::Rails %>
Rails give me route errors:
No route matches {:action=>"index", :controller=>"games", :page=>2}
I've inverted my routes.rb and it works now. It seems order is important and I was not aware.
...
resources :editions, :path => "games" do
collection do
get 'to_review'
post 'review'
get 'existing_work'
end
member do
put 'split'
end
resources :expansions do
end
end
authenticated :user do
root to: 'game_shelves#index', as: :authenticated_root
end
unauthenticated do
root to: 'editions#index'
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

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

Rails: form for different controller

I'm developing a rails app with a landingpage. On the landingpage, the user can sign up for the app. For login, there is an extra view with an extra controller.
It looks like this:
views/landinpage/index.html --> sign up form
views/login/index.html --> login form
but I only want to have one controller
controllers/login_controller --> create new user from sign up form & check login data
so I have to get a connection between the landingpage view and the login_controller.
This is my attempt:
<%= form_for #login, :url => { :controller => "login_controller", :action => "create" }, :html => {:method => :post} do |f| %>
but it throws a route error:
No route matches {:controller=>"login_controller", :action=>"create"}
I already defined login resources in routes.rb, but it seems that the problem is elsewhere?
resources :logins
any ideas?
try this
class LoginsController < ApplicationController
def new
...
end
def create
...
end
...
end
in your route.rb file write
match '/login/create' => 'logins#create', :as => :create_login
or
resources :logins
in your console - write - rake routes and check your routes
then
<%= form_for #login, :url => create_login_path(#login) do |f| %>
I think your code should look like this:
<%= form_for #login, :url => { :controller => "login", :action => "create" }, :html => {:method => :post} do |f| %>
can't test this right now, but I believe the _controller part is not required.
Update:
Another thing that I'm using a lot and that works:
<%= form_for #login, :url => create_login_path(#login), :html => {:method => :post} do |f| %>
You may have to fix the create_login_path part to match your application's routes but that's how I usually define these views.
Try this
class LoginsController < ApplicationController
def new
...
end
def create
...
end
...
end
in your routes.rb file
resources :logins do
collection do
post :create
end
end
and in your views
<%= form_for #login, :url => create_login_path(#login) do |f| %>>
you can see the html form action part, you can see!
your config/routes has
resources :posts
namespace :admin do
resources :posts
end

Resources