Custom Controller action not working - ruby-on-rails

Hoping someone can point out the error of my ways here. I've got a form with a custom action with a standard submit button:
= form_for( #property, :html => {:data => {:abide => ''}, :id => 'property-edit-form'},:url => url_for(:action => 'update_promo')) do |f|
The route is as follows:
post 'properties/update_promo', :as => 'update_promo'
The controller action is:
def update_promo
#property = Property.find(params[:id])
if #property.update(property_params)
respond_to do |format|
format.html
format.js
end
else
render 'edit'
end
end
The problem is that it's still calling the default Update action. And I get the error:
Couldn't find Property with 'id'=update_promo
Can anyone help with this please?
Full routes...
get 'users/index'
get 'home/index'
get 'properties/update_regions', :as => 'update_regions'
get 'properties/update_places', :as => 'update_places'
get 'properties/update_map', :as => 'update_map'
get 'properties/update_promo', :as => 'update_promo'
root 'home#index'
post "versions/:id/revert" => "versions#revert", :as => "revert_version"
resources :properties
resources :users do
collection do
get :properties # add this line
end
end

Update your routes.rb file's line:
get 'properties/update_promo', :as => 'update_promo'
to this:
post 'properties/update_promo', :as => 'update_promo'
Also, you can change your form to look like this:
= form_for( #property, :html => {:data => {:abide => ''}, :id => 'property-edit-form'},:url => update_promo_path)) do |f|
You can use the url helpers instead of url_for(:action => ...).

You are probably GET methoding the submit so the show method is being called with the id update_promo, try adding
:method => :post
to the form_for.
To check, look at the log when you submit,
if your log starts with
Started GET
and not
Started POST
you need to add said paramter to form_for

Related

RAILS: Rendering views from the same controller based on the route

I have two routes (defined in my config/routes.rb)
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report
I would like to know how I can tell my controller to render the view based on my route, without adding a parameter on my URL.
Based on this Question&Answer I Managed to get the result I want with
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare, :defaults => { :render => 'compare' }
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report, :defaults => { :render => 'full_report' }
And in my controller I defined my action as:
def search_by_id
render :action => (params[:render] || "full_report")
end
But is this a Good Practice or is there a better way to do it?
Instead of creating different routes for each category you are making for simplifying you can write it like:
# config/routes.rb
get ":category", to: "front_office#search_by_id", as: 'front_office', constraints: {category: /(compare|full_report)/}
the above routes looks for /compare and /full_report and this will call search_by_id action in front_office controller.
then inside the controller do as follows:
def search_by_id
render params[:category]
end
params[:category] will hold the slug values which we passed through the URL

No route matches [PUT] error in Rails 4.2.6

I am facing this error while running my application:
No route matches [PUT] "/accounts/1/payment"
This is my form syntax;
<%= semantic_form_for(#account, :url => payment_path, :html => { :method => :put }) do |f| %>
......
<% end %>
routes.rb:
get "/accounts/:id/payment" => "accounts#payment", :as => "payment"
controller method:
def billing
#account = Account.find(params[:id])
if request.put?
// some code here
end
end
Replace
get "/accounts/:id/payment" => "accounts#payment", :as => "payment"
with
match "/accounts/:id/payment", to: "accounts#payment", :as => "payment", via: [:get, :put]
and let me know if this works.
EDIT
If your controller action is billing, then you may have to do
match "/accounts/:id/payment", to: "accounts#billing", :as => "payment", via: [:get, :put]
your form is trying to find a route matches method put while your route has a get method.
Use put in routes instead of get
put "/accounts/:id/payment" => "accounts#payment", :as => "payment"
In your form you are telling this form to be treated as PUT request but your routes says it is GET request. You should use same request type in both routes.rb and your form definition.
Try this
put "/accounts/:id/payment" => "accounts#billing", :as => "payment"

How to add ScriptTag on shopify_api gem?

Hi Everyone,
I came across on a problem that I can't really figure out myself, It believe that all your expertise can help me through solving this error I get whenever I try to access a route to add a script. Here is my controller code:
class HomeController < AuthenticatedController
def index
#products = ShopifyAPI::Product.find(:all, :params => {:product_type => "Underarmour"})
# script = ShopifyAPI::ScriptTag.new(:all, :params => {:event => "onload", :src => "https://shopperapproved.herokuapp.com/sajs/14043.js"})
end
def script
ShopifyAPI::ScriptTag.create(:event => "onload", :src => "https://shopperapproved.herokuapp.com/sajs/14043.js")
end
end
and my route file is:
controller :sessions do
get 'login' => :new, :as => :login
post 'login' => :create, :as => :authenticate
get 'auth/shopify/callback' => :callback
get 'logout' => :destroy, :as => :logout
end
root :to => 'home#index'
match "script/",
:to => "home#script",
:via => :get
I want to add a script by accessing this route: on my index view:
<h3>Add your ShopperApproved site ID:</h3>
https://shopperapproved.herokuapp.com/script --> if i am going to click this link i will be redirected to HomeController#script
I hope you can help me..
Have you set up a seesion & token? i fnot you cant create the script in your store
https://github.com/Shopify/shopify_api
I assume you did not set up a session so you cant "connect" to your store.
Verify that you have a valide session and it will work

Is it possible to change the name of controller in URL in Rails

I have a controller
emppedes
Its fine when i want to call this in url i see
localhost:3000/emppedes
and whole the content related to it appears.
But can i match this controller to others name in rails so that when i click in it I can get in URL like
localhost:3000/employees
without changing the controller inside but outside in url you can see that.
I mean can i code somewhere like
`emppedes will display as employees in URL.`
Is it possible. May be my question is stupid but I want to know whether it is possible or not.
In edit i have two send two different id and in controller also for update i have to send two id.
For edit my code is like
<%= link_to 'Edit', { :action => "edit",:id => #emppede.id,:ad => #id},:class=>"btn btn-success" %> |
since i have two handle two id in controller
Also for update
#id= #emppede.ad
if #emppede.save
format.html { redirect_to :action => :index, :id => #emppede.ad }
format.json { render json: #emppede, status: :created, location: #emppede }
How can i send two params :id and :ad both in rails path formate?
Since my controller is like this
def index
#id = params[:id]
#data = Emppede.where(:ad => #id)
if #data.count > 0
#data.each do |data|
#ids= data.id
end
redirect_to :action => :show, :id => #ids, :ad => #id
else
redirect_to :action => :new, :id => #id
end
end
Yes, you can just specify the controller to use for any given resource:
resources :employees, :controller => "emppedes"
Or if you prefer the Ruby 1.9 syntax:
resources :employees, controller: "emppedes"
For more details, see Rails Guide: Routing.
What you will want to do is pass in the :path option
resources :employees, :path => "emppedes"
This replace all your route references with /employees to /emppedes
See: http://guides.rubyonrails.org/routing.html, Section 4.7 Translating Paths
You can use the following in routes.rb for your queries
match '/employees/:id/:ad(.:format)' => 'emppedes#index', :via => :get, :as => :employees
resources :emppedes, :as => 'employees'
try use inflections in your initialize file
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'employees', 'emppedes'
end

What is the method for this route

In routes.rb I have
map.resources :groups, :collection => { :find_or_create => :get, :search => :get } do |group|
group.resources :recruitment_periods, :controller => 'groups/recruitment_periods' do |period|
period.resources :recruits, :controller => 'groups/recruitment_periods/recruits'
if I wanted to redirect to the show action of a specific group,recruit_period, recruit what would the path be?
i.e. redirect_to groups_recruitment_period_recruit_path(x,y,z)
I think you almost had it. It looks like it should be:
redirect_to group_recruitment_period_recruit_path(x,y,z)
No plural on group since you know which one.

Resources