Routing issue No route matches post - ruby

I'm new to spree and having some issue with routes, on my form, when I click submit, I get the following error:
No route matches [POST] "/admin/collection_pages/new"
I'm guessing POST is only for create method.
My Routes for the extension
Spree::Core::Engine.routes.draw do
namespace :admin do
resources :collection_pages
end
end
My controller:
module Spree
module Admin
class CollectionPagesController < ResourceController
The form
= semantic_form_for(:collecion_pages) do |f|
= render :partial => 'spree/shared/error_messages', :locals => { :target => #collecion_pages }
.
.
.
= submit_tag t("create")
any help would be greatly appreciated ! thank you so much

Related

undefined local variable or method `params' when implementing Search Field

I am doing a tutorial from Stuck.io on implementing a search field on my admin model, so someone can search for a specific admin. the form is appearing, however when I try to use the search I am getting an error field and I don't know where I've gone wrong.
The Exact error I am getting is:
NameError at /admins
undefined local variable or method `params' for #<Class:0x007f91e39df950>
My custom Devise Admin Controller: (to create an index and show page)
class AdminController < ApplicationController
before_action :authenticate_admin!
def index
#admins = Admin.all
# Search Query
#admins = Admin.search(params[:search])
end
def show
#admin = Admin.find_by_admin_ident(params[:id])
end
end
Routes for my custom Admin controller:
admins GET /admins(.:format) admin#index
admin GET /admins/:id(.:format) admin#show
My Admin Model: Search method
class Admin < ActiveRecord::Base
# Search Functionality
def self.search(search)
if search
#admins = Admin.where(["name LIKE ?","%#{params[:search]}%"])
else
all
end
end
end
And finally my Search form:
<div class="col-md-4">
<div class="info-block">
<%= form_tag admins_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], :class => 'form-control', :id => 'admin-search-field' %>
<center><%= submit_tag "Search Users", :class => 'btn btn-default btn-sm', :id => 'search-btn' %></center>
<% end %>
</div>
</div>
Thanks in advance for your input! Please let me know if you need anything more!
You cannot access the params hash inside the model. This is only accessible from controllers.
Instead of:
#admins = Admin.where(["name LIKE ?","%#{params[:search]}%"])
Use:
#admins = Admin.where(["name LIKE ?","%#{search}%"])
And then in the controller, pass the value as you are doing already:
Admin.search(params[:search])

Routing Issue in Rails 4

I have created a form useing form_for -
= form_for #category, url: url_for(:controller => 'admin/category',:action => new_record ? "create" : "update"), name: 'udfFieldForm', id: 'udfFieldForm',:method =>'POST', remote: true do |f|
controller is look like-
class Admin::CategoryController < ApplicationController
def create
end
def update
end
end
route defined as -
namespace :admin do
get 'category/:action' => 'category#index', :as => :category
resource :categories
end
When i have submit form it through an error like -
AbstractController::ActionNotFound (The action 'category' could not be found for AdminController):
Here category is a controller under admin directory but it's looking for category action in admin controller. here i want to call category controler.
Please help me , where is the issue?
The application expects a controller named AdminController with a method named category, but it cannot find it. That is what the error message says and this how you created the link ...url: url_for(:controller => 'admin/category'...
I think that is wrong, since obviously you don't have a category method/action in your AdminController.
Need To Update Routes Like as with in admin namesapce
post 'category/:action'=> 'category', action: :create
patch 'category/:action'=> 'category', action: :update

Get Rails to hit controller with an input tag submit

View:
%input{type: 'submit', action: 'home#create_user'}
Controller:
class HomeController < ApplicationController
def index
render 'home/index'
end
def sign_up
render 'home/sign_up'
end
def create_user
render 'dashboard/dashboard'
end
end
routes.rb
post 'home/create_user' => 'home#create_user', :as => :create_user
Why is this button not hitting the controller?
By itself, an submit tag isn't going to generate the form. I tried your code above and the button doesn't do anything. Unless I'm forgetting INPUT doesn't have an ACTION attribute.
If you had a link to that page, the reason it's not working is because by default that link will be a GET request and you've restricted the route to POST.
So, either wrap it up in a form or use button_to or the :method => :post solutions to make it POST the request and it should work.
Something like this:
= button_to 'click me', create_user_path
= link_to 'click me', create_user_path, method: 'post'

How to setup a rails 3 route for a nested resource and custom controller action

I have a recommendation that is nested below Categories and Awards.
So Category/:id/awards/:id/recommendations/:id
I have an Assets model that handles paperclip attachments to the Recommendation. A Recommendation has_many Assets, Assets belong_to :recommendation
In my Recommendation new/edit views I am rendering a form partial (as is normal) that gives the user the option to upload several assets.
If there are Assets already related, then it lists them. I am working on setting up a custom delete action on the recommendations controller.
My current link_to:
<%= link_to "Delete Attachment",
{:controller => :recommendations, :action => :destroy_asset, :id => asset.id },
{:remote => "true", :confirm => "Are you sure you want to delete this image?"}
%>
My Controller action:
def destroy_asset
##recommendation = Recommendation.find(params[:id])
#asset = Asset.find(params[:id])
#asset.destroy
respond_to do |format|
format.js
end
end
routes:
resources :recommendations
resources :categories do
resources :awards do
resources :recommendations
end
end
I am still learning remote => true, and how to route this sucker. Not sure if I need to have the route nested or not. I tend to think not. Since I have an Asset.id in my loop, I should just be able to execute the destroy without needing the Recommendation at all.
So the question/s: do I need a route to access a custom action in my recommendations_controller?
thanks
Try this:
resources :categories do
resources :awards do
resources :recommendations
member do
get :destroy_asset
end
end
end
end
Or If you only want only destroy_asset action under recommendation controller, do something like this :
resources :recommendations
member do
get :destroy_asset
end
end
Or If you want custom match, do something like this :
match "asset/:id/destroy_asset", :to => "recommendations#destroy_asset",
:as=> "destroy_asset"

Form behavior posting nested resource

I am using Ruby on Rails 3 and I would like to change its conventional behavior on posting a form in order to post from a signup action to the create action instead that from the new action. That is, I would like to use the signup action instead of the (conventional) new action in my User controller and trigger the create action to save my model data that contains nested resources.
In my /config/routes.rb file I have:
resources :users do
collection do
get 'signup'
end
resource :profile
end
In my /app/controllers/users_controller.rb I have
class UsersController < ApplicationController
def signup
#signup_user = User.new(params[:user])
#signup_user.build_profile # NOTE: Nested resource
...
end
def create
...
#signup_user.save
respond_to do |format|
format.html { render :action => :signup } # signup.html.erb
end
end
end
In my /app/views/users/signup.html.erb file I have
<%= form_for #signup_user do |f| %>
...
<% end %>
My problem is that if I submit the above form, I will be redirected to the index action of the user controller and not to the create action I expect. It seams that the form posts only to the index action.
How can I solve the problem?
I tryed to use the following
<%= form_for( :user, #signup_user, :url => { :controller => "users", :action => "create" }, :html => { :method => :post } do |f| %>
but I have still the problem: I am redirected to the index action.
SOLUTION
The problem did seam to be in the routers.rb. The correct code was
resources :users do
collection do
get 'signup'
post 'create'
end
resource :profile
end

Resources