undefined method on Passing Id for reference table - ruby-on-rails

I have two tables: client and client info. I am displaying client info on the client show page. Now I am trying to delete a specific client info in my client show page. I have:
<%= link_to "Delete", [ #client.id, #client.client_info ],
:confirm => 'Are you sure?',
:method => :delete,
:class => "btn"
%>
In my controller I am doing:
def destroy
#contact_info = ClientInfo.find(params[:id])
#contact_info.destroy
respond_to do |format|
format.html { redirect_to #client }
format.json { head :ok }
end
I am getting an error saying undefined method.

Related

Delete method in ROR route error when using remote true

I'm trying to delete post with the remote: true command in rails. Everything is working fine when i'm not using ajax. But when i use the remote true command i get an routing error.
view:
<% #posts.each do |post| %>
<%= post.title %>
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
<% end %>
controller:
def index
#posts = Post.all
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to :back
end
routes:
resources :posts
This is the error i get in the log.
Started DELETE "/posts" for 127.0.0.1 at 2014-10-11 12:46:33 +0200
ActionController::RoutingError (No route matches [DELETE] "/posts"):
Thanks in advance.
Update. I get this when i write rake routes.
DELETE /posts/:id(.:format) posts#destroy
To get this to working.
Change:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
redirect_to :back
end
To this:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.js { head :no_content }
end
end
and:
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
to:
<%= link_to 'delete', post, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
Incase anyone is having any problems with this, I managed to solve it by adding :data => { :type => :json } to the link.
For example:
<%= link_to "Delete this post", #post, :method => :delete, :remote => true, :data => { :type => :json } %>
I imagine UJS is expecting HTML, however JSON is returned so it fails and posts again (I noticed two requests).
Hope this helps!

RoR Empty Cart/Delete line_Items

I'm still pretty new to RoR and have run into a problem I can't seem to solve. I've already built the functions to add new products to the database and allow a user to add individual items to their cart, my delete method just gives me an error saying it can't find the delete method in the carts_controller. Any help would be appreciated.
\app\views\cart\show.html.erb
<%= button_to 'Empty Cart', #cart, method: :delete,
data: { confirm: 'are you sure?'} %>
\app\controllers\carts_controller.rb
def destroy
#cart.destroy if #cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_url,
notice: 'Your cart is empty' }
format.json { head :no_content }
end
end
private
def set_cart
#cart = Cart.find(params[:id])
end
Error Message Given
Unknown Action
The action 'destroy' could not be found for CartsController
\config\routes.rb
Depot::Application.routes.draw do
root 'store#index', as: 'store'
resources :line_items
resources :carts
get "store/index"
resources :products
end
Depends on what is currently set in your #cart variable, can you put out an inspect for that?
Otherwise, you can also try something like this:
<%= link_to 'Empty Cart', cart_path(#cart), method: :delete, confirm: 'are you sure?' %>

Routing error with confirmed (seemingly) existing route

I'm copying what seems to be identical logic, but it doesn't work with one of my models.
In surveys, I have
View
<% #surveys.each do |survey| %>
...
<%= link_to 'Delete', survey, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
Controller
def destroy
#survey = Survey.find(params[:id])
#survey.destroy
respond_to do |format|
format.html { redirect to '/' }
format.json { head :no_content }
end
end
The delete function works fine.
Yet in question, I have
View
<% #questions.each do |question| %>
...
<%= link_to 'Delete', question, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
Controller
def destroy
#survey = Survey.find(params[:survey_id])
#question = Question.find(params[:id])
#question.destroy
respond_to do |format|
format.html { redirect to #survey }
format.json { head :no_content }
end
end
This gives me the error:
undefined method `question path' for #<#<Class:0x008ff2534....
When I remove the link_to, it works just fine retrieving question and its properties.
Changing the logic in my view to something more specific,
<%= link_to "Delete", :controller => "questions", :action => "destroy", :id => question.id %>
I get a more specific error.
No route matches {:controller=>"questions", :action=>"destroy", :id=>1}
Running rake routes, this confirms the path exists.
DELETE /surveys/:survey_id/questions/:id(.:format) questions#destroy
And here's my routes.rb entry:
devise_for :users do
resources :surveys do
resources :questions do
resources :responses
end
end
end
Computers don't make mistakes, so what did I do wrong?
questions are nested resources, so you should also pass survey to the path:
<%= link_to 'Delete', [#survey, question], :confirm => 'Are you sure?', :method => :delete %>
Assuming that you have set #survey variable.
Question is a nested resource under Survey so your route needs to reflect that. Note that in the rake routes output there's a :survey_id parameter as part of the route. It is required. Therefore your link needs to look like this:
<%= link_to "Delete", :controller => "questions", :action => "destroy", :survey_id => #survey.id, :id => question.id %>
Alternatively you can use Marek's path, namespacing the question resource:
<%= link_to 'Delete', [#survey, question], :confirm => 'Are you sure?', :method => :delete %>

Rails dynamic increasing button

I have to do an increasing value button "bg" for p1 entry in table game but it is not working... It save the game with p1 = 0 but the link_to has no effect..
Many thanks for any help!!! (And sorry for my bad English...)
controller
def create
#game = Game.new(params[:game])
#game.p1 = 0
respond_to do |format|
if #game.save
format.html { render action: "show" }
end
end
end
def show
#game = Game.find(params[:id])
respond_to do |format|
format.js {render 'hnew'}
end
end
def add_three_points
#game = Game.find(params[:id])
#game.update_attribute(:p1, #game.p1 + 3)
end
views
show.html.erb
<%= render 'hnew' %>
hnew.js.erb
$('#ajax_hidden').html("<%= escape_javascript(render('hnew')) %>");
_hnew.html.erb
<%= link_to "bg", add_three_points_path(#game.id), {remote: true, method: :put},
class: 'btn btn-small' %>
<%= #game.p1 %>
error message:
Started PUT "/add_three_points.14" for 127.0.0.1 at 2013-07-10 21:27:01 +0200
Processing by GamesController#add_three_points as
Completed 404 Not Found in 1ms
ActiveRecord::RecordNotFound (Couldn't find Game without an ID):
app/controllers/games_controller.rb:86:in `add_three_points'
The :method option of link_to is for the HTTP verb (:get, :post, :put or :delete)
What you want to do is below (using the :put method, preferred verb for updates)
<%= link_to "bg", add_three_points_path(#game), {remote: true, method: :put}, class: 'btn btn-small' %>
and in routes.rb:
put 'add_three_points' => 'games#add_three_points', :as => :add_three_points

Rails does not display error messages on a form in a custom method

I've created a custom method called checkout in my app. I create an order (which is done my adding products to my "cart"), assign it to my client, and then I head to my checkout screen where I confirm the items and enter their customer order number and complete the order (submit).
Everything works great except that it doesn't display error messages. I'm able to display a flash error notice (seen in complete_order method) when things go wrong but it doesn't specify the details like a normal form would. The error messages should appear if the customer order number is not unique for that client.
Below is the custom method (checkout) related code.
Order Model:
validates_uniqueness_of :customer_order_number, :scope => :client_id
Orders_controller:
def checkout
#order = current_order
end
def complete_order
#order = current_order
respond_to do |format|
if #order.update_attributes(params[:order])
#order.complete #sets submitted datetime and state to 'complete'
flash[:notice] = 'Thank you! Your order is being processed.'
format.html { redirect_to( products_path ) }
format.xml { head :ok }
else
flash[:error] = 'Please review your items' #added to confirm an error is present
format.html { redirect_to( checkout_path ) }
format.xml { render :xml => #order.errors, :status => :unprocessable_entity }
end
end
end
And the form in the checkout view:
<% form_for #order, :url => { :controller => "orders", :action => "complete_order" } do |f| %>
<%= f.error_messages %>
<%= f.text_field :customer_order_number, :label => "Purchase Order Number" %>
<p>
<%= f.submit 'Complete Order', :confirm => 'Are you sure?' %> <small> or <%= link_to 'cancel', current_cart_path %></small>
</p>
<% end %>
Any idea how I can display the specific error messages?
Change redirect_to to render in else condition otherwise checkout method get called again & no error will displayed.
else
format.html { render :action => 'checkout' }

Resources