So, I have the following link-to:
<%= link_to(outing_add_guests_path, :class => 'modal') do %>
<div id="notImportant"></div>
<% end %>
When I click on it, Rails tells me that
No route matches {:controller=>"outings", :action=>"add_guests"}
However, here's my routes file:
resources :outings do
get "/add_guests" => "outings#add_guests"
post "/add_guests" => "outings#add_guests"
delete "/remove_guests" => "outings#remove_guests"
end
and the corresponding action from my Outings Controller:
def add_guests
#outing_guest = OutingGuest.new(:outing_id => params[:outing_id])
#outing_guest.user_id = params[:user_id]
if #outing_guest.save
flash[:notice] = "Guest added successfully"
redirect_to({ :action => 'outing', :id => params[:outing_id] })
else
flash[:notice] = "Guest could not be added"
redirect_to({ :action => 'outing', :id => params[:outing_id] })
end
end
Is there any reason Rails would be unable to detect my controller or its actions?
EDIT: Here's part of the results from rake routes
outing_add_guests GET /outings/:outing_id/add_guests(.:format) outings#add_guests
POST /outings/:outing_id/add_guests(.:format) outings#add_guests
I notice your link_to is not consistent with the other routes
outing_add_guests_path
outings_add_guests_path
Did you do rake routes to verify that outing_add_guests_path exists?
EDIT:
Your rake routes shows you need an outing_id so your routes aren't setup right (at least not for the POST). I'd fix them the way #RyanBigg is suggesting.
You should be defining these routes using the collection method:
resources :outings do
collection do
get :add_guests
post :add_guests
delete :remove_guests
end
end
What this will do is define new routes for the specified actions, as well as automatically defining the routing helpers for those routes. For more information please read the Routing Guide.
Related
I have a _form for new and edit for a #Giveaway object. Within this form I have a field for a random winner.
I want to populate this field by calling the method giveaways#random_winner with <%= button_to "Randomly Pick Winner!", {:action => 'choose_winner'}, :method => :get %>, but I am getting this error No route matches {:action=>"choose_winner", :controller=>"giveaways"} when loading /giveaways/new.
Here is my controller:
def choose_winner
random_winner = SubscriberUser.where(user_id: current_user.id).pluck(:subscriber_id).sample(1)
session[:random_winner] = random_winner
redirect_to :back
end
Here are the routes that I have tried. I'm not very good at non-scaffold routes yet:
resources :giveaways do
member do
get 'choose_winner' => 'giveaways#choose_winner'
#tried get :choose_winner, as: :choose_winner
#tried get 'new/choose_winner'
#tried get 'choose_winner'
#tried get 'choose_winner', to: 'giveaways#choose_winner', as: 'choose_winner'
end
end
Question -- Why is the page not loading when I have defined the controller and action in the route? Will I have to reload the page when I do run that route... is there a better way to get at this data?
Your routes.rb is close
resources :giveaways do
member do
get :choose_winner
end
end
And then I would use a Rails route helper so you don't have to worry about setting the action/controller yourself.
<%= button_to "Randomly Pick Winner", choose_winner_giveaway_path(#giveaway), method: :get %>
The link in _applicant.html.erb looks like this in the browser: http://localhost:3000/needs/3/applicants.1
and when clicked on this shows up in the browser:
Routing Error
No route matches [PUT] "/needs/3/applicants.1"
I want it to update the acceptance column for this particular applicant row. Basically I want it to send data to the update method of the applicants controller. How can I modify the code to do this?
_applicant.html.erb
<%= link_to 'Accept Applicant', need_applicants_path(applicant.need_id, applicant.id), :method => :put, :action => "update", :applicant => {:acceptance => true} %>
got this from running rake routes:
PUT /needs/:need_id/applicants/:id(.:format) applicants#update
routes.rb:
resources :needs, except: [:new] do
resources :applicants
end
applicants_controller.rb
class ApplicantsController < ApplicationController
def update
#need = Need.find(params[:need_id])
#applicant = #need.applicants.find(params[:id])
if #applicant.update_attributes(params[:applicant])
flash[:success] = 'Your applicant has been accepted/rejected!'
redirect_to #need
else
#need = Need.find(params[:need_id])
render 'needs/show'
end
end
end
I think there are two possible fixes here:
First,
http://localhost:3000/needs/3/applicants.1
should probably read
http://localhost:3000/needs/3/applicants/1
The error is in this line:
<%= link_to 'Accept Applicant', need_applicants_path(applicant.need_id, applicant.id), :method => :put, :action => "update", :applicant => {:acceptance => true} %>
where...
need_applicants_path(applicant.need_id, applicant.id)
You can try passing in two instance objects like so:
need_applicants_path(Need.find(applicant.need_id), applicant)
Second, another possible solution is to explicitly set the PUT path in your routes.
In your config/routes.rb add the line
put 'need/:need_id/applicant/:id/update
then run
rake routes
and see what the PUT path is
I'm using a custom action to get the id of a project into the session, so that only relevant info for that project is shown in other areas. I've made a custom action in the projects controller, and am having trouble getting a link to work in the view to call that action. I just get an error saying "Couldn't find project without ID". I'm new to rails - I know it's probably an easy question, but help would be much appreciated, thanks!
View Code:
<%= link_to 'Select Project', :action => :select_project %>
Controller Code:
def select_project
#project = Project.find(params[:id])
session[:project_id] = #project.id
end
Routes:
resources :projects do
collection do
get :select_project
end
end
Alternative routes code:
resources :projects do
put 'select_project', on: :member
end
This is untested but I believe it is what you are looking for:
Routes:
resources :projects do
member do
post :set_current
end
end
this should create the following:
Endpoint: /projects/:id/set_current POST
Helper: set_current_project_path
Controller
def set_current
project = Project.find(params[:id])
session[:project_id] = project.id
redirect_to projects_path, :notice => "Current project set to #{project.name}"
end
Views
# index / erb tags excluded for simplicity
#projects.each do |project|
link_to 'Select Project', set_current_project_path(project), :method => :post
end
# show
<%= link_to 'Select Project', set_current_project_path(#project), :method => :post %>
See:
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
Note also the use of 'post' instead of 'get', since we are changing the state of an object (session)
it is preferred to use a post not a get, otherwise users might pull up an old get request in the address bar
of their browser and set their session to a project unknowingly.
like varatis said - use rake routes or CONTROLLER=projects rake routes to help with determining what your route/path helpers look like and what http verbs they are expecting
And is there a reason why it's project not #project in the controller
The #project creates an instance variable; in a rails controller instance variables are made available to the views. This set_current action will never render a view, so no reason to make an instance variable out of it.
How come you have to set it to member and not collection in the routes
any action where you want to reference params[:id] should be a member route, an alternative would be to leave it as a collection route and pass params[:project_id] and pass that in all of your link_to calls, but in this case member makes more sense.
I believe resources :projects is a short cut for this break down
member do
get :show
get :edit
put :update
delete :destroy
end
collection do
get :index
get :new
post :create
end
hopefully that clarifies your questions some?
I think the route generated would be select_project_projects_path.
Link:
<%= link_to 'Select Project', select_project_projects_path %>
For future reference, run rake routes to see the automatic route helpers generated by Rails.
HERE IS MY new.html.erb FILE BELOW
<%= form_for(:subject, :url => {:action => 'create'}) do |f| %>
i just want to insert a record in my database.
Here is my subjectscontroller.rb file below
end
Here is my route.rb file below
get "subjects/create"
When i run localhost:3000/subjects/new it runs perfectly. And then I try to insert a value through text boxes and when i click on Create subject button of my form it gives me no route matches create.Please solve my problem as soon as possible otherwise i am in deep trouble
You've defined a route which responds to an HTTP GET. When submitting the form, you send an HTTP POST. That's the source of your current error message.
Before panicking, take a few minutes to read through this Rails Guide on routing. I think it will help clarify how to proceed.
You should add the subjects to the routes as resources .
AppName::Application.routes.draw do
resources :subjects
root :to => "demo#index"
get ':controller(/:action(/:id(.:format)))'
end
Your controller should be like this
def create
#subject = Subject.new(subject_params)
if #subject.save
redirect_to(:action => 'list')
else
render('new')
end
end
private
def subject_params
params.require(:subject).permit(:id, :name, :position, :visible)
end
It should work.
When I am on the index page and click on the delete link to destroy the post i get that error:
Unknown action
No action responded to delete. Actions: add, edit, and index
The edit link next to delete works with out a problem I do not understand why delete won't work. This is what is in my controller car_controller.rb
def delete
#car = Car.find(params[:id])
flash[:notice] = "Question #{#car.name} deleted!"
#car.destroy
redirect_to :controller => :car, :action => :index
end
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.root :controller => "main"
map.root :controller => "car"
end
Isn't the action for delete actually destroy?
If you look at a controller that was generated as part of scaffold, you should see what the delete action maps to...
Rails provides the 7 classical RESTful actions out of the box when you generate the controller and each one has the actual URL + method commented above the action method...
# DELETE /subject_families/1
# DELETE /subject_families/1.xml
def destroy
...
end
I hope this helps...
Make sure your request is using the correct HTTP verb. Rails' default for REST resources is to use HTTP DELETE on the destroy action. Are you using GET (a simple link) or POST instead of DELETE?
I recommend checking your routes - run rake routes - to confirm what Rails is expecting. If you are using Rails' resources route generator, then I would expect your view template to contain something like this:
<%= form_for #car, :html => { :method => :delete } do |f| %>
<%= submit_tag 'Delete Car' %>
<% end %>