I know this question gets asked a lot
Rails dot instead of slash in URL
link_to delete url is not working
Path helpers generate paths with dots instead of slashes
Just to show a few of them.
In all of them, a pluralization error seems to be the culprit. Yet when I try those solutions in my code, the issue still persists, which is why I am submitting this question.
So, in my main_pages_controller
def console
@article_list = Article.all
end
In routes
resources :articles, only: [:create, :destroy, :edit]
In console.html.erb
<% if (@article_list.any?) %>
<% @article_list.each do |i| %>
<tr>
<td><%= i.title %></td>
<td><%= i.category %></td>
<td><%= i.user.name %></td>
<td><%= link_to "destroy", i, method: :delete,
data: { confirm: "You sure?" } %></td>
<td>Edit</td>
</tr>
<% end %>
<% end %>
This results in articles.X where x is the id of i.
If i replace i with articles_path(i), I get the same articles.x
If I replace i with article_path(i), I get article.x
If i replace i with articles_path(i.id) or article_path(i.id), I still get article(s).X
And if it matters, this is in articles_controller
def destroy
Article.find(params[:id]).destroy
redirect_to '/console'
end
What works to delete the articles is
<td><a href="/articles/<%= i.id %>" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a></td>
But I would like to understand what is happening so I can learn and be able to use link_to properly in the future.
Edit
So the above was for my article resource. With my user resource it works
Routes
resources :users, only: [:create, :destroy, :edit]
main_pages_controller
def console
@user_list = User.all
end
console.html.erb
<% if (@user_list.any?) %>
<% @user_list.each do |i| %>
<tr>
<td><%= i.name %></td>
<td><%= i.email %></td>
<td><%= link_to "destroy", i, method: :delete,
data: { confirm: "You sure?" } %></td>
<td>Edit</td>
</tr>
<% end %>
<% end %>
This is all copy and pasted from the article code, yet this gives me /users/X. So for comparison sake, users works, articles give me a dot.
EDIT
Since it was asked
Rails.application.routes.draw do
get 'sessions/new'
get 'users/new'
root 'main_pages#home'
get 'home' => 'main_pages#home'
get 'author' => 'main_pages#author'
get 'article' => 'main_pages#article'
get 'console' => 'main_pages#console'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :articles, only: [:create, :destroy, :edit]
resources :users, only: [:create, :destroy, :edit]
end