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) %>
Related
I got an error
No route matches [POST] "/article/1/like"
My articles_controller.rb is.
def like
#article = article.all.find(params[:id])
Like.create(user_id: current_user.id, article_id: #article.id)
redirect_to articles_path(#article)
end
This is my index page.
<% if article.liked?(current_user) %>
<%= button_to "like", like_path(article), methode:"put", desabled: true %>
<% else %>
<%= button_to "like", like_path(article), methode:"put" %>
<% end %>
and routes.rb is
Rails.application.routes.draw do
get 'static_pages/landing_page'
get 'static_pages/dashboard'
devise_for :users
resources :users
resources :articles do
resources :comments
end
put '/article/:id/like', to: 'article#like', as: 'like'
root "articles#index"
end
I am writing this code from a website given below.
enter link description here
The idomatically correct way to add additional actions to a resource is:
resources :articles do
put :like
resources :comments
end
This creates the route /articles/:article_id/like(.:format) - not that articles is plural.
<%= button_to "like", article_like_path(article), method: :put, disabled: article.liked?(current_user) %>
def like
#article = article.all.find(params[:id])
Like.create(user_id: current_user.id, article_id: #article.id)
redirect_to articles_path(#article)
end
in like method use params[:article_id] instead of params[:id]
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.
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)
I want to a searching function for users. It does not work. So I simplified the method, I just want to refresh the index page when I hit the search button. But it still does not work, it said
ActiveRecord::RecordNotFound in UsersController#show, Couldn't find User with id=search.
please tell me Why
my controller
class UsersController < ApplicationController
load_and_authorize_resource :except => [:index]
def search
redirect_to users_path
end
end
My view
<%= form_tag users_search_path, :method => 'get' do %>
<td><%= text_field_tag :username, params[:username] %></td>
<%= submit_tag "Search", :class => "buttons buttons-rounded buttons-flat-action", :id => "button-new"%>
<br><br><br>
<% end %>
My Route
Procedures::Application.routes.draw do
devise_for :users
#### USER MANAGEMENT ####
resources :users do
resources :rateofpays # professional timesheet
resources :roles
resources :biographies
resources :qualifications do
collection do
put 'complete', :action => 'complete'
end
end
resources :supervisors
end
#### users search ####
get 'users/search' => "users#search", as: 'users_search'
The line
get 'users/search' => "users#search", as: 'users_search'
...is too far down in your routes. the resources :users appears first, and it has a match path that looks like users/:id and the users/search is incorrectly matching against that.
Just move the get 'users/search' to the top... or alternatively define it as a collection method under resources :users
resources :users do
collection do
get 'search'
end
<%= 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