Rails: Routing error - ruby-on-rails

I have a button_to tag in my show.html.erb file.
<%= link_to 'Click HERE to open file', #user.image.url %><br/><br/><br/>
<%= label_tag(:q, "Parse CSV File:") %><br/>
<%= button_to 'Parse CSV', {:controller => "users_controller", :action => "process" } %>
<% end %>
Then I have this added to my users_controller.rb file
# GET /users/1/process
def process
puts 'To be Implemented'
end
Im getting an error in the routing file
No route matches [POST] "/assets"
This is how my routing file looks:
resources :users
resources :listings
What should I change. Im a bit confused, woould really appreciate some help.

Please correct your route and define like this
<%= button_to 'Parse CSV', {:controller => "users", :action => "process" } %>
Then in route file
resources :users do
collection do
get: process
end
end
It will sure work

1) In view, use the controller name like 'users', not the 'users_controller' .
<%= button_to 'Parse CSV', {:controller => "users", :action => "process" } %>
2) rails define few routes by default , but for other you need to define yourself .
Declare the routes like :
resources :users do
:member => {
:process => :get
}
end
Hope that help .

Related

How to pass an ID/parameter to ROR controller

In view I have a link as follow in "teacher" controller. this link supposed to send an id to another controller "teacher_details" (1 to 1 relationship). This link open a webpage to add more details about teacher.
<%= link_to 'Add details', new_teacher_detail_path(#teacher), :id => "add_detail_link" %>
My controller code is
private
def set_teacher
#teachers = Teacher.find(params[:id])
end
When I run this code it shows me an error that "cannot find with out an ID". What am I doing wrong. The link does not passing the id parameter properly.
Route file is
root 'sessions#login'
get 'homes/home'
get '/login' => "sessions#login", :as => "login"
get '/logout' => "sessions#logout", :as => "logout"
get '/homes' => "homes#home"
resources :users
resources :sessions
resources :homes
resources :teachers
resources :teacher_details
resources :profiles
It seems like you are not nesting TeacherDetail with Teacher model in routing.
That's why it not getting any id parameter in new_teacher_detail_path. So you can't find params[:id] in below action.
private
def set_teacher
#teachers = Teacher.find(params[:id])
end)
Try change Your link_to as below :
<%= link_to 'Add details', new_teacher_detail_path(:id => #teacher.id), :id => "add_detail_link" %>
Now you will get ID parameter properly.
Routes file for teacher and details should be like this:
resources :teachers do
get 'details/new', on: :member
end
Then in view you can use link_to like this
<%= link_to 'Add details', details_new_teacher(#teacher) %>
No need to change in controller
In one-to-one relationship teacher_detail has foreign_key as teacher_id, So you can use like this
<%= link_to 'Add details', new_teacher_detail_path(id: #teacher.id), :id => "add_detail_link" %>

Controller action doesn't run with link_to function

I need to call a simple controller action through my view using link_to.
In preferences > index.html.erb:
<%= link_to "My link", :controller =>
:preferences, :action => :produces_text %>
Note, I have also tried index.html.erb with this format with no luck:
<%= link_to "My link", {:controller =>
:preferences, :action => :produces_text } %>
In preferences_controller.rb:
def produces_text
puts "test"
redirect_to preferences_url
end
In routes:
resources :preferences do
member do
get 'produces_text'
end
end
"test" is not produced in the terminal when I click "My link" and I am not redirected to preferences_url either.
resources :preferences do
collection do
get 'produces_text', as: :produces_text
end
end
<%= link_to "Link", produces_text_preferences_path %>

Making a link in Ruby on Rails

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 %>

rails3 routes.rb

I have been working with rails3, here the view.html.erb form have one login button, so when i click on that button, gives no routes matches :controller => 'home', :action => 'login'. But i have put that in routes.rb. Why this happening?
view.html.erb
<%= form_tag( { :controller => 'home', :action => 'login' }, { :method
=> 'post'}) do %>
<%= text_field(:name, :name, :class => "span2",:placeholder =>
"Username") %>
<%= password_field_tag(:password, :password, :class =>"span2") %>
<%= submit_tag "Login", :class => "btn btn-primary" %>
<% end %>
**routes.rb**
resources :home
resources :home do
post :login, :on => :member
end
**homecontroller.rb**
class HomeController < ApplicationController
def login
end
end
You have defined "resources :home" twice, first declaration is useless and overrides second.
Since you used resources to define your routes (which is recomended) you should use the helper method generated, in this case its login_home_path instead of the old syntax { :controller => 'home', :action => 'login' }
First,
You declare resources :home two times.
try this way in your routes.rb
resources :home
match '/login', to: 'home#login'
and use login_path in submit tag.
I would prefere for login, logout, create Sessions controller
rails generate controller Sessions --no-test-framework
and for login create new method and for logout(signout) create destroy method

Posting to controller via link_to

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

Resources