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
Related
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
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
I've got shallow nested resource routes like below:
resources :venues, shallow: true do
#Halls
get "hall/:id/exhibition" => "halls#exhibition", as: :exhibition
get "hall/:id/visit" => "halls#visit", as: :hall_visit
get "structure", :to => "venues#venue_structure"
resources :asset_types, :booths_tags, :tags, :uploaded_files, :events, :chats
resources :halls do
resources :webcasts
resources :booths do
resources :chats
end
end
end
Below is a simple_form that is currently being used.
= simple_form_for(hall_booths_path(#booth), :html => { :class => "form-horizontal" }, :wrapper => "horizontal", defaults: { :input_html => { class: "form-control"}, label_html: { class: "col-lg-4" } } ) do |f|
= f.simple_fields_for #booth do |b|
The problem is that hall_booths_path(#booth) part is generating /halls/1/booths/new instead of /halls/1/booths
Is there something wrong here that needs fixing?
my booths routes:
hall_booths_path GET /halls/:hall_id/booths(.:format) booths#index
POST /halls/:hall_id/booths(.:format) booths#create
new_hall_booth_path GET /halls/:hall_id/booths/new(.:format) booths#new
edit_booth_path GET /booths/:id/edit(.:format) booths#edit
booth_path GET /booths/:id(.:format) booths#show
PATCH /booths/:id(.:format) booths#update
PUT /booths/:id(.:format) booths#update
DELETE /booths/:id(.:format) booths#destroy
Pass the path in the options hash, not as the first argument:
<%= simple_form_for :booth, :url => hall_booths_path(#hall) do |f| %>
...
<% end %>
Note that the argument to hall_booths_path is a Hall, not a Booth. When you create a child, you need to supply the parent.
Another option is to pass not the URL but the model objects. Assuming #hall is an existing Hall and #booth is a new Booth:
<%= simple_form_for [#hall, #booth] do %>
...
<% end %>
I find this approach much simpler.
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 %>
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.