Polymorphic Paths for Custom Routes? - ruby-on-rails

<%= collection.each do |record| %>
<%= link_to record.name, polymorphic_path([:admin, record]) %>
<% end %>
Breaks with this route:
resources :users, as: "authors", path: "authors", except: [:create, :destroy]
Error:
undefined method `admin_user_path'
Can anyone help?
I've got it as a Rails bug

You should also define users resource inside admin namespace
namespace :admin do
resources :users
end

Related

Rails custom action not working due to show, how to fix?

I'm trying to create a customer action in my campaigns controller called building.
http://localhost:3000/campaigns/building
Shows error:
Showing .../app/views/campaigns/show.html.erb where line #1 raised:
undefined method `name' for nil:NilClass
<h2><%= #campaign.name %></h2>
<p>
Created by: <%= #campaign.user.email %> <%= time_ago_in_words(#campaign.created_at) %> ago.
</p>
<h5>Website: <%= #campaign.website %></h5>
My routes file:
resources :campaigns do
resources :targets
end
get "campaigns/building" => "campaigns#building", :as => :campaigns_building
Controller:
class CampaignsController < ApplicationController
before_action :find_campaign, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def show
end
def building
end
...
My show.html.erb:
<h2><%= #campaign.name %></h2>
<p>
Created by: <%= #campaign.user.email %> <%= time_ago_in_words(#campaign.created_at) %> ago.
</p>
<h5>Website: <%= #campaign.website %></h5>
How do I make it not give this error?
It seems you are trying to make a custom route and unable to make it work. If so, to make it work, you should move your custom route above resources to avoid conflicts with the default resourceful route and correctly route to building action
get "campaigns/building" => "campaigns#building", :as => :campaigns_building
resources :campaigns do
resources :targets
end
This is because Rails tries to match routes starting from the top down. You can also define it as a collection route on the resources
resources :campaigns do
get "building", on: :collection
resources :targets
end

No route matches {:action=>"show", :controller=>"report"

I have a report#show view which I would to link_to, but I'm unsure about how to set up the routing.
In my packages#show view:
<% link_to 'Report', package_report_path(#package) %>
Here's my routes.rb:
Rails.application.routes.draw do
devise_for :users
resources :packages do
resources :sales, only: [:new]
resources :report, only: [:show]
end
root "packages#index"
end
If I do rake routes:
package_report GET /packages/:package_id/report/:id(.:format) report#show
The route is set up correct, but you need to pass both #package and #report to the package_report_path, like:
<% link_to 'Report', package_report_path(#package, #report) %>
Your report resource is nested under the package resource. So, you have to pass both #package and #report to the package_report_path helper method:
<% link_to 'Report', package_report_path(#package, #report) %>

No route matches [PUT] but I included "resources" in routes.rb

I'm coding a blog that has two types of articles: "drafts" and "published". I'm using the aasm gem for making the article transition from draft to published or viceverza. There are also three types of users: "regular readers", "editors" and "admins".
As users write articles, admins can evaluate whether to publish them or not. To accomplish this, admins have a view in which they can see both drafts and published articles.
The problem is that when I try to publish the articles I get the No route matches [PUT] "/articles" error, regardless I've added resources :articles in routes.rb.
The code I wrote is the following:
routes.rb
resources :categories
resources :articles do
resources :comments, only: [:create, :update, :destroy, :show]
end
devise_for :users
root 'welcome#index'
get '/dashboard', to: 'welcome#dashboard'
put '/articles/:id/publish', to: 'articles#publish'
articles_controller.rb
...
def publish
#article.publish! # Article transition from draft to published.
redirect_to #article
end
...
dashboard.html.erb
...
<% #articles.each do |art| %>
<h1><%= link_to art.title, art, method: :get %> | id = <%= art.id %> | user_id = <%= art.user_id %></h1>
<div>
<%= art.body %> - <%= link_to "Eliminar", art, method: :delete %>
<% if art.may_publish? %>
- <%= link_to "Publicar", '/articles/#{article.id}/publish' , method: :put %>
<% end %>
</div>
<% end %>
...
I can't see why I get this error if I included the article resource. If you need me to include more code don't hesitate to ask me.
Thanks in advanced.
You should remove your custom routes and put it into resources :articles like this :
routes.rb
resources :articles do
put 'publish', on: :member
resources :comments, only: [:create, :update, :destroy, :show]
end
and you should use this in your view :
<%= button_to "Publicar", publish_article_path(article), method: :put %>
It will generate a form.
The code seems to be correct, so try to reset the server and, by the way, restart the browser. It usually solves strange problems like this :) Let me know if it worked.

undefined method `post_up_vote_path'

I am trying to come up with upvote/downvote method in my app and I running into a no method error.
Here is my voter form:
<div>
<div class= 'pull-left'>
<div><%= link_to " ", post_up_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
<div><strong><%= post.points %></strong></div>
<div><%= link_to " ", post_down_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
</div>
Here are my routes:
App::Application.routes.draw do
devise_for :users
resources :users
resources :topics do
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post '/up-vote' => 'votes#post_up_vote', as: :up_vote
post '/down-vote' => 'votes#post_down_vote', as: :down_vote
end
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
And here is my partial call:
<%= render partial: 'votes/voter', locals: { post: post } %>
Now I don't think there is anything wrong with my partial call or my voter partial because everything works until I try to route it.
First, you will need to change up your routes a bit.
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post 'upvote', on: :member
post 'downvote', on: :member
end
The reason these are member routes instead of collection routes is being they apply to a specific member of the collection of posts...a single Post.
Then, you will want to run the command, rake routes in your Terminal to see what the appropriate path methods are. It should be something like
upvote_post_path(:id)
which requires an object be passed in..so in your view you would use it like you currently are.
upvote_post_path(post)

Rails route not finding corresponding action

Inside app/views/participants/index.html.erb:
<%= form_tag bulk_add_participants_program_path do %>
<%= wrap_control_group do %>
<%= text_area_tag :bulk_add_participants, :size => "60x3" %>
<% end %>
<%= submit_tag "Import Participants and Users" %>
<% end %>
But notice that the controller and route pertain to the Program model (for good UI reasons). And I think that might be related to the problem. When I render that view I get this error message:
No route matches {:action=>"bulk_add_participants", :controller=>"programs"}
Which is weird because in app/controllers/programs_controller.rb:
def bulk_add_participants
puts "yay!" # because i am troubleshooting
end
And my config/Routes.rb is:
RepSurv::Application.routes.draw do
root to: 'programs#index'
devise_for :users, path_prefix: 'devise'
resources :users
resources :programs do
resources :participants do
resources :rounds do
get 'survey' => 'rounds#present_survey'
put 'survey' => 'rounds#store_survey'
end
end
resources :questions
resources :rounds
member do
get 'report' => 'reports#report'
get 'bulk_add_participants'
end
end
end
It's not finding the route because you have programs defined as a plural resource:
resources :programs do
When you do that and reference a member route like your bulk_add_participants, it expects a :program_id parameter in your case. (Try running rake routes, and you'll see a path like /programs/:program_id/bulk_add_participants.)
So your form_tag call should perhaps look like this:
<%= form_tag bulk_add_participants_program_path(#program) do %>

Resources