routing error showing that No route matches [POST] "/admin/locations/1" - ruby-on-rails

I am new to ROR when i edit my location it will gives me following error
No route matches [POST] "/admin/locations/1"
here i am using rails 3.2.12
this is my location controller
class Admin::LocationsController < ApplicationController
def index
#location= Location.order("location desc")
end
def new
#location=Location.new
end
def create
#location = Location.new(params[:location])
if #location.save
# flash[:notice] = 'Location is successfully added in to list.'
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def edit
#location = Location.find(params[:id])
end
def update
#location = Location.find(params[:id])
if #location.update_attributes(params[:location])
#flash[:notice] = 'Category is successfully updated.'
redirect_to :action => 'index'
else
render :action => 'index'
end
end
end
this is my edit.html.erb
<h2>Edit Location</h2>
<%= simple_form_for(:location, :url => {:action => 'update', :id => #location.id}) do |f| %>
<%= render(:partial => 'form', :locals => {:f => f}) %>
<%= submit_tag("Update",) %> <%= link_to("cancle", {:action => 'index'} )%>
<%end%>
and this is my route.rb
GuestHouse::Application.routes.draw do
devise_for :customers
namespace :admin do
resources :locations
end
and in my index.html.erb as
<%= link_to("Edit", {:action => 'edit', :id => location.id}, :class => 'btn btn-info')%>

<%= simple_form_for(:location,
:url => {:action => 'update', :id => #location.id},
:method => 'put' ) do |f| %>
Pass method in simple_form_for

For an edit form, you likely want to be using the PUT method instead of POST. It looks like you are using [SimpleForm][1], though, which normally would handle constructing the path for a given model for you. Is there any reason you are not passing your Location instance in your call to simple_form_for? I would expect something like the following:
<%= simple_form_for #location do |f| %>
...

Here your routes for admin/location like the following.
admin_locations GET /admin/locations(.:format) admin/locations#index
POST /admin/locations(.:format) admin/locations#create
new_admin_location GET /admin/locations/new(.:format) admin/locations#new
edit_admin_location GET /admin/locations/:id/edit(.:format) admin/locations#edit
admin_location GET /admin/locations/:id(.:format) admin/locations#show
PUT /admin/locations/:id(.:format) admin/locations#update
DELETE /admin/locations/:id(.:format) admin/locations#destroy
so if you want to send the form to 'update' action, you should mention the path like below.
<%= simple_form_for #location, :url => admin_location_path(#location),:html => { :method => "post"} do |f| %>

Related

Making a link in Ruby on Rails

I'm really really newbie in Ruby on Rails...
I'm trying to make a link to another page in my project, where it's listed the posts that belong to an escuela.
This is what I did:
In posts_controller.rb I wrote:
def postesc
#posts = Post.where(:escuela_id => params[:id])
respond_to do |format|
format.html # postesc.html.erb
format.json { render json: #posts }
end
end
In config/routes.rb I wrote:
match 'postesc' => 'posts#postesc'
In view/escuelas/listaesc.html.erb I wrote the link:
<%= link_to "Escuelas", :controller => "posts", :action => "postesc" %>
And in view/escuelas/postesc.html.erb I want to make a list of the matching posts.
But this page appears just blank, with only the layout.
Please, some help?
First make the association between post and escuela, then you can find it just by
Escuela.find(params[:id]).posts
Change your routes to -
resources :posts do
get 'postesc', :on => :collection
end
View :
<%= link_to "List posts", postesc_posts_path %>
make a change in routes.rb as
get 'postesc' => 'posts#postesc'
try...<%= link_to "Escuelas", postesc_path %>
OR
<%= link_to "Escuelas", { :controller => "posts", :action => "postesc" } %>
you're missing to add an ID for the Escuela to be selected - as you're doing in your Controller#postesc Action (as in words: where: escuela_id => params[:id]).
<%= link_to "Escuela", :controller => "posts", :action => "postesc", :id => 1 %>
but you could use the object-link method using the following syntax (by changing your routes a litte):
# in routes.rb
match 'postesc' => 'posts#postesc', on: :collection, as: 'esc_index'
# in your view
<%- for escuela in #escuelas do %>
<%= link_to "Escuela", esc_index(escueal) %>
<% end %>

Form submit says Unkown Action?

Im trying to create a form that submits to a certain action within the current controller (results_controller).
My form code is:
<%= form_tag(:controller => "results", :action => "filter", :id => "filter") do %>
And in the results_controlle.rb i have :
def filter
#setting dispatches
#dispatches = Dispatch.find_by_message_ids(params[:message_ids]) unless params[:message_ids].blank?
unless #dispatches.blank? || #input_messages.blank?
#output_messages = OutputMessage.find_by_dispatch_ids(
#dispatches.collect{|d| d.id }.uniq
)
end
respond_to do |format|
format.html #default rendering
end
end
Yet i get the error:
Unknown action
The action 'filter' could not be found for ResultsController
Any suggestions?
UPDATE
On the same page, i have a form that does work:
<%= form_tag(:controller => "results", :action => "show", :id => "show") do %>
try instead
<% form_tag(filter_result_path(), :method => :get) do %>
and add to your route table
resources :results do
member do
get 'filter'
end
end

Rails 3 form_for nested routes for custom action

My setup: Rails 3.0.9, Ruby 1.9.2
I added a custom action to a nested resource task.
routes.rb
resources :tasks
resources :projects do
resources :tasks, :constraints => { :protocol => "http" } do
put :cancel, :on => :member
end
end
rake routes shows
cancel_project_task PUT /projects/:task_id/tasks/:id/cancel(.:format) {:protocol=>"http", :action=>"cancel", :controller=>"tasks"}
In my controller,
tasks_controller.rb
def cancel
#task = Task.find(params[:id])
respond_to do |format|
if #task.cancel
format.html { redirect_to(#task, :notice => 'Task was successfully canceled.') }
else
format.html { render :action => "edit" }
end
end
end
I need to define a form to perform the action, here's what I have currently
_form.html.erb for subscription
<%= form_for [#project, #task], :url => { :action => 'cancel' } do |f| %>
<%= f.submit "Cancel your task"%>
<% end %>
It throws an error
No route matches {:action=>"cancel", :controller=>"tasks"}
I also tried adding :method => "put" with the same error
_form.html.erb for subscription
<%= form_for [#project, #task], :url => { :action => 'cancel', :method => "put" } do |f| %>
<%= f.submit "Cancel your task"%>
<% end %>
Anyone knows the correct form_format syntax to accomplish this?
In case anyone wants the answer, here's what I had to do
<%= form_for [#project, #task], :url => cancel_project_task_path(#project, #task) do |f| %>
It took me way too long to figure this out, hopefully this helps the next unsuspecting developer.

Rails Routing Error

In routes.rb I have:
get "survey/show" => "survey#show"
post "survey/step_2" => "survey#step_2"
post "survey/step_3" => "survey#step_3"
And in step_2.html.erb I have:
<%= form_for #result, :url => { :controller => 'survey', :action => 'step_3' } do |f| %>
And in survey_controller.rb I have:
def step_2
#result = Result.new(params[:result])
if #result.save
session[:result_id] = #result.id
render :action => "step_2"
else
render :action => "show"
end
end
def step_3
#result = Result.find(session[:result_id])
if #result.update_attributes(params[:result])
render :action => "step_3"
else
render :action => "step_2"
end
end
And when I submit the form on step_2 I get the following error:
No route matches "/survey/step_3"
I believe Rails form_for method may be making that a PUT request, since the #result object has an id. I believe you should change your form_for line to:
<%= form_for #result,
:url => { :controller => 'survey', :action => 'step_3' },
:html => { :method => :post} do |f| %>
or change the route type to put in routes.rb
You have to use match.
match 'survey/step_3' => 'survey#step_3', :via => 'post'
I might be wrong about the :via, but it's something like that.

How do I create a link that passes an ID for a custom action in Rails?0 <%= link_to "PDF", :action => "showpdf", :id => "#{#letter.id}.pdf" %>

I have the following code:
<%= link_to "PDF", :action => "showpdf", :id => "#{#letter.id}.pdf" %>
'showpdf' is an action in my Letters controller.
My expectation is that this link should yield the following:
http://domain.com/letters/showpdf/id.pdf
But instead, I get:
http://domain.com/letters/showpdf?id.pdf
If the default routes are :controller/:action/:id shouldn't this work?
Do I need to do something in the routes, even though the format for default appear right? Thanks!
Have you tried something like:
<%= link_to "PDF", :action => "show", :id => letter.id, :format => :pdf %>
where your route would be
:controller/:action/:id.:format
and in your controllers "show" action:
respond_to do |format|
format.pdf .....
format.html .....
end

Resources