简体   繁体   中英

rails link_to maps to incorrect action

I have the following link set up

 %span{title: 'Publish/unpublish ALL', id: 'publish_all_link'}= link_to image_tag('lock.png'),
    toggle_publish_all_courses_path(@course.id),  remote: true, class: 'icon_link_button',
    'data-publish-all': "#{@course.public}", style: 'margin-top:-1.25em; background-color: #dfd'

When I mouse over it in the view I get what I expect:

foo.yuuk.dev/courses/toggle_publish_all.275

where 275 is the id of the current course . But when I click on it, it is routed to the wrong action:

D, [2015-08-16T08:40:42.134233 #58161] DEBUG -- : Action controller:
D, [2015-08-16T08:40:42.134869 #58161] DEBUG -- : params = {"action"=>"show", "controller"=>"courses", "id"=>"toggle_publish_all", "format"=>"275"}

D, [2015-08-16T08:40:42.135710 #58161] DEBUG -- : 'set_course' = "set_course"

D, [2015-08-16T08:40:42.135830 #58161] DEBUG -- : params = {"action"=>"show", "controller"=>"courses", "id"=>"toggle_publish_all", "format"=>"275"}

I've checked by routes - rake routes | grep toggle_publish_all rake routes | grep toggle_publish_all yields

toggle_publish_all_courses GET    /courses/toggle_publish_all(.:format)  courses#toggle_publish_all

At this point I am stumped, blocked, etc. Any help is greatly appreciated.

Well, your rake routes | grep toggle_publish_all rake routes | grep toggle_publish_all is clearly telling that it don't need any id at all. Look the url /courses/toggle_publish_all . But yes, if you want to pass any id with the url, you can make a query string for that. Like

link_to image_tag('lock.png'), 
        toggle_publish_all_courses_path(course_id: @course.id),..

What you saw in view, is also not a correct url, that was your first clue as it is wrong:

foo.yuuk.dev/courses/toggle_publish_all.275 # << see this

.html , .js , .xml etc are format, not .275 . link_to allows you to pass the format also by passing like format: :xml like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM