I'm getting the following error when trying to destroy a user's vote on a "contribution":
No route matches {:action=>"destroy", :controller=>"contribution_votes",
:id=>#<ContributionVote contribution_id: 8245, user_id: 40>}
But have no idea why. Here is the button sending the request
<%= button_to "undo voteup!",
contribution.contribution_votes.find_by_user_id(current_user),
:method => :delete, :class => 'contribution_vote undo' %>
Here's the "destroy" action in the controller:
def destroy
#vote = current_user.contribution_votes.find(params[:id])
#vote.destroy
#contribution = Contribution.find_by_id(#vote.contribution_id)
#contribution_decrement = #contribution.decrement(:votes)
if #vote.destroy and #contribution_decrement.save
respond_to do |format|
format.html {redirect_to :back}
format.js
end
else
redirect_to :back
end
end
(some redundancy here I know, but it's to be filled in at a later date)
And here's the setup in routes.rb
resources :contribution_votes, :only => [:create, :destroy]
Can anyone help? I suspect the answer's obvious but I can't find it anywhere on SO.
Change your code in view to (I think it will help):
<%= button_to "undo voteup!",
contribution_votes_path(current_user.id),
:method => :delete, :class => 'contribution_vote undo' %>
.. by the way: type rake routes in your console and verify that you use right route path (I can mistake).
Related
I'm trying to be able to upvote using acts_as_votable without the page reloading but I'm not sure how to change the routes to make it work
Controller
def upvote
#video = Video.find(params[:id])
#ip = request.remote_ip
# Ipaddresstracker.delete_all
if Ipaddresstracker.find_by(ipaddress: #ip)
else
Ipaddresstracker.create(:ipaddress => #ip, :upvoted => true, :upvotedcount => 1)
#video.vote_by voter: User.first, :duplicate => true
end
# redirect_to :back
respond_to do |format|
format.html { redirect_to :back }
format.js { render layout: false }
end
end
View
<%= link_to like_video_path(x), method: :get, remote: true, class: "btn btn-default btn-sm" do %>
Route
resources :videos do
member do
put "like", to: "videos#upvote"
put "dislike", to: "videos#downvote"
end
end
Error
ActionController::RoutingError (No route matches [GET] "/videos/53/like"):
The route expects a put request. Change the :method option to :put
<%= link_to like_video_path(x), method: :put, remote: true, class: "btn btn-default btn-sm" do %>
Actually, if you had Jquery you can solve this problem by Jquery.
If not, try to change this format.js { render layout: false }. You need to render nothing so you can set render :nothing => true in your format.js block
I’m using Rails 4.2.3. I have this in my controller file, “./app/controllers/users_controller.rb” …
def edit
#user = User.find(session["user_id"])
render 'edit'
end
def update
#user = User.find(session["user_id"])
if #user.update_attributes(user_params)
flash[:success] = "Profile updated"
end
render 'edit'
end
And I have this in my “./app/views/users/edit.html.erb” file
<%= form_for(#user) do |f| %>
…
<%= button_to "Save", { :action => "update" }, :method => :post, :class => 'button' %>
But when I visit my url “http://localhost:3000/users/edit”, I get this error
No route matches {:action=>"update", :controller=>"users"}
This is what I have in routes/config.rb so I’m not sure why its falling apart …
get "users/edit" => "users#edit"
resources :users
I believe you need to use PUT/PATCH method for the update action, while POST is used for the create action as per the Rails documentation:
<%= button_to "Save", { :controller => "users", :id => #user.id }, :method => :put, :class => 'button' %>
Also, rake routes is a really useful command for debugging routing issues that dumps all defined routes.
Couldn't find Post without an ID
My routes:
get '/download_pdf(.:format)' => 'posts#show', :method => :get, :as=>:show
My link to create PDF on the show page:
<%= link_to "Create PDF document", show_path(#post, :format => :pdf) %>
My show action:
def show
#post = Post.friendly.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render :pdf => "show",:layout => 'pdf.html.erb',:template => '/post/show'
end
end
end
I have show.pdf.erb
What is wrong?
The params[:id] is empty in your show action. It's probably better to do this with resources in your routes.rb file and create a route that looks like:
get '/download_pdf/:id(.:format)'
Hope this helps you get in the right direction.
I have a show form and want to add a form to edit this so that a user can input a ticket id. I had initially amended this in the controller as an edit action but have since changed this to update. When i click on the submit button i get an error that says "No route matches [PUT]" and i feel a bit as though i may have missed a step somewhere.
edit.html.haml
= form_for [:admin, #diagnostic], url: edit_admin_diagnostic_path do |f|
.field
= label_tag :ticket_id
= text_field_tag "ticket_id"
= submit_tag "Submit", method: :put, data: { confirm: "Are you sure?" }
.field
= link_to 'show', admin_diagnostic_path
diagnostics_controller
class Admin::DiagnosticsController < Admin::BaseController
before_filter :diagnostic, only: [:show, :delete, :edit, :update]
def index
#diagnostics = DiagnosticInfo.all.order_by(:created_at.desc).page(params[:page]).per(50)
end
def show
respond_to do |format|
format.html
format.json { render json: #diagnostic }
end
end
def update
#diagnostic = DiagnosticInfo.find(params[:id])
if allowed.empty?
render action: "edit", error: 'Un-allowed attribute update request!'
elsif #diagnostic.update_attributes(allowed)
redirect_to admin_lessons_url, notice: 'Lesson was successfully updated.'
else
render action: "edit"
end
end
end
def destroy
diagnostic.destroy
redirect_to admin_diagnostics_path
end
relevant part of routes.rb
resources :diagnostics, only: [:index, :show, :destroy, :edit, :update] do
put 'ticket_id', on: :collection
end
As far as I know form argument should be:
= form_for [:admin, #diagnostic], url: admin_diagnostic_path(#diagnostic) do |f|
not edit_admin_diagnostic_path
edit_admin_diagnostic_path is going to get the edit page for admin_diagnostic while you are looking for put/patch
In your case you are trying to send put request to .../edit url which obviously doesn't exist hence this error.
I have a file reports/print.html.erb
in reports_controller
def print
#report = Report.find(params[:id])
respond_to do |format|
format.html { render :layout => false }
format.xml { render :xml => #report }
end
end
in routes.rb
match 'reports/print(:id)'
trying to call with
<%= link_to 'Print', report_print_path(:id => #report.id), :method => :put %>
and getting this error:
ActionController::RoutingError in Reports#show
No route matches {:action=>"print", :id=>23, :controller=>"report"}
Where am I going wrong?
Change your route to:
match 'reports/print/:id' => 'controller#print', :via => :put
That may fix it (didn't test the code though, and change the 'controller#print' part to your actual controller name.
made it work with
<%= link_to 'Print', print_url(:id => #report.id) %>
and
match 'print/(:id)' => 'reports#print', :via => :get, :as => :print
No idea why it was giving me problems, there's 4 hours of my life i'll never get back.