how to pass parameters in page












-1















I have this page (called "page"). I tried calling those pages by id with <%= link_to page.title, page_path(page.id) %>which is working, but I want to call those pages using params, I don't want their link to be direct like http://localhost:3000/pages/6 but I want that id 6 to be passed through a params, so I'll have a link like http://localhost:3000/pages?category_id=1.



Here's my pages controller



    class PagesController < ApplicationController
def show
@pages = Page.find(params[:id])
end

def all
@category = Category.find_by(id: params[:category_id])
@pages = @category.pages
end

def index
if params[:id].present?
show
else
#Code of index action
@pages = Page.all
end
end
end


and my show.html.erb



<%= @page.title %>
<%= @page.body %>


and then the link to this page



<% @pages.each do |page| %>
<h4>
<b>
<%= link_to page.title, page_path(page.id)%>
</b>
</h4>
<% end %>









share|improve this question




















  • 1





    use the index action and then call show if your param is present

    – Lenin Raj Rajasekaran
    Nov 27 '18 at 5:10
















-1















I have this page (called "page"). I tried calling those pages by id with <%= link_to page.title, page_path(page.id) %>which is working, but I want to call those pages using params, I don't want their link to be direct like http://localhost:3000/pages/6 but I want that id 6 to be passed through a params, so I'll have a link like http://localhost:3000/pages?category_id=1.



Here's my pages controller



    class PagesController < ApplicationController
def show
@pages = Page.find(params[:id])
end

def all
@category = Category.find_by(id: params[:category_id])
@pages = @category.pages
end

def index
if params[:id].present?
show
else
#Code of index action
@pages = Page.all
end
end
end


and my show.html.erb



<%= @page.title %>
<%= @page.body %>


and then the link to this page



<% @pages.each do |page| %>
<h4>
<b>
<%= link_to page.title, page_path(page.id)%>
</b>
</h4>
<% end %>









share|improve this question




















  • 1





    use the index action and then call show if your param is present

    – Lenin Raj Rajasekaran
    Nov 27 '18 at 5:10














-1












-1








-1








I have this page (called "page"). I tried calling those pages by id with <%= link_to page.title, page_path(page.id) %>which is working, but I want to call those pages using params, I don't want their link to be direct like http://localhost:3000/pages/6 but I want that id 6 to be passed through a params, so I'll have a link like http://localhost:3000/pages?category_id=1.



Here's my pages controller



    class PagesController < ApplicationController
def show
@pages = Page.find(params[:id])
end

def all
@category = Category.find_by(id: params[:category_id])
@pages = @category.pages
end

def index
if params[:id].present?
show
else
#Code of index action
@pages = Page.all
end
end
end


and my show.html.erb



<%= @page.title %>
<%= @page.body %>


and then the link to this page



<% @pages.each do |page| %>
<h4>
<b>
<%= link_to page.title, page_path(page.id)%>
</b>
</h4>
<% end %>









share|improve this question
















I have this page (called "page"). I tried calling those pages by id with <%= link_to page.title, page_path(page.id) %>which is working, but I want to call those pages using params, I don't want their link to be direct like http://localhost:3000/pages/6 but I want that id 6 to be passed through a params, so I'll have a link like http://localhost:3000/pages?category_id=1.



Here's my pages controller



    class PagesController < ApplicationController
def show
@pages = Page.find(params[:id])
end

def all
@category = Category.find_by(id: params[:category_id])
@pages = @category.pages
end

def index
if params[:id].present?
show
else
#Code of index action
@pages = Page.all
end
end
end


and my show.html.erb



<%= @page.title %>
<%= @page.body %>


and then the link to this page



<% @pages.each do |page| %>
<h4>
<b>
<%= link_to page.title, page_path(page.id)%>
</b>
</h4>
<% end %>






ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 16:35







Albert Conrad

















asked Nov 27 '18 at 4:51









Albert ConradAlbert Conrad

548




548








  • 1





    use the index action and then call show if your param is present

    – Lenin Raj Rajasekaran
    Nov 27 '18 at 5:10














  • 1





    use the index action and then call show if your param is present

    – Lenin Raj Rajasekaran
    Nov 27 '18 at 5:10








1




1





use the index action and then call show if your param is present

– Lenin Raj Rajasekaran
Nov 27 '18 at 5:10





use the index action and then call show if your param is present

– Lenin Raj Rajasekaran
Nov 27 '18 at 5:10












1 Answer
1






active

oldest

votes


















0














There may be many ways to do this and my way is as follow.





Change index action of pages_controller as follow



def index
if params[:id].present?
show
else
#Code of index action
@pages = Page.all
end
end


Change show action of pages_controller as follow



def show
@page = Page.find(params[:id])
end


And pass page id in link_to function as follow



<% @pages.each do |page| %>
<h4><b><%= link_to page.title, pages_path(:id => page.id)%> </b></h4>
#<h4><b><a href="/pages?id=page_number">Page Title</a></b></h4>
<% end %>


I hope this will solve your problem.






share|improve this answer


























  • i want the page to open when i click on the title, not category list, i only want the page to be passed through a params

    – Albert Conrad
    Nov 27 '18 at 10:53











  • @AlbertConrad check my updated answer.

    – Niraj Kaushal
    Nov 27 '18 at 11:05











  • when i click on the page title, it takes me to the same page, how can i make it take me to a different page

    – Albert Conrad
    Nov 27 '18 at 16:29











  • i posted my full controller if it will help you help me

    – Albert Conrad
    Nov 27 '18 at 16:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492943%2fhow-to-pass-parameters-in-page%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














