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 %>
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?
Having a bit of trouble with the following ruby on rails code snippet - I have books and would like to update a book's name, here is how I try to do it:
<h1>Edit the book "<%= #book.name %>"</h1>
<%= form_tag action: :update, id: #book do %>
<p>
<label for="book_name">Name</label>
<%= text_field :book, :name %>
<%= submit_tag 'Save changes' %>
</p>
<% end %>
This is what I do in the books controller:
def edit
#book = Book.find(params[:id])
end
def update
#book = Book.find(params[:id])
if #book.update_attributes(params[:book])
redirect_to :action => 'show', id => #book
else
#subjects = Subject.find(:all)
render :action => 'edit'
end
end
These are my routes:
root to: 'authors#index'
resources :books, :authors
When I click the submit button in the form, it gives me No route matches [POST] "/books/5" and directs to localhost:3000/books/5 instead of staying and localhost:3000/books/5/edit
What am I doing wrong here? Shouldn't there be a put method for updating stuff somewhere rather than a post method?
Updates should use put not post.
<%= form_tag( { :controller => :books, :action => :update, :id => #book.id }, { :method => :put } ) do %>
or better yet use form_for
<%= form_for #book do |f| %>
On your console run "rake routes" and it will print out all available routes.
Please try this:
We need to specify match in routes file.
match "/books/:id" => "books#update"
resources :books should do the job. you dont have to explicitly use "match".
def edit
#book = Book.find(params[:id])
end
form.html
form_for #book, :method => :put do |f|
def update
#book = Book.find(params[:id])
#book.update_attributes(params[:book])
end
this should do the job.
I had this issue before. Everything was right but still getting the error then I found out it was
gem 'rails-api'
Removed it and it all worked fine.
I am trying to follow the tutorial of making a twitter clone in ruby on rails (http://www.youtube.com/watch?v=oXr1jAsBlPI&feature=relmfu). Unfortunately for me he is doing it in Rails 2.x.x and I am having some trouble keeping up. I am currently 41:34 when he is defining the paths in routes.rb.
I am getting this problem when I enter my 'show' page:
"Routing Error No route matches {:action=>"show",
:controller=>"toggle_follow"} Try running rake routes for more
information on available routes."
This is what I have in the relevant files:
routes.rb
match '/:username', :controller => 'home', :action => 'show'
match '/:username/toggle_follow', :controller => 'home', :action => 'toggle_follow'
show.html.rb
<% if current_user.is_friend? #user %>
<%= submit_tag "Following", :class => "button" %>
<% else %>
<%= submit_tag "Stop following", :class => "button" %>
<% end %>
home_controller.rb
def show
#user = User.find_by_username(params[:username])
#flits = #user.all_flits
end
def toggle_follow
#user = User.find_by_username(params[:username])
if current_user.is_friend? #user
flash[:notice] = "You are no longer following ##{#user.username}"
current_user.remove_friend(#user)
else
current_user.add_friend(#user)
flash[:notice] = "You are following ##{#user.username}"
end
redirect_to user_flits_path(#user.username)
end
...
Thanks in advance
Ok I solved it...
in routes you have to enter:
routes.db
match '/:username', :to => 'home#show', :as => 'user_flits'
match '/:username/toggle_follow', :to => 'home#toggle_follow', :as => 'toggle_follow'
My setup: Rails 3.0.9, Ruby 1.9.2
I added a custom action to a nested resource task.
routes.rb
resources :tasks
resources :projects do
resources :tasks, :constraints => { :protocol => "http" } do
put :cancel, :on => :member
end
end
rake routes shows
cancel_project_task PUT /projects/:task_id/tasks/:id/cancel(.:format) {:protocol=>"http", :action=>"cancel", :controller=>"tasks"}
In my controller,
tasks_controller.rb
def cancel
#task = Task.find(params[:id])
respond_to do |format|
if #task.cancel
format.html { redirect_to(#task, :notice => 'Task was successfully canceled.') }
else
format.html { render :action => "edit" }
end
end
end
I need to define a form to perform the action, here's what I have currently
_form.html.erb for subscription
<%= form_for [#project, #task], :url => { :action => 'cancel' } do |f| %>
<%= f.submit "Cancel your task"%>
<% end %>
It throws an error
No route matches {:action=>"cancel", :controller=>"tasks"}
I also tried adding :method => "put" with the same error
_form.html.erb for subscription
<%= form_for [#project, #task], :url => { :action => 'cancel', :method => "put" } do |f| %>
<%= f.submit "Cancel your task"%>
<% end %>
Anyone knows the correct form_format syntax to accomplish this?
In case anyone wants the answer, here's what I had to do
<%= form_for [#project, #task], :url => cancel_project_task_path(#project, #task) do |f| %>
It took me way too long to figure this out, hopefully this helps the next unsuspecting developer.
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