how to pass parameters in page
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
add a comment |
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
1
use theindexaction and then callshowif your param is present
– Lenin Raj Rajasekaran
Nov 27 '18 at 5:10
add a comment |
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
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
ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5
edited Nov 27 '18 at 16:35
Albert Conrad
asked Nov 27 '18 at 4:51
Albert ConradAlbert Conrad
548
548
1
use theindexaction and then callshowif your param is present
– Lenin Raj Rajasekaran
Nov 27 '18 at 5:10
add a comment |
1
use theindexaction and then callshowif 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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
use the
indexaction and then callshowif your param is present– Lenin Raj Rajasekaran
Nov 27 '18 at 5:10