How to create my own route in rails where I pass the identifier of an existing model?

I created the following route:

  map.todo "todo/today",
            :controller => "todo",
            :action => "show_date"

Initially, the show_date action and its associated view displayed all the activities associated with this day for all campaigns.

As a result, this database was very slow: it would create about 30 records, but it was still slow.

So, I’m thinking of creating a partial one, which will first list the campaigns separately.

If someone clicked on the link associated with campaign_id = 1, I want it to go to the following route:

todo/today/campaign/1

Then I would like to know how to find out that "1" is the campaign_id in the controller, and then just does its job.

, URL- , . , .

. , , , sqlite , , , , .

+3
2

@Damien , . :

map.todo "todo/today/campaign/:id", :controller => "todo", :action => "show_date"

, , :

<%= link_to "Campaign 1", todo_path(:id => 1) %>

<%= link_to "Campaign 1", todo_path(1) %>

params[:id] .

, sqlite .

EDIT: :

, ,

map.todo "todo/today/:campaign/:id", :controller => "todo", :action => "show_date"

:

<%= link_to "Campaign 1", todo_path(:campaign => "campaign", :id => 1) %>

todo show_date:

def show_date

#IF YOU ARE USING THIS REPEATEDLY IN LOTS OF DIFFERENT ACTIONS THEN A BETTER PLACE FOR THIS WOULD BE AS A HELPER IN application_controller.rb

 if params[:id].nil? && params[:campaign].nil?
    #DO SOMETHING WHEN BOTH ARE NIL,
 elsif params[:campaign]!="campaign"
    #DO SOMETHING WITH CAMPAIGN BEING SOMETHING OTHER THAN "CAMPAIGN"
 elsif params[:campain]=="campaign" && params[:id].nil?
    #DO SOMETHING WITH ID BEING NIL.
 else
    #FIND YOUR CAMPAIGN HERE.
 end
end

, .:)

+4

:

map.todo "todo/today/:id",
        :controller => "todo",
        :action => "show_date"

URL /todo/today/:id, id - , URL-.
params[:id].

Rails Routing from Outside In, .

0

Source: https://habr.com/ru/post/1749518/


All Articles