I want to use three models in one controller
if click tag '삭제', I want to execute free_destroy action in management controller.
When I click the link, this error page occurs:
ActiveRecord::RecordNotFound in ManagementsController#free_destroy
and Couldn't find Management with id=1
How can i fix it?
Why does this error occur?
Please answer for me.
controller
class ManagementsController < ApplicationController
load_and_authorize_resource param_method::params_management
def index
#users = User.all
#freemanages = Freemanagement.all
end
def freepost_delete
#freemanage = Freemanagement.new(params.require(:modal).permit(:content, :title_id))
#freemanage.save()
redirect_to :back
end
def secret_delete
# #secretmanage = Secretmanagement.new(params.require(:modal).permit(:content, :title_id))
# #secretmanage.save()
# redirect_to :back
end
def show
freemanage = Freemanagement.find(params[:id])
redirect_to freemanagements_path(freemanage), method: :delete
end
def free_destroy
freemanage = Freemanagement.find(params[:id])
freemanage.destroy()
redirect_to freepost_path(freemanage.title_id), method: :delete
end
def refuse_freepost
freepost = Freepost.find(params[:remodal][:title_id])
freemanages = Freemanagement.all
freemanages.each do |freemanage|
if (freemanage.title_id.to_i != freepost.id)
next
else
freemanage.destroy()
end
end
redirect_to :back
end
def refuse_secretpost
end
end
index.html
<head>
</head>
<body>
<header>
<!-- 공지사항 작성 글 -->
<%= link_to '익명겟', secretposts_path %>
<%= link_to '자유겟', freeposts_path %>
<%= current_user.last_name + current_user.first_name %>
</header>
<section>
<% #users.each do |user| %>
<%= user.uid %>
<%= user.first_name %>
<%= user.last_name %>
<%= user.roles_name%>
<% end %>
</section>
<section>
<% #freemanages.each do |freemanage| %>
<%= link_to '게시글보기', freepost_path(freemanage.title_id) %>
<%= freemanage.content %>
<a href='/freemanagements/<%= freemanage.id %>/delete'>삭제</a>
<br />
<% end %>
</section>
<section>
<!-- 익명게시판 관련 게시글 섹션 -->
</section>
<script>
</script>
</body>
routes.rb
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'homes#index'
resources :freeposts do
resources :free_comments
end
resources :secretposts do
resources :secret_comments
end
resources :free_comments
resources :secret_comments
get '/managements' => 'managements#index'
get '/freemanagements/:id' => 'managements#show'
post '/freemanagements' => 'managements#freepost_delete'
get '/freemanagements/:id/delete' => 'managements#free_destroy'
post '/freemanagements/refuse' => 'managements#refuse_freepost'
post '/secretmanagements' => 'managements#secretpost_delete'
get 'login', to: redirect('/auth/google_oauth2'), as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
# get 'home', to: 'home#show'
# get 'me', to: 'me#show', as: 'me'
end
Related
I'm trying to make a dropdown search by category feature on my webapp. I am currently trying to get the dropdown menu to work, but I am constantly getting the error saying:
First argument in form cannot contain nil or be empty
Here is the view:
<%= form_for #search, html: {class: "pure-form"} do |s| %>
<div class="field">
<%= s.label :category %>
<%= s.select(:category, options_for_select([['Other', 'Other'], ["Breakfast", "Breakfast"], ["Lunch/Dinner", "Lunch/Dinner"], ["Dessert", "Dessert"]])) %>
<%end%>
<div class="card-columns">
<% #images.each do |image| %>
<div class="card">
<div class="card-body p-1">
<%= image_tag image.image, class: 'd-block w-100' %>
<%= link_to image.title, image_path(image.id), class: "btn btn-primary stretched-link" %>
</div>
</div>
<% end %>
</div>
And here is the controller:
class SearchesController < ApplicationController
def new
#search = Search.new
#categories = Image.uniq.pluck(:category)
end
def create
#search = Search.create(search_params)
redirect_to #search
end
def show
#search = Search.find(params[:id])
end
private
def search_params
params.require(:search).permit(:category)
end
end
And here are the routes:
Rails.application.routes.draw do
devise_for :users
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root 'pages#home'
get '/images', controller: 'images', action: 'recent'
get '/upload', controller: 'images', action: 'new'
get '/#:username', to: 'users#show', as: :profile
delete 'images/:id(.:format)', :to => 'images#destroy'
resources :images, only: [:index, :show, :create]
get 'searches/search'
resources :searches
end
I tried to configure different things to see if I could get it working, but I could not. I am very unclear as to what is the problem. Any help would be appreciated! Thanks in advance!
I am new bee in rails and trying to figure out how to pass id using custom controller but it look a blocker to me.
below is code of controller :
class UploadsController < ApplicationController
def index
#upload = Upload.new
#uploadimage = Upload.all
end
def home
end
def destroy
end
end
below is the index.html.erb file
<h1>Upload#index</h1>
<p>Find me in app/views/upload/index.html.erb</p>
<%= form_for(#upload, url: "/uploads/home", :html => {:class => 'dropzone'} ) do |f| %>
<div class="fallback">
<input name="file" type="file" multiple />
</div>
<% end %>
<h3>Uploaded images</h3>
<% #uploadimage.each do |image| %>
<% if !image.nil? %>
<%= image_tag("/images/#{image.name}",style: 'height:100px; width:100px;') %>
<%= link_to "delete #{image.name}", uploads_url(image) %>
<% else %>
<p>No images are present in db</p>
<% end %>
<% end %>
below is routes.rb
Rails.application.routes.draw do
get "uploads", to: "uploads#index", via: :get
get "uploads/:id", to: "uploads#destroy", via: :get
get "uploads/index/:id", to: "uploads#destroy", via: :get
get "uploads/index/", to: "uploads#index", via: :get
root "uploads#index"
end
how to pass id to destroy/home action as i m getting id as dot instead of slash
Why you are using custom routes, if rails providing RESTful routes using resources
Rails.application.routes.draw do
resources :uploads
root "uploads#index"
end
Edited:
Rails.application.routes.draw do
resources :uploads, :controller => "custom_controller"
root "uploads#index"
end
This will manage all 7 action, and check rake routes for path for this all 7 action.
I made Forum model which belongs to Category and in show view of every category each forum from that category needs to be showed, but links are pointing at wrong paths (for example, in category 4 I have only one forum which points to category/1/forum/1 and category 1 doesn't even exist).
One more note: each category has it's position field (integer) and they're sorted with that field in ascending order. Category 4 have position 1, but why would that number be on the place of category id in url?
When I try to access /category/4/forum/1 I get the error "Couldn't find Category with 'id'=1"
CategoriesController
class CategoriesController < ApplicationController
def index
categories_ordered
end
def new
#category = Category.new
categories_ordered
end
def create
#category = Category.new(category_params)
categories_ordered
if #category.save
redirect_to forums_path, notice: "Kategorija je uspešno kreirana."
else
render "new"
end
end
def show
find_category
find_forums
end
def edit
find_category
end
def update
find_category
if #category.update(category_params)
redirect_to category_path(#category), notice: "Kategorija je uspešno ažurirana."
else
render "edit"
end
end
def destroy
find_category
#category.destroy
redirect_to forums_path, notice: "Kategorija je uspešno obrisana."
end
private
def category_params
params.require(:category).permit(:name, :description, :position)
end
def find_category
#category = Category.find(params[:id])
end
def find_forums
#forums = #category.forums.all
end
def categories_ordered
#categories = Category.all.order("position ASC")
end
end
ForumsController
class ForumsController < ApplicationController
def new
find_category
#forum = #category.forums.new
end
def create
#category = Category.find(params[:category_id])
#forum = #category.forums.create(forum_params)
if #forum.save
redirect_to category_path(#category), notice: "Forum je uspešno kreiran."
else
render "new"
end
end
def show
find_category
find_forum
end
def edit
find_category
find_forum
end
def update
find_category
find_forum
if #forum.update(forum_params)
redirect_to forum_path(#forum), notice: "Forum je uspešno ažuriran."
else
render "edit"
end
end
private
def forum_params
params.require(:forum).permit(:title, :description)
end
def find_category
#category = Category.find(params[:category_id])
end
def find_forum
#forum = #category.forums.find(params[:id])
end
end
categories#show
<% authorize %>
<h2><%= #category.name %></h2>
<p><%= #category.description %></p>
<div class="list-group">
<% #category.forums.each do |f| %>
<a href="<%= forum_path(f) %>" class="list-group-item">
<h4 class="list-group-heading"><%= f.title %></h4>
<p class="list-group-text">
<%= f.description %>
</p>
</a>
<% end %>
</div>
<% if is_admin? %>
<%= link_to new_forum_path, class: "btn btn-primary" do %>
<span class="glyphicon glyphicon-plus"></span> Dodaj forum
<% end %>
<%= link_to edit_category_path(#category), class: "btn btn-primary" do %>
<span class="glyphicon glyphicon-pencil"></span> Izmeni
<% end %>
<%= link_to category_path(#category), class: "btn btn-danger", method: :delete, data: { confirm: "Da li ste sigurni da želite obrisati ovu kategoriju?" } do %>
<span class="glyphicon glyphicon-trash"></span> Obriši
<% end %>
<% end %>
routes.rb
Rails.application.routes.draw do
root "welcome#index"
get "start" => "welcome#index", as: "index"
get "registration" => "users#new", as: "register"
get "login" => "sessions#new", as: "login"
post "login" => "sessions#create"
get "logout" => "sessions#destroy", as: "logout"
get "users" => "users#index", as: "users"
post "users" => "users#create"
get "profile" => "users#show", as: "profile"
get "user/:id" => "users#show", as: "user"
get "user/:id/edit" => "users#edit", as: "edit_user"
patch "user/:id" => "users#update"
delete "user/:id" => "users#destroy"
get "categories/add" => "categories#new", as: "new_category"
get "category/:id" => "categories#show", as: "category"
patch "category/:id" => "categories#update"
delete "category/:id" => "categories#destroy"
get "category/:id/edit" => "categories#edit", as: "edit_category"
get "terms" => "welcome#terms", as: "terms"
get "wh" => "bugs#new", as: "wh"
get "bugs" => "bugs#index", as: "bugs"
post "bugs" => "bugs#create"
get "bug/:id" => "bugs#show", as: "bug"
delete "bug/:id" => "bugs#destroy"
get "forum" => "categories#index", as: "forums"
get "category/:category_id/forum/add" => "forums#new", as: "new_forum"
get "category/:category_id/forum/:id" => "forums#show", as: "forum"
get "category/:category_id/forum/:id/edit" => "forums#edit", as: "edit_forum"
patch "category/:category_id/forum/:id" => "forums#update"
delete "category/:category_id/forum/:id" => "forums#destroy"
resources :users
resources :sessions
resources :categories do
resources :forums
end
resources :bugs
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Error is partially solved; I just had to use different parameters with ForumsController (category_id for categories and id for forums).
def find_category
#category = Category.find(params[:category_id])
end
def find_forum
#forum = #category.forum.find(params[:id])
end
And change forum routes accordingly.
It looks like problem still isn't solved. I changed this parts but I still have problems with URLs. Now category and forums IDs are mixed in url (instead of /category/4/forum/1 it shows /category/1/forum/4).
Edit: Problem is finally solved! This answer gave me an idea what I should do. I just had to specify #category with paths too (just changing two lines):
<div class="list-group">
<% #category.forums.each do |f| %>
<a href="<%= forum_path(#category, f) %>" class="list-group-item">
<h4 class="list-group-heading"><%= f.title %></h4>
<p class="list-group-text">
<%= f.description %>
</p>
</a>
<% end %>
</div>
<% if is_admin? %>
<%= link_to new_forum_path(#category), class: "btn btn-primary" do %>
<span class="glyphicon glyphicon-plus"></span> Dodaj forum
<% end %>
I have a users model and controller. I am trying to make a put and delete request to my controller where the put request is tied to users#allow and the delete request is suppose to be directed to users#destroy. I However keep on getting this error when I make the request:
No route matches [GET] "/users/1/allow"
I am also using devise for users and admin classes so that might have something to do with this error. Im not really sure. Here is my routes.rb:
Rails.application.routes.draw do
get 'users/index'
put 'users/:id/allow', :to => 'users#allow', :as=>:users_allow
delete 'users/:id/destroy', :to => 'users#destroy',:as => :users_destroy
devise_for :users
devise_for :admins
root 'website#show'
resources :tweets do
put '/pass', :to => 'tweets#pass',:as => :pass
put '/fail', :to => 'tweets#fail',:as => :fail
end
get '/to_json', :to=>'tweets#to_json'
end
Here is my users controller:
class UsersController < ApplicationController
before_action :find_user, only:[:allow,:destroy]
before_filter :authenticate_admin!
def index
#users = User.all
end
def allow
find_user.update(:edit_permission=>true)
redirect_to(:back)
end
def destroy
find_user.destroy
redirect_to(:back)
end
private
def find_user
#user = User.find(params[:user_id])
end
end
And here is my users view where I am calling from, users/index.html.erb:
<h1>Users#index</h1>
<p>Find me in app/views/users/index.html.erb</p>
<ul>
<% #users.each do |user|%>
<% if user.edit_permission == nil%>
<li style="color:red"> <%=link_to 'approve', users_allow_path(user), method: :put %> <%=link_to 'delete', users_destroy_path(user), method: :delete %><%= "#{user.name} #{user.email}"%> </li>
<% else%>
<li style="color:green"><%=link_to 'delete', users_destroy_path(user), method: :delete %><%= "#{user.name} #{user.email}"%> </li>
<% end%>
<%end%>
</ul>
I also want to add that when I go into the view and I do an inspect element in my browser for the link for the put request I do see this:
<a rel="nofollow" data-method="put" href="/users/1/allow">approve</a>
I would run rake routes to see your actual routes, because in your routes.rb you actually don't have a route that matches a get with users/:id/allow
#config/routes.rb
resources :users, only: [:index, :destroy] do
put :allow, action: :allow #-> url.com/users/:user_id/allow
end
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
#users = User.all
end
def allow
user.update edit_permission: true
redirect_to :back
end
private
def user
User.find params[:user_id] #-> why return an instance var if you aren't going to use it
end
end
#app/views/users/index.html.erb
<ul>
<% #users.each do |user| %>
<% options = {} %>
<% options.allow = ["allow", "red", "put"] %>
<% options.destroy = ["destroy", "green", "delete"] %>
<% option = user.edit_permission ? options.allow : [options.allow, options.destroy] %>
<% option.each do |action,colour,method| %>
<%= link_to action, eval("users_#{action}_path(user)"), method: method, class: colour %><%= "#{user.name} #{user.email}") %>
<% end %>
<% end %>
</ul>
--
To fix your actual problem, you need to make sure you have your Rails UJS driver loaded:
#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require_tree .
I am currently working on a simple website where people can list postings. Most of the code is based on Michael Hartl's tutorial.
I want users to have the ability to click on a link that displays the listing individually.
currently, the listings' of each user are found under
http://localhost:3000/users/id
Every listing has it's own id
Here are my routes
Rails.application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :listings
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via:'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/new', to: 'listings#new', via: 'get'
Here is my listing_controller.rb
class ListingsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy, :edit]
before_action :correct_user, only: :destroy
def create
#listing = current_user.listings.build(listing_params)
if #listing.save
flash[:success] = "Job Post created"
redirect_to current_user
else
render 'listings/new'
end
end
def edit
end
def show
#listing = Listing.find(params[:id])
end
def new
#listing = Listing.new
#listings = Listing.paginate(page: params[:page])
end
def destroy
#listing.destroy
redirect_to current_user
end
private
def listing_params
params.require(:listing).permit(:description, :location, :title)
end
def correct_user
#listing = current_user.listings.find_by(id: params[:id])
redirect_to current_user if #listing.nil?
end
def current_listing
#listings = listing.find(params[:id])
end
end
I also created a show page for each listing under listings the listings folder.
The show page itself works.
Here is the show.html.erb for the listing
<div class="show_listing">
<div class="col-md-6">
<div class="col-md-6">
<h3><%= #listing.title %></h3>
<h3><%= #listing.location %></h3>
<p><%= #listing.description %></p><br>
<div class="center">
<%= link_to "Apply Now", '#', class: "btn btn-info", data: {no_turbolink: true} %>
</div>
</div>
</div>
</div>
<div class="show_link_position">
<% if current_user == #listing.user %>
<%= link_to 'Edit', '#', class: "btn btn-link" %> |
<% end %>
<%= link_to 'Back', current_user, class: "btn btn-link" %>
</div>
Now I would like to have a link on the user page (under each listing) that would link to each post individually.
I'm looking for something like this
how to display a link to individual microposts? (ruby on rails 3)
Thank you
Let me know if you need more information
To create a link from a page where you have the collection of listings as #listings, you might have something like this...
<ol class="listings">
<% #listings.each do |listing| %>
<div class="listing">
<h2><%= listing.title</h2>
<h3><%= listing.location</h3>
<p><%= listing.description</p>
<%= link_to "Show", listing, class: "btn btn-link" %>
</div>
<% end %>
</ol>
Here is what I did to get it to work (with the help of the following stackoverflow post how to display a link to individual microposts? (ruby on rails 3))
Routes rb.
match '/users/:id/:id', to 'listings#show', via: :get, as: :user_listing
users view file
add the link
<li><%= link_to "Show", user_listing_path(id: #user.id, id: listing.id) %></li>
Changed my listing_controller file
def show
#user = User.find_by_id(params[:id])
#listing = Listing.find_by_id(params[:id])
end