ruby - Rails routing issue, can't figure out the link path -


let me fair outset, , tell i've 'solved' problem i'm describing. solution don't understand not solution, it?

i have resource, newsbites. have index page newsbites. crud actions work fine.

i created separate index (frontindex.html.erb) acts front page of website show newest newsbites. formatting different normal index readers larger photo, more of text of article(more ads too:).

in routing table, have following statements:

 resources :newsbites  'newsbites/frontindex'  root 'newsbites#frontindex' 

rake routes show following:

newsbites_frontindex    /newsbites/frontindex(.:format)   newsbites#frontindex 

if load website root (localhost:3000), works great. there separate menu page rendered @ top, , loads fine. can click on links, except 'home' link, , work fine.

the 'home' link is:

 <%= link_to 'home', newsbites_frontindex_path %> 

when click on linked, following error:

couldn't find newsbite 'id'=frontindex 

the error points 'show' action of newbites controller. here frontindex , show def controller. appear i'm posting them:

  def frontindex   @newsbites = newsbite.all   end     def show    @newsbite = newsbite.find(params[:id])   end 

i don't it. why show action being called newbites_frontindex_path when there both def , views match? now, can around pointing home root_path. doesn't me understand. if not root of site?

any appreciated.

actually i'm surprised code worked @ all. route must define 2 things

  • some sort of regex against url of user matched (newsbites/frontindex different newsbites/backindex)
  • what want given url ? want point controller action

rails won't "guess" action is. or maybe, still able "guess" wanted use newsbites controller, didn't guess action right time :(.

you should declare root this, did

root 'controller#action' 

for rest, there 2 ways can declare it. prefer second one

resources :newsbites 'newsbites/frontindex', to: 'newsbites#frontindex'  resources :newsbites   # stuff added here have context of `newsbites` controller   'frontindex', on: :collection # name of action inferred `frontindex` end 

the on: :collection, means 'frontindex' action concerns newsbites, url generated newsbites/frontindex.

on other hand get 'custom_action', on: :member, means custom_action targets specific item, url generated newsbites/:id/custom_action

edit : rails generate path_helpers based on route declaration

get 'test', to: 'newsbites#frontindex' 'test/something', to: 'newsbites#frontindex' resources :newsbites       'frontindex', on: :collection       'custom_action', on: :member 

will generate path helpers

test_path test_something_path # crud helpers : new/edit/show/delete, etc. helpers frontindex_newsbites_path custom_actions_newsbite_path(id) # without s ! 

you can override adding as: option

get 'custom_action', on: :member, as: 'something_cool' # => something_cool_newsbites_path 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -