i am a newbie to rail and try to build my first site but face an issue with a link_to in an index page. The link redirect to /recipes.1 instead of /recipes/1.
The show page work when i try /recipes/1.
Index.html.erb
<% provide(:title, "Recipe") %>
<% #recipes.each do |recipe| %>
<%= link_to recipe.label, recipe%>
<%end%>
route.db
get 'recipe' => 'recipes#show'
get 'recipe' => 'recipes#new'
post 'recipe' => 'recipes#create'
resources :users
resources :recipes
recipes_controller.rb
def index
#recipes = Recipe.all
end
def show
#recipe = Recipe.find(params[:id])
end
Remove the following routes from routes.rb:
get 'recipe' => 'recipes#show'
get 'recipe' => 'recipes#new'
post 'recipe' => 'recipes#create'
The above routes are not required since resources :recipes generate all these routes for you.
Hope it helps!
Try this code
<% #recipes.each do |recipe| %>
<%= link_to recipe.label, recipe_url(recipe) %>
<%end%>
Use
<%=link_to recipe.label, recipe_path(recipe)%>
Instead of
<%= link_to recipe.label, recipe%>
you have defined show path twice.
1. in manual defined path
get 'recipe' => 'recipes#show'
2. Through
resources :recipes
If you do rake routes, then you will first option create routes for get method with /recipe url and appends parameter to it which result in /recipes.1 path.
Also rails read routes file in top to bottom approach. As uses first routes for show method.
Related
I have a question about acts_as_taggable_on. I'm making a BBS and want to display link tags. However, my website can display only strings not links. So I want to put link to strings.
I want to link_to each tags#show.
Now some tags are saved in Tag table and this table have these column(id, name, created_count)
This is my code:
post.rb
def save_tags
array = self.check_taggable_word(self.title)
self.tag_list.add(array, parse: true)
end
def tag_lists
tag_lists = self.tag_list
end
def check_taggable_word(text)
ary = Array.new
nm = Natto::MeCab.new
nm.parse(text) do |n|
ary<<n.surface
end
tags = ActsAsTaggableOn::Tag.pluck(:name)
return ary & tags
end
show.html.erb (post)
Tag:
<% #post.save_tags %>
<%= #post.tag_list %>
posts_controller.rb
def show
#post = Post.find(params[:id])
#category = #post.category
end
routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
resources :categories do
resources :posts
end
resources :posts do
resources :comments
end
resources :tags, only: [:index, :show]
end
It is not so sure what you are trying to achieve, and it might help if you post your controller , too.
However, displaying links can be achieved in rails by using the link_to helper
<% = link_to 'text', some_path %>
Your target is to connect the link to your controller action where you handle the 'tags'
The gem you mentioned gives you another example of displaying links in views. This might work for your depending on your naming conventions in your controller.
<% tag_cloud(#tags) do |tag| %>
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => 'css_class' %>
<% end %>
In general: you need to call
#tags = Tags.all
somewhere in your controllers to get all entries for the tags. Than you can loop over tags which each...that can produce you a link of all your tags and if you want to link to tags#show the link must go to an action where you find that specific tag. For example this can be in that action:
#tag = Tag.find(params[:id])
. That's it.
OK i edit once again. TRY:
in you post controller#show you can show all tags for that post like so
#post.tags.each do |tag|
<%= link_to tag.name, tag %>
this requires that your tag has attribute "name"
in your post#index you can do
<%#post.each do |post|%>
<%post.tags.each do |tag|%>
<%= link_to tag.name, tag %><%end%><%end%>
if you just want all tags to be displayed without post etc. TRY:
in your controller posts#index
#tags = Tag.all
<%#tags.each do |tag|%>
<%= link_to tag.name, tag %><%end%>
In your current controller action posts#show you will only find the tags for that #post because all you do is finding the post by id. So you will only find tags for that post if the association is correct.
That allows you to do #post.tags.each do |tag|...etc.
tag than can be used for the link_to helper.
If you use
<%= link_to tag.name, tag %>
it will show you a link with the tag.name that links to that specific tag for each tag that belongs to the post.
This error is creeping in on more than one occasion and i can't seem to pin point it.
error log:
undefined method `medium_path' for #<#<Class:0x0000010687a788>:0x00000101d62d90>
Extracted source (around line #3):
media controller.
class MediaController < ApplicationController
def index
#medias = Media.all
end
def show
#media = Media.find(params[:id])
end
def edit
#media = Media.find(params[:id])
end
end
edit.html.erb.
<h1>Editing <%= #media.title %></h1>
<%= form_for(#media) do |f| %>
<p>
<%= f.label :title %>
</p>
<% end %>
routes.rb
Mediastuff::Application.routes.draw do
root "media#index"
get "media" => "media#index"
get "media/:id" => "media#show", as: "show_media"
get "media/:id/edit" => "media#edit", as: "edit_media"
end
I believe that error is generated from your form_for declaration. In addition to what you already have in your config/routes.rb, you may also want to add a route for update action as that form_for(#media) is going to be an update.
Add the following to your config/routes.rb:
put "media/:id/update" => "media#update"
Also make sure to define update action in your MediaController.
Another option would be to use resources in config/routes.rb as a replacement to all the media/... routes you have:
Mediastuff::Application.routes.draw do
root "media#index"
resources :media
end
And to see what path/url helpers you can use, run rake routes from terminal.
I created a form to create articles, but I can only see the published articles on the index page, and, I'm unable to see individual articles.
Every time I click on 'show' for an individual article on the index, it opens a blank page with a URL like localhost:3000/articles.1, instead of localhost:3000/articles/1.
I don't know what's wrong with the code, it's similar to the code I have for creating and editing articles as admin and it works there, but I keep getting this error.
Here's the code:
views/articles/show.html.erb:
<h1><%= #article.name %></h1>
<%= #article.content %>
<%= image_tag #article.photo.url(:small) %>
<p>Tags: <%= raw article.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %></p>
<%= link_to 'Back', noticias_path %>
views/articles/index.html.erb:
<% #articles.each do |article| %>
<div>
<h2><%= article.name %></h2>
<%= article.content %>
<p><%= link_to 'Show', noticias_path(article) %></p>
<p>Tags: <%= raw article.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %></p>
</div>
<% end %>
articles_controller.rb:
# GET /articles/1
# GET /articles/1.json
def show
#article = Article.find(params[:id])
end
routes.rb:
Blog::Application.routes.draw do
root to: 'welcome#index'
get 'tags/:tag', to: 'noticias#index', as: :tag
get "noticias/index"
get "dashboard/index"
get "/sitemap" => "sitemap#index", :as => :sitemap, :defaults => {:format => :xml}
match '/noticias', to: 'noticias#index', via: 'get'
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
resources :users
resources :sessions
namespace :admin do
get '', to: 'dashboard#index', as: '/'
resources :noticias
end
end
It's more a problem with noticias_path, consider verify your routes with rake routes, but I think to change noticias_path to noticia_path, may can fixed it.
Based on your routes file, it looks like the problem is indeed incorrect routing. Presently, you are manually creating routes for RESTful resources, when you should simply let rails handle that for you properly.
Remove the manual get and match lines:
get "noticias/index"
match '/noticias', to: 'noticias#index', via: 'get'
And replace them with this:
resources :noticias
If noticias should be pointing to the ArticlesController (or any other controller), then simply do this:
resources :noticias, to: 'articles'
Also, Matheus Caceres was correct that the URL helper should in fact be noticia_path for the show action. The noticias_path points to the index action. Rails tries to be "helpful" by making routes, helpers, functions, etc, sound proper if read in English, but might not necessarily make sense for words in another language. I don't know if "noticia" makes any sense in Portuguese though.
Another quick side note, the two manual routing lines I indicated above are redundant; they would in fact both match a GET request and route them to noticias#index. However, keep in mind that routes are matched in the order they appear in the routing file, so the match line would never have been called, since the route would have matched on that get line.
Noob here.
Trying to figure out how to display a method in my controller into my index page. Here is what I have thus far.
Controller -
class SammichesController < ApplicationController
def index
#sammiches = Sammich.all
end
def create
#sammich = Sammich.find_by_name(params[:sammich][:name])
end
def random
#sammichy = Sammich.rand(params[:sammich][:name])
end
end
Routes-
Sammiches::Application.routes.draw do
resources :sammiches do
get "random"
end
root :to => 'sammiches#index'
Index-
<h1>All my Sammiches</h1>
<%= form_for Sammich.new do |f| %>
<%= f.label :sammich %>
<%= f.text_field :name %>
<%= f.submit 'Find Sammich', :id => 'sammich_submit' %>
<% end %>
<%= link_to "Random sandwich", sammich_random_path %>
routes-
sammich_random GET /sammiches/:sammich_id/random(.:format) sammiches#random
sammiches GET /sammiches(.:format) sammiches#index
POST /sammiches(.:format) sammiches#create
new_sammich GET /sammiches/new(.:format) sammiches#new
edit_sammich GET /sammiches/:id/edit(.:format) sammiches#edit
sammich GET /sammiches/:id(.:format) sammiches#show
PUT /sammiches/:id(.:format) sammiches#update
DELETE /sammiches/:id(.:format) sammiches#destroy
root / sammiches#index
Error-
localhost:3000 - Routing Error
No route matches {:action=>"random", :controller=>"sammiches"}
Try running rake routes for more information on available routes.
Any assistance would be much appreciated.
Thanks!
If you look at your route it has :sammic_id in there as well:
sammich_random GET /sammiches/:sammich_id/random(.:format) sammiches#random
Which means you need to pass an id to your URL helper sammich_random_path which you haven't.
Update your routes to this:
resources :sammiches do
collection do
get "random"
end
end
After adding that your route would be just /sammiches/random
I really have no clue why it happen.
this is my routes
resources :users do
resources :bookmarks
end
Controller
# bookmarks_controller
def edit
# this returns perfectly data in the edit view
#bookmark = current_user.bookmarks.find(params[:id])
end
This is the view
<%= form_for #bookmark do |b| %>
<% end %>
Since everything messed up when I nested the bookmarks resources inside users it causes the error
undefined method `bookmark_path'
Then I change the form to
<%= form_for user_bookmark_path(current_user, #bookmark) do |b| %>
The error is gone but there is no such data in the text field form, and the form action is /users/[user_id]/bookmarks/[bookmark_id]/edit
rake routes info
user_bookmarks GET /users/:user_id/bookmarks(.:format) bookmarks#index
POST /users/:user_id/bookmarks(.:format) bookmarks#create
new_user_bookmark GET /users/:user_id/bookmarks/new(.:format) bookmarks#new
edit_user_bookmark GET /users/:user_id/bookmarks/:id/edit(.:format) bookmarks#edit
user_bookmark GET /users/:user_id/bookmarks/:id(.:format) bookmarks#show
PUT /users/:user_id/bookmarks/:id(.:format) bookmarks#update
DELETE /users/:user_id/bookmarks/:id(.:format) bookmarks#destroy
Any idea how to fix it ? Thanks
Try something like:
<%= form_for [current_user, #bookmark] do |b| %>
<% end %>