wanted define custom routes resources in addition ones rails defines default. this, relevant parts of routes.rb
file this:
resource :top, only: [:show] scope module: :top resource :reso, only: [:show] end 'foo_reso' => 'top/reso#foo' 'bar_reso' => 'top/reso#bar'
as can see, want routes resocontroller
's methods show
, foo
, , bar
. works, rake routes
gives:
reso /reso(.:format) top/reso#show foo_reso /foo_reso(.:format) top/reso#foo bar_reso /bar_reso(.:format) top/reso#bar
this works: clicking on link in application takes route foo_reso
result in call resocontroller#foo
, , subsequent display of associated view.
however, thought route definition ugly, , instead of defining routes explicity, wanted rails generate them automatically telling resource has 2 additional rest methods, foo
, bar
(while still restricting standard methods means of only:
argument).
i followed advice in this answer, , changed routes.rb
this:
resource :top, only: [:show] scope module: :top resource :reso, only: [:show] member :foo :bar end end end
now, rake routes
gives:
reso /reso(.:format) top/reso#show foo_reso /reso/foo(.:format) top/reso#foo bar_reso /reso/bar(.:format) top/reso#bar
note difference between paths in 2 cases: while helper path , controller#action same, path has changed /foo_reso(.:format)
/reso/foo(.:format)
.
(i have resource names defined uncountable in config/initializers/inflections.rb
, don't automatic pluralization of names, because in application, each controller associated particular screen, not model, pluralization doesn't fit picture. in app, rest methods more function calls operations on resource, why need different set standard.)
now, clicking on link foo_reso
in application results in rails routing error page says:
no route matches [get] "/foo_reso"
any ideas on fix situation, aside using original solution?
added on edit, 2013-07-12:
as note in comment below, according output of rake routes
, helper route foo_reso
matches controller , method want call, top/reso#foo
, and when manually enter matching url (reso/foo
) url bar, works intended. however, trying open route foo_reso
within application results in no route matches [get] "/foo_reso"
. foo_reso_path
, foo_reso_url
result in same error.
what on earth can problem be? surely not bug in rails?
added on edit, 2013-07-22:
to make use case bit clearer, idea when user presses "reset" button on page, controller's reset
method called, clears page's inputs , outputs. have been abstracting question changing actual identifier reset
foo
. (reso
, and couple of other identifiers result of "obfuscation".) i'll continue use same translations consistency's sake, code below should clearer when bear in mind foo
reset
. (bar
else, doesn't matter, since routing problem same.)
to answer thong kuah's , john hinnegan's questions, how use foo_reso_path
. relevant parts of file reso_controller
:
class top::resocontroller < top::resosupercontroller # /reso def show ... @reset_path = "foo_reso" ... end # show # /foo_reso def foo perform_foo_action redirect_to reso_path end ... end
the relevant parts of file app/views/top/reso/show.html.erb
:
<%= render partial: "top/resosuper_inputs", locals: { the_form_path: reso_path } %> ... <input type="hidden" id="reset_path" name="reset_path" value="<%= @reset_path %>">
the form includes partial _resosuper_inputs.html.erb
, relevant parts are:
<!-- using "reset" button. --> <%= javascript_include_tag "my_reset_form.js" %> <%= simple_form_for :filtering_criteria, url: the_form_path, method: :get |f| %> ... inputs elided ... <%= f.button :submit, value: "search" %> <%= f.button :submit, type: 'button', value: "reset"), id: 'reset_button' %> <% end %>
resosuper
superclass of 2 different resources, don't think has effect on case. anyway, here's javascript file app/assets/javascripts/my_reset_form.js
:
$(document).ready(function() { /* hang functionality on "reset" button. */ $('#reset_button').click(function () { var reset_path = $('#reset_path').val(); window.open(reset_path, "_self") }); })
it's in jquery, , comment says, makes "reset" button open path value has been stored in hidden input variable id reset_path
. value given form top::resocontroller
in variable @reset_path
, namely, "foo_reso"
.
keep in mind worked fine when defined foo_reso
in routes.rb
this:
get 'foo_reso' => 'top/reso#foo'
also keep in mind replacing foo_reso
foo_reso_path
or foo_reso_url
in assignment statement in resocontroller
made no difference.
the places use foo_reso
in controller , in view, shouldn't problem.
as posted in description, here "usage".
# /reso def show ... @reset_path = "foo_reso" ... end # show
however, above doing setting @reset_path
instance variable literal string `"foo_reso" (which coincidentally matches old route)
what poster wants :
@reset_path = foo_reso_path
which generate right path /reso/foo
instance variable @reset_path
sidenote: poster has done right things here debug route problems. of time, can trust rake routes
. checking 1 can access route directly good, , checking usage of route helper correct crucial
Comments
Post a Comment