How to pass an id in URL to controller? Rails


I want to deal with some data rows with same key_id. I want them to get select by using an id in URL, so I will have /field_data/index/12 and 12 here means the key_id.

I can manually do it in the controller by hardcoding the key_id

def index 
    @key_id = 12
    @field_data ='field_content').where("key_id = ?", @key_id)

and my URL is very general: /field_data

So how to make it be able to get the id in URL?



get "/field_data/index/:key_id", to: "field_datas#index"


def index 
  @field_data ='field_content').where(key_id: params[:key_id])
By : j-dexx

Isn’t it better to just make it an empty string if you want to set it as null, and null if did not really sent something at all?

By : Debet Wah

This video can help you solving your question :)
By: admin