how to show article's content from another view - ruby-on-rails

I was making a blog and I've got problem when I want to show an article
I've generate scaffold article title content:text and I just generate new controller called welcome with a view called homepage. I created new controller+view just to show articles. for this part I did not find the problem and then I created a new controller called post with a view called show is intended only to show the contents of the selected articles from the homepage.
how to show article's content from another view ?
I just added #article = Article.find(params[:id]) to post_controller
and then when I click an article's title in homepage I got error like this
Couldn't find Article with 'id'=
did I miss some code?
so this is my welcome_controller
class WelcomeController < ApplicationController
def homepage
#articles = Article.all
end
end
welcome/homepage.html.erb
<div class="post-preview">
<% #articles.each do |article|%>
<h2 class="post-title"><%= link_to article.title, welcome_show_path %></h2>
<%= truncate article.content, length: 160 %>
<hr>
<% end %>
</div>
post/show.html.erb
<div class="post-heading">
<h1><%= #article.title %></h1>
</div>
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<p><%= #article.content %></p>
</div>
post_controller
class PostController < ApplicationController
def show
#article = Article.find(params[:id])
end
end
routes.rb
Rails.application.routes.draw do
resources :articles
get 'welcome/homepage'
get 'post', to: 'post#show'
root 'welcome#homepage'
end
thanks :)

The problem seems to be in your link:
<h2 class="post-title"><%= link_to article.title, welcome_show_path %></h2>
welcome_show_path needs an id. Try welcome_show_path(id: article.id).
<h2 class="post-title"><%= link_to article.title, welcome_show_path(id: article.id) %></h2>
If that doesn't work, try: post_show_path(id: article.id).

You have 3 Controllers:
Article => generated by scaffolding, it means with predefined routes.
Welcome => only one method, called homepage
Post => only one method, called show.
Routes:
resources :articles ##generate default routes
get 'welcome/homepage' ##generate only one route, URL would be -> homepage_welcome_path.
get 'post', to: 'post#show' ##it will call show method without any parameter.
root 'welcome#homepage'
First:
get 'photos/:id', to: 'photos#show'
In your homepage.html.erb
<h2 class="post-title"><%= link_to article.title, welcome_show_path %></h2>
welcome_show_path ## expect show method in welcome controller, which is not exist.
Second:
To call show method of articles, you must pass ID of that article.
get 'articles/:id', to: 'articles#show' ##This route is already defined as you have `resource articles`. URL would be articles_path for the same.
Replace in homepage.html.erb
<h2 class="post-title"><%= link_to article.title, articles_path(id: article.id) %></h2>

Related

Displaying search bar query results from an API in Rails

