(Rails 3.2 newbie, App is twitter)
I wish I understood routes, but I still don't. I've setup a system where users can follow other users by submitting their username manually:
#relationship = User.find_by_username(params[:username])
if #relationship
if current_user.following? #relationship
current_user.unfollow #relationship
else
current_user.follow #relationship
end
else
flash[:notice] = "User with username #{params[:username]} is not found"
end
Which spits out this:
undefined method `model_name' for NilClass:Class
That didn't really work until I changed #relationship to :relationship in view like this:
<%= form_for :relationship do |f| %>
<%= f.text_field :username, placheholder: "username" %>
</div>
<br/>
<%= f.submit "Add/Subtract" %>
<% end %>
Now that error message ("User with username..") pops up all-the-time BUT when I click "Add/Subtract" it changes to this:
No route matches [POST] "/buddies"
All this is under the User model.
I imagine something is terribly wrong.
EDIT: Routes looks like this.. (ribbit = tweets)
Ribbitapp::Application.routes.draw do
resources :ribbits
resources :relationships
resources :sessions
resources :users
get 'logout', to: 'sessions#destroy', as: 'logout'
#get 'buddies', to: 'users#buddies', as: 'buddies'
match 'buddies', to: 'users#buddies', as: 'buddies'
root to: 'users#new'
match 'buddies', to: 'users#buddies', as: 'buddies' it should be like this in your routes
Related
I got an error
No route matches [POST] "/article/1/like"
My articles_controller.rb is.
def like
#article = article.all.find(params[:id])
Like.create(user_id: current_user.id, article_id: #article.id)
redirect_to articles_path(#article)
end
This is my index page.
<% if article.liked?(current_user) %>
<%= button_to "like", like_path(article), methode:"put", desabled: true %>
<% else %>
<%= button_to "like", like_path(article), methode:"put" %>
<% end %>
and routes.rb is
Rails.application.routes.draw do
get 'static_pages/landing_page'
get 'static_pages/dashboard'
devise_for :users
resources :users
resources :articles do
resources :comments
end
put '/article/:id/like', to: 'article#like', as: 'like'
root "articles#index"
end
I am writing this code from a website given below.
enter link description here
The idomatically correct way to add additional actions to a resource is:
resources :articles do
put :like
resources :comments
end
This creates the route /articles/:article_id/like(.:format) - not that articles is plural.
<%= button_to "like", article_like_path(article), method: :put, disabled: article.liked?(current_user) %>
def like
#article = article.all.find(params[:id])
Like.create(user_id: current_user.id, article_id: #article.id)
redirect_to articles_path(#article)
end
in like method use params[:article_id] instead of params[:id]
I have my routes defined as below
get 'contacts/new', to: 'contacts#new'
And in my ContactsController I have defined as below
def new
#contact = Contact.new
end
In views contacts/new/_form.html.erb I have structured form as below
<%= form_for #contact, html: {multipart:true} do |f| %>
<%= f.label :username %>
<%= f.text_field :username %>
<% end %>
But when i go to localhost:3000/contacts/new
I get the below error.
undefined method contacts_path which is occuring in first line of form.
But when i try to define as below in routes, it worked
get 'contacts/new', to: 'contacts#new', as: 'contact'
Any idea why rails throws such error when i have defined it in the routes file. I am just trying to understand inner workings of rails routes
To avoid this kind of errors, remove your route and use:
resources :contacts, only: [:new, :create]
Try better use railsy way like resources as #Graham mentioned.
or
get 'contacts', to: 'contacts#index', as: :contacts #genetares: contacts_path
get 'contacts/new', to: 'contacts#new', as: :new_contact #new_contact_path
Make a post type route for contacts (for this it is throwing error)
Or remove this route
get 'contacts/new', to: 'contacts#new'
And add this simply
resources :contacts
I've got the following route defined:
get 'invites/search', to: 'invites#edit', controller: :invites
Which I changed from:
get 'invites/search', to: 'invites#show', controller: :invites
The controller is as follows:
def show
#invite = Invite.find(params[:id])
end
...
def edit
# If the request is a search containing an 'invite_code'
if params.has_key?(:invite_code)
#invite = Invite.where(invite_code: params[:invite_code]).first
else
#invite = Invite.find(params[:id])
end
end
I get the following error:
ActiveRecord::RecordNotFound in InvitesController#show
Couldn't find Invite with 'id'=search
Extracted source (around line #7):
6 def show
7 #invite = Invite.find(params[:id])
8 end
def new
Which suggests the route hasn't updated?
Here's the rake route:
invites_search GET /invites/search(.:format) invites#edit
I've tried restarting the server, but I'm not really sure what to do now? How can I get the route to go to 'edit' not 'show'?
Thanks,
LM.
EDIT:
Here's the form from the index page:
<%= form_tag invites_search_path, method: :get do %>
<%= label_tag :invite_code, "#" %>
<%= text_field_tag :invite_code, nil %>
<%= submit_tag "Search", name: nil %>
<% end %>
EDIT 2:
See here for the explantion of what I'm trying to achieve with this code:
Ruby on Rails: Find a record by an attribute not an id
Why don't you try to create another name for that method because resources :invites always generate the complete CRUD so maybe it's a conflict
Try something like that in routes.rb
resources :invites do
resources :guests
collection do
get 'search'
end
end
Then you call it in the form like that
<%= form_tag search_invites_path, method: :get do %>
I inherited an old Rails app and I'm really struggling with this error:
ActionController::RoutingError
(No route matches {:controller=>"users", :action=>"profile"}):
app/views/layouts/application.html.erb:59:in
`_app_views_layouts_application_html_erb__105<snip>'
app/controllers/users_controller.rb:40:in `index'
I ONLY get this error when I log in as admin, not as any other user.
Here is my routes.rb
Vvault::Application.routes.draw do
resources :orders
devise_for :users
resources :videos
resources :users
root :to => "users#index"
match 'users/:id/profile' => 'users#profile', :as => :user_profile
end
I think this is the relevant snippet from users_controller.rb:
def index
if current_user.admin?
# admin sees all users
#users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
else
redirect_to "/users/" + current_user.id.to_s() and return
end
end
I think this is the relevant snippet from application_html.erb:
<div class="sidebar">
<% if user_signed_in? %>
<%= link_to "My account", user_profile_path, :method => :get %>
<% end %>
</div>
If i comment out the third line of application_html.erb I can login as admin, but obviously the profile link does not render for any user.
Any help appreciated.
Thanks!
Try:
<%= link_to "My account", user_profile_path(current_user.id), :method => :get %>
Your user_profile_path helper needs an id to be passed in, as the corresponding route has an :id parameter.
The error message is telling you that no route exists for that controller/action combination without any other parameters.
Or, you need to define a route without an id parameter where the controller automatically loads the current user's profile
You need to provide the id to user_profile_path. But since that route points to the user's account, there is no point in setting the id, because it should always be the current user (is that your intention?). If this is the case, then you can rewrite the route as:
match 'users/profile' => 'users#profile', :as => :user_profile
If not, then provide an id to the helper method for your route.
I have a controller "find_numbers", which I'm using to submit a form to the Twilio API. Before it submits though, I'd like to validate against two form fields, which aren't in the data model for this controller. The fields are :name, and :original_number
So, in my find_numbers model, I added attr_accessor :name, attr_accessor :originial number to run a validates command under it.
After doing that and submitting the form as invalid, I get the error :
Routing Error
No route matches {:controller=>"phone", :action=>"new"}
Try running rake routes for more information on available routes.
I'm not sure why it says there's no roots, but I'm not sure why it's accessing that anyways. I want it to POST to find_numbers
The find_numbers/new template
<%= form_tag("/find_numbers", :method => "post", :id => "new_user" ) do %>
<%= render 'shared/error_messages' %>
<%= label_tag(:name, "What Are You Tracking?") %>
<%= text_field_tag(:name) %>
<%= label_tag(:original_number, "Your Orginal Number") %>
<%= text_field_tag(:original_number) %>
<%= label_tag(:in_postal_code, "Near US postal code (e.g. 94117):") %>
<%= text_field_tag(:in_postal_code) %>
<%= label_tag(:near_number, "Near this other number (e.g. +4156562345)") %>
<%= text_field_tag(:near_number) %>
<%= label_tag(:contains, "Matching this pattern (e.g. 415***EPIC):") %>
<%= text_field_tag(:contains) %>
<%= submit_tag("Search", :class => "btn btn-large btn-primary") %>
<% end %>
here's my find_number model
class FindNumber < ActiveRecord::Base
attr_accessor :name
attr_accessor :original_number
validates :name, presence: true
validates :original_number, presence: true
end
Here's my Find_number controller
class FindNumbersController < ApplicationController
def new
#user = current_user
end
def create
#user = current_user
client = Twilio::REST::Client.new(#user.twilio_account_sid, #user.twilio_auth_token)
search_params = {}
%w[in_postal_code near_number contains].each do |p|
search_params[p] = params[p] unless params[p].nil? || params[p].empty?
end
local_numbers = client.account.available_phone_numbers.get('US').local
#numbers = local_numbers.list(search_params)
unless #numbers.empty?
render 'find_numbers/show'
else
flash.now[:error] = "Sorry, We Couldn't Find Any Numbers That Matched Your Search! Maybe Something Simpler?"
render 'find_numbers/new'
end
end
def show
end
end
Any thoughts on accomplishing this would be greatly appreciated!
Edit
Routes.rb file
Dct::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :phones, only: [:new, :create, :destroy]
resources :find_numbers, only: [:new, :create, :destroy]
match '/find_numbers', to: 'find_numbers#new'
match '/signup', to: 'users#new'
match '/login', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
root to: 'static_pages#home'
match '/product_demo', to: 'static_pages#product_demo'
match '/pricing', to: 'plans#index'
match '/contact', to: 'static_pages#contact'
Edit
Here is the server log, of what happened when I hit submit
http://stepanp.com/railserror.jpg
Also, here's the find_numbers/show view
From what you've posted, the only other thing that looks suspicious to me is that you presumably have a PhonesController (plural) since you've declared resources :phones, but the routing error seems to occur because it is looking for a PhoneController (singular).