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
Related
I have a method to toggle a user between admin or not-admin. When I click the link I am placed back in the User index with the following in my address bar: http://localhost:3000/users?id=1&method=toggle_admin. As far as I can tell, I have the appropriate code to run the method. Can anyone see a mistake?
Here's the view link_to method:
<%= link_to 'Toggle Admin', { :controller => :users, :method => :toggle_admin, :id => user.id} %>
Here's the routes.rb statement:
match 'users/:id/toggle_admin' => 'users#toggle_admin'
The controller method:
def toggle_admin
#user = User.find(params[:id])
User.toggle_admin(#user)
respond_to do |format|
format.html { redirect_to #users }
end
end
The model method:
def toggle_admin(user)
if user.is_admin.nil or user.is_admin = ''
user.is_admin = false
end
user.toggle is_admin
user.save
end
Trying using :action instead of :method. :method should be used with HTTP verb (i.e GET, PUT and so on)
<%= link_to 'Toggle Admin', { :controller => :users, :action => :toggle_admin, :id => user.id} %>
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| %>
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 %>
I want to update attributes in a data table called "rang" from 0 to 1 using a link.
I have an action:
def ready
#task = Task.find(params[:id])
#task.update_attributes(:ready => '1')
#task.save
redirect_to :action => :index
end
And a link:
<%= link_to 'READY', { :action => :ready, :id => task.id } %>
But nothing happens. What am I doing wrong?
Try :
in routes.rb
resources :tasks do
member do
get 'ready'
end
end
Then link like:
<%= link_to 'READY', ready_task_url(task.id) %>
in my group controller I have two methods:
def new
#group = Group.new
respond_to do |format|
format.js
end
end
def new_beta
#group = Group.new
respond_to do |format|
format.js
end
end
I have a form that starts like so:
<%= form_for Group.new, :remote => true do |f| %>
How can I get the form_for to post to the new_beta controller? Thanks
You can set :
<%= form_for Group.new, :url=>{ :action =>"new_beta", :controller =>
"group"}, :remote => true do |f| %>
(you can also -preferably- directly use a named route instead of ":url => ")
First this is bad practice but..
in your routes add
resources :groups do
member do
get :new_beta
post :new_beta_create
end
end
Now
<%= form_for Group.new, :url => new_beta_create_groups_path, :remote => true do |f| %>
However I recommend creating a new controller called something like: alternate_groups_controller. Even better make a namespace for them.
Good luck