I am trying to create a simple app where you find games from an API via a search bar. I am using the Giant Bomb API gem (https://github.com/games-directory/api-giantbomb). The gem seems to be working since I can call things via the console. However, I am not quite sure how to display results or if there are problems with my search method (likely).
Here is my controller for Games:
class GamesController < ApplicationController
//Display search results
def index
#games = Game.all.order('created_at DESC')
#games = #games.search(params[:query]) if params[:query].present?
end
//Searches through API and redirects to results on index.
def search
#games = GiantBomb::Search.new().query(params[:query]).resources('game').limit(5).fetch
redirect_to index_path
end
private
def game_params
params.require(:game).permit(:name)
end
end
My search bar code on my home page:
<div class="search-font"><h1>Build your Collection</h1></div>
<div class="container">
<div class="row">
<div class="col-md-12">
<%= form_tag search_path class: "row", method: :get do %>
<div class="col-12 col-sm pr-sm-0">
<%= text_field_tag :game,
params[:game],
class: "form-control input-lg",
id: "typed-text" %>
</div>
<div class="input-group-btn ml-3">
<%= submit_tag "Search", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
And my index.html.erb where it is supposed to spit out the game names.
<ul>
<% #games.each do |game| %>
<li><%= game.name %></li>
<% end %>
</ul>
The search bar redirects but nothing is posted. In my console I can do search.query('Mario') then search.fetch to print out the results, but I am not sure how to use this function in my controller correctly.
[edit] Here are my routes as well.
Rails.application.routes.draw do
devise_for :users
resources :games
root to: 'pages#home'
get '/search', to: 'games#search', as: :search
get '/games', to: 'games#index', as: :index
end
redirect_to creates a new request, you need to use render as in...
render 'index'
That way you can use your #games variable in the view

Rails how to pass id to custom controller

I am new to rails and trying to figure out how to pass id from view to controller. I have create below routes.rb file i didnt want resources here just to have better understanding of how to pass params
Rails.application.routes.draw do
get 'sites/edit/:id', to: 'sites#edit'
get 'sites/main'
devise_for :users
root 'sites#main'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
below is my controller
class SitesController < ApplicationController
def main
#site = Site.all
end
def edit
#site = Site.find(params[:id])
end
private
def site_params
params.require(:site).permit(:title, :subtitle,:name)
end
end
and i have two views for my site controller. In main.html.erb
<h1>Sites#main</h1>
<p><%= current_user.email %> user is signned in </p>
<%= #site.each do |temp| %>
<p><%= temp.name %></p>
<p></p>
<p></p>
<%= link_to "Edit Site", sites_edit_path(#temp) %>
<% end %>
I am not able to link it to correct controller.
First: your edit routes should not be
get 'sites/edit/:id', to: 'sites#edit'
Instead it should be get 'sites/:id/edit' => 'sites#edit'
Second: your link to method should not be
<%= link_to "Edit Site", sites_edit_path(#temp) %>
instead it should be <%= link_to "Edit Site", sites_edit_path(temp) %>
You can get to know about your routes from terminal by doing rake routes or If you want a browser view then
http://localhost:3000/rails/info/routes
You need to replace
<%= link_to "Edit Site", sites_edit_path(#temp) %>
to
<%= link_to "Edit Site", sites_edit_path(temp) %>
You have the variable temp not #temp
Also, As sarcastic suggested you need to change the route entry to
get 'sites/edit/:id', to: 'sites#edit', as: :sites_edit

How to use one layout by two different actions in rails 4

I'm building a micropost rails app where users can create posts, I created a route and an action to display posts that belong to the signed-in user, happens that the general index layout for posts is exactly the same as the "myposts" layout so instead of duplicating code I would like to use just one layout with different parameters.
This is what I have until now:
routes.rb
resources :posts
get '/myposts', to: 'posts#my_posts', as: 'myposts'
posts_controller.rb
def index
#posts = Post.all
end
def my_posts
#myposts= Post.where(user_id: current_user.id)
end
index.html.erb
<% #posts.each do |post| %>
<div>
<h1><%= link_to post.title, post %></h1>
<%= link_to image_tag(post.meme_url(:thumb)), post, :target => "_blank" %>
</div>
<% end %>
my_posts.html.erb
<% #myposts.each do |post| %>
<div>
<h1><%= link_to post.title, post %></h1>
<%= link_to image_tag(post.meme_url(:thumb)), post, :target => "_blank" %>
</div>
<% end %>
Thanks in advance!
You can use 'render' on 'my_posts' action - http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
Add before 'end' in my_posts action:
render :index

Rails 4, params not received by controller

I'm trying to setup a post editing page, with links to edit and delete posts. I'm having troubles because my edit controller somehow doesn't seem to receive the parameters I'm sending. I've printed them out on the page to make sure they're there, and it displays correctly, but when I click the editlink I get ActiveRecord::RecordNotFound Couldn't find Article with 'id'= .
here's the view with the link: (article.id display correctly on this one)
<div id= "articles-index">
<div class = "row">
<% #article.each do |article| %>
<div class="row">
<div class = "container">
<p><%= link_to article.title, article %> | <%= article.updated_at.strftime("%e. %b %Y, %H:%M") %> | <%= link_to 'edit', articles_update_path(article.id) %> | delete</p>
<h2><%= article.id %></h2>
</div>
</div>
<% end %>
</div>
</div>
articles controller:(the ... is where I edited more code out as it's irrelevant to this question)
class ArticlesController < ApplicationController
...
def update
#article = Article.find(params[:id])
end
def manage
#article = current_user.articles
#article = current_user.articles.order('id DESC')
end
...
def article_params
params.require(:article).permit(:id, :title, :body, :user_id)
end
end
routes:
get 'articles/edit'
get 'articles/destroy'
get 'articles/manage'
get 'articles/show'
get 'articles/index'
get 'articles/new'
get 'articles/update'
get 'articles/search'
get 'articles/create'
get 'sessions/destroy'
get 'sessions/new'
get 'users/new'
resources :users
resources :sessions
resources :articles
GitHub Link
Clicking on a link instigates a GET request. I'm guessing the update action on your controller will be expecting the params to be a POST request.
The edit action normally handles GET.
The update action receives the POST request.
I was trying to find the DHH 2007 Keynote at Railsconf that covers this really well. If you can find it, watch it.
See CRUD Verbs and Actions at Rails Guides
UPDATE: Just seen your routes file. If it's a GET request then just put the URL directly into the browser. e.g.
articles/update?id=123
See if params[:id] == 123 in the controller. It should be.
UPDATE 2
articles_update_path(id: article.id)
Will generate a params has with an :id key but fundamentally you shouldn't be calling update actions with a GET route.

Why am I seeing "No route matches :action=>"show"" when my show action is defined for that controller?

I have a "Category" resource, and am trying to link to Categories#show in its index.html.erb file using the link_to method and the _path routes. When I view the index in my browser I see the following error:
Routing Error No route matches {:action=>"show", :controller=>"categories"}
But I do have a show action defined in my Categories controller, and removing the URI part of link_to causes the index to display normally without having an exception thrown! I can't figure out where I'm going wrong with the URI.
Here's /categories/index.html.erb:
<h1>My Links</h1>
<% #categories.each do |c| %>
<h2><%= link_to "#{c.category}", category_path %></h2>
<p><%= c.description %></p>
<% end %>
<%= link_to "Add new category", new_category_path %>
Here's /categories/show.html.erb:
<h1><%= #category.category %></h1>
<p><%= #category.description %></p>
<p><%= link_to "Back to index", root_path %></p>
Here's /config/routes.rb:
LinkManager::Application.routes.draw do
resources :categories do
resources :links
end
root :to => 'categories#index'
Here's part of /controllers/categories_controller.rb:
class CategoriesController < ApplicationController
def index
respond_with(#categories = Category.all)
end
def show
#category = Category.find(params[:id])
end
And here's the result of running rake routes (put it on pastebin since I couldn't figure out how to format it nicely here): rake routes
Can anyone spot what's wrong? I've looked at many other similar routing questions here but couldn't get a solution from them. Thank you.
Try to replace:
<h2><%= link_to "#{c.category}", category_path %></h2>
with:
<h2><%= link_to "#{c.category}", category_path(c) %></h2>
See? You have to provide a category for category_path.
Or just use the magic:
<h2><%= link_to "#{c.category}", c %></h2>
And by the way, what's the purpose of interpolation here: "#{c.category}"? Wouldn't simply c.category be enough?
Well, i think if you add other route to action show it works, do this:
do this in your browser:
localhost:3000/categories/show
and in your routes.rb:
match "/categories/show/:id" => categories#show
what is the version of our ruby and the version of rails?

Resources