Rails: routing update error - ruby-on-rails

I am getting a routing error: No route matches "/deccom_tasks/update/1" with {:method=>:put}
I'm not sure why I am getting a routing error for a route that usually works.
Route:
map.resources :decom_tasks, :collection => {:sort => :post, :deactivate_task => :get, :reactivate_task => :get}
Controller:
def update
#task = Task.find(params[:id])
respond_to do |format|
if #task.update_attributes(params[:task])
format.html { redirect_to(#task, :notice => 'Task was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #task.errors, :status => :unprocessable_entity }
end
end
end

Mark Thomas was right, syntax error

Related

Create a new Record from link_to results in urlgenerationerror

I am trying to create a record in a table direcly when the user clicks on a link.
However it is generating the following error when it tries to load the view.
ActionController::UrlGenerationError in SopHeaders#show
No route matches {:action=>"create", :company_id=>2, :controller=>"sop_detail", :cost=>0.1e2, :cost_sop=>0.103e2, :customer_id=>1, :desc1=>"Printer cable YDP30 - Secura 224 ICEU", :flag_required=>true, :id=>"4", :list_price=>0.0, :opportunity_id=>"9", :product_code=>"01-69Y03293", :quantity=>1, :sophead_id=>4, :unit_price=>0.0}
My view
<%= link_to 'Add to Quote', {:controller => "sop_details",
:action => "create",
:sophead_id => #sop_header.id,
:customer_id => #customer.id,
:company_id => #customer.company_id,
:product_code => stock.product_code,
:unit_price => stock.price,
:quantity => 1,
:cost => stock.cost,
:cost_sop => stock.cost_sop,
:list_price => stock.list_price,
:flag_required => true,
:desc1 => stock.desc1},
:method => "post" %>
My Controller
def new
#sop_detail = SopDetail.new
end
def create
respond_to do |format|
if #sop_detail.save
format.html { redirect_to customer_opportunity_sop_header_path(#customer, #opportunity, #sop_header) }
format.json { render :show, status: :created, location: #sop_header }
else
format.html { render :new }
format.json { render json: #sop_header.errors, status: :unprocessable_entity }
end
end
end
My route table output
customer_opportunity_sop_header_sop_details GET /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details(.:format) sop_details#index
POST /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details(.:format) sop_details#create
new_customer_opportunity_sop_header_sop_detail GET /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details/new(.:format) sop_details#new
edit_customer_opportunity_sop_header_sop_detail GET /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details/:id/edit(.:format) sop_details#edit
customer_opportunity_sop_header_sop_detail GET /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details/:id(.:format) sop_details#show
PATCH /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details/:id(.:format) sop_details#update
PUT /customers/:customer_id/opportunities/:opportunity_id/sop_headers/:sop_header_id/sop_details/:id(.:format) sop_details#update
What am I doing wrong?
Your redirect_to path is not in your routes. You want to go to the customer_opportunity_sop_header_path path while this is not in your routes. I guess you want to navigate to the customer_opportunity_sop_header_sop_details_path.

Add default table field when creating new obect Ruby on Rails 3

My models are: Projects has_many Feeds. I just added a column to my Feeds table called feed_error. I currently have a form on the app that creates a new Feed when entered. I want to be able to set feed_error to false by default. In my feeds_controller, I have my create method:
def create
#feed = Project.find(params[:project_id]).feeds.build(params[:feed])
respond_to do |format|
if #feed.save
format.html { redirect_to( :back, :notice => 'Feed was successfully created.') }
format.xml { render :xml => #feed, :status => :created, :location => [#feed.project, #feed] }
else
format.html { render :action => "new" }
format.xml { render :xml => #feed.errors, :status => :unprocessable_entity }
end
end
end
I was thinking I could try adding :feed_error => 'false' to the params, but that doesn't seem to work. How do I set this field by default?
You have a couple options. In your controller you can do:
def create
#feed = Project.find(params[:project_id]).feeds.build(params[:feed])
#feed.feed_error = false
respond_to do |format|
if #feed.save
format.html { redirect_to( :back, :notice => 'Feed was successfully created.') }
format.xml { render :xml => #feed, :status => :created, :location => [#feed.project, #feed] }
else
format.html { render :action => "new" }
format.xml { render :xml => #feed.errors, :status => :unprocessable_entity }
end
end
end
You could also set this up in your database migration. For example, if you don't need a null value and instead want the default to be false you can add:
t.boolean "feed_error", :null => false
to your migration.

The action could not be found for controller

Having gone through my code I have a separate problem from my original question and rather than writing a new question. I will leave the old part at the bottom of this and post the new problem here. I do this because they are closely related.
New:
Im getting an error message saying
Unknown action
The action 'response' could not be found for CrawlerController
I'll keep it simple but the code for model, controller and routes are below in the previous question.
A basic run down is response is a def within CrawlerController as is add_Request.
The routes are matched as such:
match "/requests/new" => "crawler#add_Request"
match 'requests/:id' => 'crawler#response'
Here is controller code as per user request:
class CrawlerController < ApplicationController
def add_Request
#request = Request.new(params[:request])
respond_to do |format|
if #request.save
format.html { redirect_to(#request, :notice => 'Request was successfully created.') }
format.xml { render :xml => #request, :status => :created, :location => #request }
else
format.html { render :action => "new" }
format.xml { render :xml => #request.errors, :status => :unprocessable_entity }
end
end
end
def response
#request = Request.find(params[:id])
respond_to do |format|
format.html
format.js { render :json => #request }
end
end
def show
#request = Request.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #request }
format.json{
render :json => #request.to_json
}
end
end
end
please recheck code of controller as I can see it
class CrawlerController < ApplicationController
def add_Request
#request = Request.new(params[:request])
respond_to do |format|
if #request.save
format.html { redirect_to(#request, :notice => 'Request was successfully created.') }
format.xml { render :xml => #request, :status => :created, :location => #request }
else
format.html { render :action => "new" }
format.xml { render :xml => #request.errors, :status => :unprocessable_entity }
end
end
def response
#request = Request.find(params[:id])
respond_to do |format|
format.json {render :#request.to_json}
end
end
so one end is missing an your response action is defined inside add_Request

Why do I get No Route Error in Rails 3 even though I have defined a route?

This is the error:
ActionView::Template::Error (No route matches {:action=>"send_to_client", :controller=>"stages"}):
app/views/stages/_show.html.erb:13:in `_app_views_stages__show_html_erb__3606907191157577988_2203521120_617355097038007272'
app/controllers/stages_controller.rb:78:in `block (2 levels) in create'
app/controllers/stages_controller.rb:75:in `create'
This is line 13 in the _show.html.erb:
<%= link_to "<span class='icon send-to-client-icon' title='Send to Client'> </span>".html_safe, send_to_client_stage_path, :id => stage.id, :confirm => "This will send #{stage.name.capitalize} to #{stage.client.email}. Are you sure you are ready?" %>
This is my stages controller, create action:
def create
#stage = current_user.stages.create(params[:stage])
client = #stage.client
unless #stage.new_record?
respond_with(#stage, :status => :created, :location => #stage) do |format|
flash.now[:notice] = 'Stage was successfully created.'
format.html { redirect_to(#stage) }
format.js { render :partial => "stages/show", :locals => {:stage => #stage, :client => client}, :layout => false, :status => :created }
end
else
respond_with(#stage.errors, :status => :unprocessable_entity) do |format|
format.js { render :json => #stage.errors, :layout => false, :status => :unprocessable_entity }
format.html { render :action => "new" }
end
end
end
Stages Controller, send_to_client action:
def send_to_client
stage = Stage.find(params[:id])
ClientMailer.send_stage(stage).deliver
if ClientMailer.send_stage(stage).deliver
flash[:notice] = "Successfully sent to client."
redirect_to("/")
else
flash[:notice] = "There were problems, please try re-sending."
redirect_to("/")
end
end
You are missing the ID and need to pass the stage instance it in your send_to_client_stage_path call.
send_to_client_stage_path(stage)
instead of:
send_to_client_stage_path

How do I set up my #product=Product.find(params[:id]) to have a product_url?

Trying to recreate { script/generate scaffold }, and I've gotten thru a number of Rails basics. I suspect that I need to configure default product url somewhere. But where do I do this?
Setup:
Have: def edit { #product=Product.find(params[:id]) }
Have edit.html.erb, with an edit form posting to action => :create
Have def create { ... }, with the code redirect_to(#product, ...)
Getting error: undefined method `product_url' for #< ProductsController:0x56102b0>
My def update:
def update
#product = Product.find(params[:id])
respond_to do |format|
if #product.update_attributes(params[:product])
format.html { redirect_to(#product, :notice => 'Product was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #product.errors, :status => :unprocessable_entity }
end
end
end
Ah, add in /config/routes.rb the line:
map.resources :products
and make sure you put that above the default:
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
This defines a system for giving :product's urls.

Resources