There may be many ways to do this and my way is as follow.





Change index action of pages_controller as follow



def index
if params[:id].present?
show
else
#Code of index action
@pages = Page.all
end
end


Change show action of pages_controller as follow



def show
@page = Page.find(params[:id])
end


And pass page id in link_to function as follow



<% @pages.each do |page| %>
<h4><b><%= link_to page.title, pages_path(:id => page.id)%> </b></h4>
#<h4><b><a href="/pages?id=page_number">Page Title</a></b></h4>
<% end %>


I hope this will solve your problem.






share|improve this answer


























  • i want the page to open when i click on the title, not category list, i only want the page to be passed through a params

    – Albert Conrad
    Nov 27 '18 at 10:53











  • @AlbertConrad check my updated answer.

    – Niraj Kaushal
    Nov 27 '18 at 11:05











  • when i click on the page title, it takes me to the same page, how can i make it take me to a different page

    – Albert Conrad
    Nov 27 '18 at 16:29











  • i posted my full controller if it will help you help me

    – Albert Conrad
    Nov 27 '18 at 16:36
















0














There may be many ways to do this and my way is as follow.





Change index action of pages_controller as follow



def index
if params[:id].present?
show
else
#Code of index action
@pages = Page.all
end
end


Change show action of pages_controller as follow



def show
@page = Page.find(params[:id])
end


And pass page id in link_to function as follow



<% @pages.each do |page| %>
<h4><b><%= link_to page.title, pages_path(:id => page.id)%> </b></h4>
#<h4><b><a href="/pages?id=page_number">Page Title</a></b></h4>
<% end %>


I hope this will solve your problem.






share|improve this answer


























  • i want the page to open when i click on the title, not category list, i only want the page to be passed through a params

    – Albert Conrad
    Nov 27 '18 at 10:53











  • @AlbertConrad check my updated answer.

    – Niraj Kaushal
    Nov 27 '18 at 11:05











  • when i click on the page title, it takes me to the same page, how can i make it take me to a different page

    – Albert Conrad
    Nov 27 '18 at 16:29











  • i posted my full controller if it will help you help me

    – Albert Conrad
    Nov 27 '18 at 16:36














0












0








0







There may be many ways to do this and my way is as follow.





Change index action of pages_controller as follow



def index
if params[:id].present?
show
else
#Code of index action
@pages = Page.all
end
end


Change show action of pages_controller as follow



def show
@page = Page.find(params[:id])
end


And pass page id in link_to function as follow



<% @pages.each do |page| %>
<h4><b><%= link_to page.title, pages_path(:id => page.id)%> </b></h4>
#<h4><b><a href="/pages?id=page_number">Page Title</a></b></h4>
<% end %>


I hope this will solve your problem.






share|improve this answer















There may be many ways to do this and my way is as follow.





Change index action of pages_controller as follow



def index
if params[:id].present?
show
else
#Code of index action
@pages = Page.all
end
end


Change show action of pages_controller as follow



def show
@page = Page.find(params[:id])
end


And pass page id in link_to function as follow



<% @pages.each do |page| %>
<h4><b><%= link_to page.title, pages_path(:id => page.id)%> </b></h4>
#<h4><b><a href="/pages?id=page_number">Page Title</a></b></h4>
<% end %>


I hope this will solve your problem.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 11:04

























answered Nov 27 '18 at 5:32









Niraj KaushalNiraj Kaushal

765511




765511













  • i want the page to open when i click on the title, not category list, i only want the page to be passed through a params

    – Albert Conrad
    Nov 27 '18 at 10:53











  • @AlbertConrad check my updated answer.

    – Niraj Kaushal
    Nov 27 '18 at 11:05











  • when i click on the page title, it takes me to the same page, how can i make it take me to a different page

    – Albert Conrad
    Nov 27 '18 at 16:29











  • i posted my full controller if it will help you help me

    – Albert Conrad
    Nov 27 '18 at 16:36



















  • i want the page to open when i click on the title, not category list, i only want the page to be passed through a params

    – Albert Conrad
    Nov 27 '18 at 10:53











  • @AlbertConrad check my updated answer.

    – Niraj Kaushal
    Nov 27 '18 at 11:05











  • when i click on the page title, it takes me to the same page, how can i make it take me to a different page

    – Albert Conrad
    Nov 27 '18 at 16:29











  • i posted my full controller if it will help you help me

    – Albert Conrad
    Nov 27 '18 at 16:36

















i want the page to open when i click on the title, not category list, i only want the page to be passed through a params

– Albert Conrad
Nov 27 '18 at 10:53





i want the page to open when i click on the title, not category list, i only want the page to be passed through a params

– Albert Conrad
Nov 27 '18 at 10:53













@AlbertConrad check my updated answer.

– Niraj Kaushal
Nov 27 '18 at 11:05





@AlbertConrad check my updated answer.

– Niraj Kaushal
Nov 27 '18 at 11:05













when i click on the page title, it takes me to the same page, how can i make it take me to a different page

– Albert Conrad
Nov 27 '18 at 16:29





when i click on the page title, it takes me to the same page, how can i make it take me to a different page

– Albert Conrad
Nov 27 '18 at 16:29













i posted my full controller if it will help you help me

– Albert Conrad
Nov 27 '18 at 16:36





i posted my full controller if it will help you help me

– Albert Conrad
Nov 27 '18 at 16:36




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492943%2fhow-to-pass-parameters-in-page%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Unable to find Lightning Node

Futebolista