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])
Related
I am using nested resources. When I click on a goal that's been created, I keep getting this error:
param is missing or the value is empty: goal
And it directs me to the "params.require..." line:
private
def goal_params
params.require(:goal).permit(:text)
end
I'm not sure what's causing this. I can create and show the list. But when I click on a goal I get this error. I'm new to rails and I'm at my wit's end.
My view:
<h1>Listing goals</h1>
<table>
<tr>
<th>Text</th>
<th></th>
</tr>
<% #user.goals.each do |goal| %>
<tr>
<td><%= link_to goal.text, user_goals_path(#user, #goal)%></td>
</tr>
<% end %>
</table>
<h2>Add a comment:</h2>
<%= form_for([#user, #user.goals.build]) do |form| %>
<p>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
My controller:
class GoalsController < ApplicationController
def index
#user = User.find(params[:user_id])
#goal = #user.goals.find(goal_params)
end
def show
#user = User.find(params[:user_id])
#goal = #user.goals.find(:id)
#also tried goal_params and :goal_id instead of :id
end
def new
#user = User.find(params[:user_id])
#goal = #user.goals.new
end
def create
#user = User.find(params[:user_id])
#goal = #user.goals.build(goal_params)
#goal.user = current_user
if #goal.save
redirect_to new_user_goal_path, notice: "Success!~"
else
redirect_to new_user_goal_path, alert: "Failure!"
end
#to root_path
end
private
def goal_params
params.require(:goal).permit(:text)
end
end
My routes:
Rails.application.routes.draw do
devise_for :users
resources :user do
resources :goals
end
devise_scope :user do
authenticated :user do
root 'home#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
end
My show.html.erb:
<p>
<strong>Text:</strong>
<%= #goal.text %>
</p>
First thing, in the show
#goal = #user.goals.find(:id)
Should be
#goal = #user.goals.find(params[:id])
You said you tried with #user.goals.find(goal_params) in show action and I see it in your index action also. This will call the goal_params method, which require params[:goal] while your index or show request does not send to server, only when you submit the form, you will have that params. This is the cause of your error.
Second thing, your index should use
#goals = #user.goals
INSTEAD OF
#goal = #user.goals.find(goal_params)
Also, the strong parameters is used for create and update actions only to avoid mass assignment to our database. It's not used to find a record.
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.
im making a twitter clone and trying to make it so the users username appears next to their tweet.
Ive made it work through adding a user and a tweet in the seed file, hoever when i add a create,new method and a form it comes up with the error "Couldn't find User without an ID" and highlighting the first line of my create method. not sure what the issue is, thanks.
class TweetsController < ApplicationController
before_action :authenticate_user!, :except => [:index, :new, :create]
def index
#tweets = Tweet.all.order("created_at DESC")
#tweet = Tweet.new
end
def show
#tweet = Tweet.find(params[:id])
end
def new
# #tweet = Tweet.new
end
def create
#user = User.find(params[:id])
#tweet = Tweet.new(tweet_params)
#tweet.user = #user
if #tweet.save
redirect_to tweets_path
end
end
def edit
#tweet = Tweet.find(params[:id])
end
def update
#tweet = Tweet.find(params[:id])
#tweet.update(tweet_params)
redirect_to tweets_path
end
private
def tweet_params
params.require(:tweet).permit(:user_id,:content)
end
end
<h1>TWEETS</h1>
<%= simple_form_for [#user,#tweet], id: "form-submit" do |f| %>
<%= f.input :content, label: "Tweet" %>
<%= f.input :user %>
<%= f.button :submit, class: "btn btn-danger" %>
<% end %>
<br>
<% #tweets.each do |tweet| %>
<ul>
<li>
<%= tweet.created_at.strftime("%B %d %Y, %l:%M%P") %> <br>
<%= tweet.content %>
<%= tweet.user.username %>
</li>
</ul>
<% end %>
You need to define #user in a variable in your index method.
Any variable you use in the form needs to be declared somewhere, either in the helper, controller, or view. Rails convention is to declare them in the controller normally.
I would need to see your config/routes.rb file for the error message you are getting in the image, but if you type rails routes at the command line, you can see a list of all available routes, when you use:
simple_form_for [#user, #tweet]
Rails will interpret [#user, #tweet] as user_tweets_path, and try to submit the form to this path. That path is defined in your config/routes.rb file.
The error is telling you that you have not defined this path in the routes file. To define this path you can add this line to your routes file:
resources :users do
resources :tweets
end
I am working on my first Rails project and I am running into a persistent issue. I suspect it has something to do with the routing, however, I can't seem to find anything about it online.
I assume it a rather simple fix, so please take a look and let me know if you can help.
TL;DR
What I was trying to achieve
Account detail Cards display Name, Phone number, and a note.
A delete and edit button would allow users to delete or edit.
What is happening:
Edit and Delete buttons return a weird param.
see image
Image of error, Showing Rails getting a different ID
Controller
class AccountdetailsController < ApplicationController
def index
#accountdetail = Accountdetail.all
end
#I can't find the ID to show the relevent card.
def show
#accountdetail = Accountdetail.find(params[:id])
if #accountdetail.nil?
redirect_to accountdetail_path
end
end
def new
#accountdetail = Accountdetail.new
end
def edit
#accountdetail = Accountdetail.find(params[:id])
end
def create
#accountdetail = Accountdetail.new(accountdetail_params)
if #accountdetail.save
redirect_to #accountdetail
else
render 'new'
end
end
#it affects this
def update
#accountdetail = Accountdetail.find(params[:id])
if #accountdetail.update(accountdetail_params)
redirect_to accountdetail
else
render 'edit'
end
end
#and this
def destroy
#accountdetail = Accountdetail.find(params[:id])
#accountdetail.destroy
redirect_to accountdetail_path
end
private def accountdetail_params
params.require(:accountdetail).permit(:first_name, :last_name, :phone, :notes, :id)
end
end
Index.HTML.ERB
<div class="ui card">
<div class="content">
<a class="header"><%= account.first_name %> <%= account.last_name %> </a>
<div class="meta">
<span class="date"><%= account.phone %></span>
<strong><p><%= account.notes %></p></strong> <br>
<%= link_to "edit", edit_accountdetail_path(#accountdetail) %>
<%= link_to 'Inspect', accountdetail_path(#accountdetail) %>
</div>
</div>
</div>
<% end %>
Routes
Rails.application.routes.draw do
get 'welcome/index'
resources :articles do
resources :comments
end
resources :accountdetails
root 'welcome#index'
end
In you index.html.erb replace following
<%= link_to "edit", edit_accountdetail_path(#accountdetail) %>
<%= link_to 'Inspect', accountdetail_path(#accountdetail) %>
with
<%= link_to "edit", edit_accountdetail_path(account) %>
<%= link_to 'Inspect', accountdetail_path(account) %>
#accountdetail was providing you all the records of account, as it was firing select query in controller. But here we need only one instance, so account.
Hope this helps.
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