Rails: How do I pass custom params to a controller method?

后端 未结 3 521
Happy的楠姐
Happy的楠姐 2021-01-24 06:49

I am fairly new to Rails.

I want to track how often a post is read in order to rank them in order of popularity. Therefore, I will have a database column read_cou

相关标签:
3条回答
  • 2021-01-24 07:00

    One of the below answers is probably a better way, but to answer your question of how to pass extra params. You can just add them to the end of your url helper, so if your existing link looks like:

    link_to 'Show', post_path(id)

    just change it to:

    link_to 'Show', post_path(id) + '?preview=true'

    0 讨论(0)
  • 2021-01-24 07:13

    What about doing something like:

    @post.read_count.increment! unless user_signed_in? && current_user.admin?
    
    0 讨论(0)
  • 2021-01-24 07:22

    Why not instead of sending a param you do it through the controller action itself? If it is hit and is not admin then increment the count, seems like a simpler solution.

    0 讨论(0)
提交回复
热议问题