I am trying to send a :vote parameter of 'up' to my controller, so that it performs the voting function of current_user.vote_exclusively_for(#book). I am using the thumbs up gem.
I am trying to do this using link_to, and the correct parameters are showing up in my server output, but it is not working with the controller. I must be doing something wrong, but I am not sure what. Do i need to do something different with routes, other than books :resources?
This my vote action in books_controller
def vote
#book = Book.find(params[:id])
if params[:vote] == 'up'
current_user.vote_exclusively_for(#book)
end
redirect_to #book
end
And this is the link_to example in my view:
<%= link_to "Vote Up", :url => { :controller => "books", :action => "vote", :vote => "up"}, :method => :post %>
Any advice on where my attempts are breaking down would be greatly appreciated ( extra note: when i put the current_user.vote_exclusively_for(#book) function in my view it works) so I think this is a view/routes/link_to issue, not the function itself.
I don't understand your link_to. It seems to be missing the ID of the book it's voting on?
Make sure your routes.rb file looks like this:
resources :books do
post :vote, :on => :member
end
Then change your link_to function to this:
link_to "Vote Up", vote_book_path(#book, :vote => "up"), :method => :post
I just had a similar problem and solved it by using this style syntax:
<%= link_to "Vote Up", {:controller => "books", :action => :vote, :vote => "up" }, {:method => :post} %>
Also make sure your routes.rb has something similar to
resources books do
post :vote
end
Related
In my redmine plugin view, I've got this link:
<%= link_to "Add", :controller => "important_user", :action => "u_edit", :u_id => user.id, :p_id => #project.id, :method => :post %>
routes.rb:
resources :important_user do
collection do
post :u_edit
end
end
and controller:
class ImportantUserController < ApplicationController
def u_edit
puts 'edit!'
end
def index
puts 'ciao'
puts params[:p_id]
puts params[:u_id]
end
end
In spite of calling the expected u_edit action, clicking on the link calls the index method (I created in a second moment to avoid the AbstractController::ActionNotFound (The action 'index' could not be found for ImportantUserController) error). I've also tried using this sort of link:
<%= link_to 'Add', { :action => 'create', :u_id => user.id, :p_id => #project.id}, :method => :post %>
But it didn't work either, returning a projects?p_id=1&u_id=1 GET 404. How could I make it call the desired u_edit action?
I'm really really newbie in Ruby on Rails...
I'm trying to make a link to another page in my project, where it's listed the posts that belong to an escuela.
This is what I did:
In posts_controller.rb I wrote:
def postesc
#posts = Post.where(:escuela_id => params[:id])
respond_to do |format|
format.html # postesc.html.erb
format.json { render json: #posts }
end
end
In config/routes.rb I wrote:
match 'postesc' => 'posts#postesc'
In view/escuelas/listaesc.html.erb I wrote the link:
<%= link_to "Escuelas", :controller => "posts", :action => "postesc" %>
And in view/escuelas/postesc.html.erb I want to make a list of the matching posts.
But this page appears just blank, with only the layout.
Please, some help?
First make the association between post and escuela, then you can find it just by
Escuela.find(params[:id]).posts
Change your routes to -
resources :posts do
get 'postesc', :on => :collection
end
View :
<%= link_to "List posts", postesc_posts_path %>
make a change in routes.rb as
get 'postesc' => 'posts#postesc'
try...<%= link_to "Escuelas", postesc_path %>
OR
<%= link_to "Escuelas", { :controller => "posts", :action => "postesc" } %>
you're missing to add an ID for the Escuela to be selected - as you're doing in your Controller#postesc Action (as in words: where: escuela_id => params[:id]).
<%= link_to "Escuela", :controller => "posts", :action => "postesc", :id => 1 %>
but you could use the object-link method using the following syntax (by changing your routes a litte):
# in routes.rb
match 'postesc' => 'posts#postesc', on: :collection, as: 'esc_index'
# in your view
<%- for escuela in #escuelas do %>
<%= link_to "Escuela", esc_index(escueal) %>
<% end %>
I've read through the relevant Stack questions but still seem to be hitting a routing error with the following code:
Routes.rb
resources :memberships do
put :toggleon
put :toggleoff
end
Memberships_controller.rb
def toggleon
#membership = Membership.find(params[:id])
#membership.update_attributes(:active => true)
if user.id == membership_id
redirect_to root_path
else
redirect_to group
end
end
def toggleoff
#membership = Membership.find(params[:id])
#membership.update_attributes(:active => false)
if user.id == membership_id
redirect_to root_path
else
redirect_to group
end
end
Show.html.erb
<% if this_membership.active %>
<%= link_to 'Pause', this_membership, controller: :memberships, method: :toggleoff, style: 'color:#ccc' %>
<% else %>
<%= link_to 'Start', this_membership, controller: :memberships, method: :toggleon, style: 'color:green' %>
<% end %>
Error text
Started POST "/memberships/13" for 127.0.0.1 at 2011-09-27 23:35:35 +0100
ActionController::RoutingError (No route matches "/memberships/13"):
I can destroy memberships just fine but toggling the membership activity is proving tricky.
Thanks in advance!
Second attempt
<%= link_to("Pause", membership_toggle(#membership), :method => :put, :title => "This toggles it off") %>
<% else %>
<%= link_to("Toggle On", membership_toggle(#membership), :method => :put, :title => "This toggles it on") %>
and
def toggle
#membership = Membership.find(params[:id])
#membership.toggle!(active)
end
and
resources :memberships do
member do
put :toggle
end
end
now gives me this error...
undefined method `membership_toggle' for #<#<Class:0x00000102e69bc0>:0x00000102e66ec0>
Links using your routes are going to be:
link_to("Toggle On", membership_toggleon_url(membership), :method => :put, :title => "This toggles it on")
link_to("Toggle Off", membership_toggleoff_url(membership), :method => :put, :title => "This toggles it off")
You need to supply :method => :put to the link_to method given that's what you have the route as in your routes file.
Something like this can be achieved with a single action:
resources :memberships do
member do
put :toggle
end
end
Then you can make use of a single boolean field in your controller by using the Rails toggle method.
This will let you do something like:
#membership.toggle(:on)
Try setting the route as being :on => :member.
Also, there's an atomic toggle! method that does what its name implies.
Kept having issues with various techniques but eventually found this article:
http://buckybits.blogspot.com/2011/09/simple-ajax-property-toggle-in-rails-30.html
BOOM!
I am having a link like
<a href = '/messages/destroy/<%= #showmessage.id %>'>Delete Message</a>
I am rewriting this into
<%= link_to "Delete Message", destroy_message_path(:id => "1") %>
In my routes i have
map.resources :messages, :collection => { :destroy => :get }
And in my controller
def destroy
#message = Message.find(params[:id])
#message.destroy
redirect_to :action => 'index'
end
When I run the page, I am getting the error as:
undefined method `destroy_message_path' for #<ActionView::Base:0xb24a24c0>
How do I resolve this?
I'm not sure if you mean to be fighting against the Rails conventions here, but its alot easier to go along with them, so if you are ok with that you can do the following:
Remove this from your routes:
map.resources :messages, :collection => { :destroy => :get }
Change it to:
map.resources :messages
And use this link format instead:
<%= link_to "Delete Message", message_path(:id => "1"), :method => 'delete' %>
The URL for the destructive action will look like /messages/1, but the "method" portion of that link_to method will make Rails create a hidden form and perform a simulated "DELETE" action against the URL. Which is far more RESTful, and follows along with what Rails is expecting you to do.
With resources, you shouldn't be making a GET request, you should be making a DELETE request like this:
<%= link_to "Delete Message", destroy_message_path(:id => "1"),
:confirm => 'Are you sure?', :method => :delete %>
and in your routes file:
map.resources :messages
On another note, you can just pass in the object of the message to destroy_message_path, so for example:
<%= link_to "Delete Message", destroy_message_path(#message),
:confirm => 'Are you sure?', :method => :delete %>
This seems incredibly similar to a question I had answered just a few days ago, but the solution then isn't working now.
I'm building a rails app, and I am trying to have a button_to trigger a destroy in a different controller.
the code I have for the button is
<%= button_to "delete", :controller => :meals,
:action => 'destroy',
:recipe_id => recipe.id,
:method => :post >
when I click the delete button, i get a
'no matches for meals/3' which is the current meal_id.
the destroy in the meals controller looks like this
def destroy
#meal = Meal.where("current_user.id => ? AND recipe_id => ?", current_user.id, params[:recipe_id]).first
#meal.destroy
respond_to do |format|
format.html { redirect_to :controller => "user" , :action => "show" }
format.xml { head :ok }
end
end
it appears as though the button_to is completely ignoring the :action and requesting show which does not exist and shouldn't exist.
And how you part of routes.rb for that one looks like?
Because if you use map.resources then destroy has same path as show but :method => :delete(which is virtual verb implemented by form and _method=delete param).
Try this:
<%= button_to "delete", {:controller => :meals,
:action => 'destroy', :id => recipe.id }, :method => :delete %>
or if recipe is instance of Meal class then
<%= button_to "delete", #recipe, :method => :delete %>
Mind the curly brackets.
I know it is way too late for an answer but hope it may help somebody(using Rails 4).
<%= button_to "delete", meal_path(:id => recipe.id), :method => :delete %>