Rails 4: Delete button posting to the index action. - ruby-on-rails-4

I am not using JS turbo links in my Rails app for several important reasons. I am having problems with one delete button:
<% if current_user?(comment.user) %>
<%= link_to "delete", micropost_response_comment_path(#micropost, #response, comment), method: :delete, data: { confirm: "You sure?" }, :class => "btn btn-default btn-xs delete" %>
<% end %>
Here is the error - its posting to the index action of the comments_controller.
{"controller"=>"comments", "action"=>"index", "micropost_id"=>"impedit-ipsam-maxime-voluptatem-quis-vitae-perferendis-voluptatem-quia-minus-officia-dolorem-aut-placeat-tempora-earum-optio-quam-saepe-velit-871c1ab8-e282-441b-b610-4d2937c9aeef", "response_id"=>"38"}
All the other delete buttons and destroy functionality on my app works. I can't see any problem here and I am not sure what else to add - I am sure more info is needed but not sure what...
If you have a suggestion and need more info please let me know.
Many thanks

You can specify the controller and action as follows:
<% if current_user?(comment.user) %>
<%= link_to "delete", { controller: "comments", action: "delete", micropost: #micropost, response: #response, comment: comment}, data: { confirm: "You sure?" }, class: "btn btn-default btn-xs delete" %>
<% end %>
Otherwise, you may have a routing issue.

the error is not showing your 3rd argument: "comment".
That is missing.
Can you identify why?
Also the error itself is not displayed. Can you select more text and bring in here?
Is that error being raised before or after the "delete" action from the controller?
Has the record already been deleted and the controller is leading you to the index correctly?
What is the full error message?
What is your "delete" action in the controller?
Regards.

Ok so I found the error and its really a string of errors that one would not have thought of. Not sure if anyone can learn from this but here goes....
Comments belong to Response which belongs to Micropost.
I had
belongs_to :response, dependent: :destroy
in my comments model rather than the other way around, therefore as I destroyed a comment, its parent was also destroyed.
In my destroy method in the comments controller I had a redirect
format.html { redirect_to micropost_response_comments_path(#micropost, #response) }
after the destroy, which was now impossible because #response was always destroyed with it......
Calamity of errors. Thanks for your time guys...
Message is:
IF YOU HAVE STRANGE BEHAVIOURS OCCURRING WHEN DELETING OBJECTS, MAKE SURE YOUR RELATIONSHIPS FOR THAT MODEL, ITS PARENTS AND ITS CHILDREN ARE CORRECT.

Related

rails 4.2 link_to method: No route matches [POST]

I'm simply trying to construct a link_to with method: :delete to call the destroy method in my users controller:
<%= link_to 'disable token', user_path(user), method: :delete, data: { confirm: 'Are you sure?'} %>
Which generates HTML that looks like:
<a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/users/6">disable token</a>
My application.js file has:
//= require jquery
//= require jquery_ujs
And, in fact, i know the javascript is loaded and doing what it is supposed to, because it generates the 'Are you sure?` alert dialog.
However, when following the link I get the following:
No route matches [POST] "/users/9"
And indeed there is no such route, because my only routes are:
users_path GET /users(.:format) users#index
user_path DELETE /users/:id(.:format) users#destroy
The mystery (to me) is WHY is Rails doing a POST to this route in the first place? You'll note that the URL is correct ("/users/9") but that the HTTP verb is not: POST.
The parameters of the request are getting set:
{"_method"=>"delete",
"authenticity_token"=>"VcAVJF1/f9mwjNI4GPteRtDiyjKobnioF0hIQvF+3BVMzUnIoHymM2Z3w2sqSLJqJ11sZ/tIHt78aA9
}
Here you can see the _method key being set to delete as it should be, so why the routing error?!?
I'm stumped.
Edit: If i change my link_to call to include remote: true Rails routes it to the proper route! So, as a "fix" for this i've changed my controller to use UJS, but i don't like this because, well, as far as I can tell what i had before should work!
Here is the link_to call which sends a proper DELETE request as JS:
<%= link_to 'disable token', u, method: :delete, data: { confirm: 'Are you sure?'}, remote: true %>
Just try explicitly stating the controller action and method also (note #user.id is an example, you may be referencing it differently) with url_for:
<%= link_to 'disable token', url_for(action: :destroy, id: #user.id), method: :delete, data: { confirm: 'Are you sure?'} %>
My suspicion in this case is that there is some other javascript that is overriding the rails behavior.
I had the same issue (but with it getting a GET request, rather than POST). It turned out to a completely unrelated bit of JS that was too general in the selector, and was calling jquery's stopPropagation on all anchor tags surrounding a button, which was how my particular HTML was set up.

Rails 4 update attribute (belongs_to) with click on link ( within dropdown list on show page)

In my app I have Issue model, which belongs_to Status model. What I need to do is to have dropdown list of links (or spans, doesn't matter) with Statuses Id's on Issue show page, so I could change status clicking on this (could be non-ajax and ajax).
I am rather new to Rails, so I do not know how to implement this, was thinking of few ways to do this, but non seems to be working.
Because you're new, I'll outline what I'd do for you (hopefully it will help):
#config/routes.rb
resources :issues do
get "change_status/:status_id", to: "issues#change_status"
end
#app/models/status.rb
Class Status < ActiveRecord::Base
has_many :issues
scope :not, ->(id) { where('statuses.id != ?', id) }
end
#app/controllers/issues_controller.rb
def show
#issue = Issue.find params[:id]
#statuses = Status.all.not(#issue.id)
end
def change_status
issue = Issue.find params[:id]
issue.status = params[:status_id]
issue.save if issue.status_id_changed?
redirect_to issue
end
#app/views/issues/show.html.erb
<%= #statuses.each do |status| %>
<%= link_to status.title, issue_change_status_path(#issue, status) %>
<% end %>
There's obviously some stuff to explain - if you need me to do that, let me know and I'll give you the details!
You can simply use the link_to rails helper like in the following code:
link_to "Change Status", my_path_to_update_status, remote: true, method: :put
This will send an AJAX PUT request (thank to the remote: true) to your my_path_to_update_status.
Then, you just have to update the status in the action corresponding to the path specified in the link_to helper tag.
More information about the link_to rails helper here

error getting path returned rails 4

I'm trying to access my page gallery/new via a link, so i have created this
<%= link_to 'New gallery' new_gallery_path %>
rake routes gives
gallery_index_path GET /gallery(.:format) gallery#index
POST /gallery(.:format) gallery#create
new_gallery_path GET /gallery/new(.:format) gallery#new
edit_gallery_path GET /gallery/:id/edit(.:format) gallery#edit
gallery_path GET /gallery/:id(.:format) gallery#show
PATCH /gallery/:id(.:format) gallery#update
PUT /gallery/:id(.:format) gallery#update
DELETE /gallery/:id(.:format) gallery#destroy
and within my routes i have
resources :gallery
My view at gallery/new is
<%= nested_form_for #gallery, :html => { :multipart => true} do |f| %>
--content here
<% end %>
whenever i click the link to view this page i get
undefined method `galleries_path
Can someone point out my mistake, please?
You've chosen the wrong name for your resources. It should always be pluralized:
resources :galleries
From this Rails will generate the plural and singular paths correctly. galleries_path for the index, gallery_path for show, etc etc.

Rails: Create a new record with link_to

When the user clicks the link "New Week", a new record of the model week should be created and appended to the end of the list.
Here is my view weeks/index.html.erb
<h1> Weeks </h1>
<%= link_to "New Week", action: :create, method:"post" %>
<% #weeks.each do |week| %>
<ul>
<li> <%= link_to week.id, week %> </li>
</ul>
<% end %>
I am also not quite sure what to put in the create action for the weeks controller. This is what I have so far. The view above is routed to be the root.
def create
#week = Week.create(params[:week])
redirect_to root_path
end
When I click the link, it submits a POST action, but nothing else happens. A new record is not created. My debug(params) look like this.
--- !ruby/hash:ActionController::Parameters
method: post
action: index
controller: weeks
Update
I got it to work by doing
<%= link_to "New Week", { action: :create, :id => Week.new }, method:"post" %>
but I'm not sure if this is the correct way to go about it. Can anyone with expertise expand upon what is happening here?

Strong Parameters Issue - Undefined Method "model" for <Model>

I am having an issue that I can't nail down and the other related questions don't seem to ever encounter this issue. I have a Message model and I am trying to add a Message (I am writing this to test something with Faye). I encountered an issue with Rails 4 and strong parameters. I followed the steps in the documentation to fix it but I am getting this error:
NoMethodError: undefined method `message' for #Message:0x007fc081202968>
Here is my controller and the section of the documentation where it directs you to do strong parameters this way (http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters):
class MessagesController < ApplicationController
def index
#messages = Message.all
end
def create
#message = Message.create!(message_params)
end
private
def message_params
params.require(:message).permit(:content)
end
end
Here is my model, very bare bones at this point:
class Message < ActiveRecord::Base
validates_presence_of :message
end
This is my form that I am submitting it with:
<%= form_for Message.new, remote: true do |f| %>
<%= f.text_field :content %>
<%= f.submit "Post" %>
<% end %>
I cannot figure out where the method "message" is being called on the message model. It says it is in the create action, I go there and it links to the strong parameters private method. I don't see how that is call "message" as the method anywhere. I am at a loss. I did binding.pry and walked through it step by step but I can't find where it is calling it there and it still fails when I step through with binding.pry.
Any help would be appreciated. I am guessing it is something obvious that I am overlooking at this point.
EDIT: add link to docs and change language surrounding my use of binding.pry
Found the problem, and it was obvious and ridiculous. In the validation I put the :content field is :message.