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
Related
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.
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!
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 %>
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
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).