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 %>
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>Page Title</b></h4>
<% end %>
I hope this will solve your problem.
Related
My pages controller has this code:
class PagesController < ApplicationController
def index
#pages = Page.all
end
def new
#page = Page.new
end
def edit
#page = Page.find(params[:id])
end
def create
#page = Page.new(page_params)
#page.save
redirect_to #page
end
def update
#page = Page.find(params[:id])
#page.update(page_params)
redirect_to #page
end
def show
#page = Page.find(params[:id])
end
def destroy
#page = Page.find(params[:id])
#page.destroy
redirect_to pages_path
end
private
def page_params
params.require(:page).permit(:title,:description)
end
end
And in my navbar I have this code:
<% #pages.each do |page| %>
<li?<%= link_to 'page.title', page_path(page)%></li>
<% end %>
The code should generate the titles of each page created, however I get the following error when I run the page:
undefined method `each' for nil:NilClass
I thought I had eliminated this issue by entering the code inside the new function in PagesController. This pages controller is just for additional pages. The navbar also contains links to static pages, so there should always be some value in the navbar.
Has anybody any advice?
Try this:
Add this in your application_controller.rb
#pages = Page.all
and in your navbar
<% #pages.each do |page| %>
<li><%= link_to "#{page.title}", page_path(page)%></li>
<% end %>
It sounds like you don't have any pages in your Page table. Ensure you have populated this table with some pages.
You can also wrap the code in an unless clause to check if #pages is nil.
For example:
<% unless #pages.nil? %>
<% #pages.each do |page| %>
<li?<%= link_to 'page.title', page_path(page)%></li>
<% end %>
<% end %>
I'm trying to display a single scraped statement on a single show page but it's not working.
This is the error I get: (screenshot)
Here's the controller with the show action in it:
class LinksController < ApplicationController
def index
#links = Link.all
end
def show
#link = Link.find(params[:id])
end
def craigslist_scrape
require 'open-uri'
url = "https://losangeles.craigslist.org/search/web"
page = Nokogiri::HTML(open(url))
#craigslist_info = page.css("ul.rows")
#link_info = page.css("li.result-row p.result-info a.result-title.hdrlnk")
#date = page.css("li.result-row p.result-info time.result-date")
#link_info.each_with_index do |link, index|
Link.new(:link_info => link.text, :date => #date[index].text).save
end
end
private
def set_link
#link = Link.find(params[:id])
end
def link_params
params.require(:link).permit(:link_info, :date)
end
Link Model:
class Link < ApplicationRecord
end
Routes:
Rails.application.routes.draw do
root 'links#craigslist_scrape'
resources :links
end
craigslist.html.erb, where I put the show link as well:
<% #link_info.each_with_index do |link, index| %>
<h2><%= "Title of the job: #{link.text}" %></h2>
<p><%= "Date: #{#date[index].text}" %></p>
<h6><%= link_to 'Show', link_path(link) %></h6>
<% end %>
show.html.erb:
<p>
<strong>Link:</strong>
<%= #link.link_info %>
</p>
<p>
<strong>Date:</strong>
<%= #link.date %>
</p>
Attaching the screenshot of craigslist_scrape.html.erb ...
In your craigslist action you create Link by
...
...
Link.new(:link_info => link.text, :date => #date[index].text).save
...
...
So in your show action you can find by link_info and in your show action should be:
def show
#link = Link.find_by(link_info: params[:id])
end
and you need to update code in your craigslist.html.erb
<h6><%= link_to 'Show', link_path(link.text) %></h6>
From your scrrenshot, I understand that in link_path(link) at craigslist.html.erb Line no: 3 you are passing url.
You should pass Id of Link, and if you want to get link from URL then you can do like
Link.find_by_text(params[:id])
If text is column in table.
Link.find_by_link_info(params[:id])
I'm making a blog type site, and I'd like the posts to be displayed newest first(descending order) Ive tried a few different things and haven't been able to figure it out. This is my current code:
post.html.erb
<%= #posts.each do |post|%>
<div>
<p><%= avatar_for(post.user, size: 40) %>
<%= link_to post.username, post.user %></p>
<h2>
<%= link_to post.title, post %>
</h2>
</div>
<% end%>
post controller
class PostsController < ApplicationController
before_action :authenticate_user!
def posts
#posts = Post.all.order("created_at DESC")
end
def new
#post = Post.new
end
def create
#post = current_user.posts.new(post_params)
if #post.save
redirect_to post_path(#post)
else
render :new
end
end
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :body, :all_tags)
end
end
I believe, based on the Guide, that the syntax you're looking for is:
Post.order(created_at: :desc)
See if that works for you.
This should be #posts as you are calling #posts in you post.html.erb to retrieve and display all the posts:
def posts
#posts = Post.all.order("created_at DESC")
end
The method should look like
def posts
# Plural #posts
#posts = Post.all.order(created_at: :desc)
end
Just a small fix, in your posts method you're declaring #post variable but in the ERB file you're calling #posts.
That should be giving you a Nil error. Declare #posts (plural), and then your ERB file with better indentation:
<%= #posts.each do |post| %>
<div>
<p>
<%= avatar_for(post.user, size: 40) %>
<%= link_to post.username, post.user %>
</p>
<h2>
<%= link_to post.title, post %>
</h2>
</div>
<% end%>
In case this question hasn't been answered yet:
It looks like the problem is coming from the name of the method in your PostsController.
You're expecting to have access to #posts from a method called posts, but your file name for the view you're trying to render is post.html.erb. I believe it should be posts.html.erb.
Why not call it index? That's the Rails convention.
As others have said, the query is correct. Hope this helps.
I am building presentation builder, and I don't know how to destroy the chosen presentation presented in the list when I click on the button Remove.
My controllers looks like that:
def create
if logged_in?
presentation = current_user.presentations.create data: params.to_yaml
redirect_to edit_presentation_path(presentation)
end
end
def edit
render action: :new
end
# def destroy
# current_presentation.destroy
# end
def show
render action: :new
end
def list
#presentations = current_user.presentations
end
def update
current_presentation.update_attributes(data: params.to_yaml)
end
def home
if logged_in?
#presentations = current_user.presentations
end
end
My list of created presentations looks like that:
<% #presentations.each do |p| %>
<a > <%= p.id %>
Show
<a class="action"> Remove </a>
</a>
<% end %>
My goal is: to write correct destroy method and create a link Remove that executes this method for a particular presentation.
<%= link_to "Delete", p, method: :delete %>
Something like that should do it.
More here http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html
<%= link_to "Delete", your_destroy_path(p), method: :delete %>
New to rails so I am implementing a basic app.
Text field and a submit button, on clicking the submit button the text to be displayed on the div placed below the submit button of the index page.
Code and things I trying is as follows -
Siteurl - http://localhost:3000/lists/index
list controller code -
class ListsController < ApplicationController
def index
List.all
end
def add
#message = params[:todo_text]
List.create(:title => #message)
redirect_to '/lists/index', :notice => "Notice"
end
end
Model list.rb -
class List < ActiveRecord::Base
attr_accessible :title
end
Views list index.html.erb code -
<div id="search">
<%= form_tag("/lists/add", :method=>"post") do %>
<%= text_field_tag(:todo_text, '',:class=>"formtext" ) %>
<%= submit_tag("Add Message", :class=>"submit") %>
<% end %>
</div>
Now when I submit some text it redirects to the same..but data is not reverting back to the page. Let me know what I am doing wrong and how do i fix it ?
in controller
def index
#lists = List.all
end
in index.html.erb
<% #lists.each do |list| %>
...
<% end %>
try
<%= form_for #list, :url => { :action => "add" } do |f| %>
controller
def index
#lists = List.all
end
def add
#list = List.new(params[:title])
redirect_to #lists, :notice => "Notice"
end