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.
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 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
So I'm getting the error stated in the title. What code would I have to write in my Postscontroller to fix this? I'm not sure what I would have to do here, would I have to define rsvp in my post controller? If thats the case how would I go about doing that?
class RsvpController < ApplicationController
def create
rsvp = current_user.rsvps.build({post_id: params[:id]})
if rsvp.save
end
end
end
Show.html.erb
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>where:<%=#post.longitude %>, <%=#post.latitude%></p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User_id:</strong>
<%= #post.user_id %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<%= button_to "Rsvp now", rsvp_post_path(#post), class: "btn btn-primary" %>
routes.rb
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :posts
devise_for :users
root 'home#index'
get 'home/ruby_meetup'
resources :posts do
post 'rsvp', on: :member
end
Also I want it so it shows the list of people who already rsvped for the event. How would I go about doing that? I would appreciate some help as I'm still learning rails and this is my first project.
By the following line, you define the route:
resources :posts do
post 'rsvp', on: :member
end
By this line you're requesting this route:
To process the request, you need a controller action rsvp, which actually the error states.
Thus, just define the action (method) and process the request:
class RsvpController < ApplicationController
def create
rsvp = current_user.rsvps.build({post_id: params[:id]})
if rsvp.save
end
end
def rsvp
# process the request here...
end
end
I am busy going through PBP - Agile Web Development with Rails and implementing the locale switcher.
However when I try switch between english and spanish I get a error:
No route matches [POST] "/en"
My controller is as follows:
class StoreController < ApplicationController
skip_before_filter :authorize
def index
if params[:set_locale]
redirect_to store_path(locale: params[:set_locale])
else
#products = Product.order(:title)
#cart = current_cart
end
end
end
and an extract of the application.hmtl.erb that is being used;
<div id="banner">
<%= form_tag store_path, class: 'locale' do %>
<%= select_tag 'set_locale', options_for_select(LANGUAGES, I18n.locale.to_s), onchange: 'this.form.submit()' %>
<%= submit_tag 'submit' %>
<%= javascript_tag "$('.locale input').hide()" %>
<% end %>
<%= image_tag("logo.png") %>
<%= #page_title || t('.title') %>
</div>
the routing folder is as follows:
scope'(:locale)' do
resources :users
resources :orders
resources :line_items
resources :carts
resources :products do
get :who_bought, on: :member
end
root to: 'store#index', as: 'store'
end
Cant figure out what I did wrong. If I enter /en or /es in the url it works correctly. However choosing it in the drop down that is created I get the error mentioned
Found the issue, the form_tag was expecting a POST so I changed
<%= form_tag store_path, class: 'locale' do %>
to
<%= form_tag store_path, class: 'locale', :method => :get do %>
and it worked
You are missing an slash in the scope as guides states:
# config/routes.rb
scope "/:locale" do
resources :books
end
http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params
I have a controller games and a method:
def index
#games = Game.all
end
def set_game
#current_game = Game.find(params[:set_game])
end
In my view I have:
<%= form_tag("/games") do %>
<% #games.each do |g| %>
<%= radio_button_tag(:game_id, g.id) %>
<%= label_tag(:game_name, g.name) %><br>
<% end %>
<%= submit_tag "Confirm" %>
<% end %>
Routes:
resources :games
match 'games', :to => 'game#index'
How can I make this form work for my set_game method?
Thanks.
<%= form_tag(set_game_games_path) do %>
...
<% end %>
#routes.rb
resources :games do
collection do
get '/set_game', :as => :set_game
end
end
That's an example of a custom route:
match "customroute" => "controller#action", :as => "customroutename"
This can be then accessed with "customroutename_url" in your views. If you want to create a custom route for your set_game action, for example, it would be
match "setgame" => "games#set_game", :as => "setgame"
Then you can do
<%= form_tag setgame_url %>
...
<% end %>
You can read more about custom routes here: http://guides.rubyonrails.org/routing.html#non-resourceful-routes