So I'm trying to create the ability for users to add comments and upload photos to their pages, but I keep getting this NoMethodError in Places#show error. It says the following (and I'm quoting the error page):
undefined method `place_photos_path' for #<#<Class:0xb5d58300>:0xb5642930>
And then shows me the line of code where the issue is apparently located:
<h4 class="modal-title" id="myModalLabel">Add a comment</h4>
</div>
<%= simple_form_for #photo, :url => place_photos_path(#place) do |f| %>
<div class="modal-body">
<%= f.input :caption %>
</div>
It highlights the simple_form line as being the line in question. I'm really stuck. Any help would be so much appreciated.
Here is the config/routes.rb file:
Findmygirlfriend::Application.routes.draw do
devise_for :users
root 'places#index'
resources :places do
resources :comments, :only => :create
end
resources :users, :only => :show
resources :photos, :only => :create
end
Thanking you in advance.
Just place your resources at the right scope. This should help
Findmygirlfriend::Application.routes.draw do
devise_for :users
root 'places#index'
resources :places do
resources :comments, :only => :create
resources :photos, :only => :create
end
resources :users, :only => :show
end
Related
I am trying to come up with upvote/downvote method in my app and I running into a no method error.
Here is my voter form:
<div>
<div class= 'pull-left'>
<div><%= link_to " ", post_up_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
<div><strong><%= post.points %></strong></div>
<div><%= link_to " ", post_down_vote_path(post), class: 'glyphicon plyphicon-chevron-up', method: :post %></div>
</div>
Here are my routes:
App::Application.routes.draw do
devise_for :users
resources :users
resources :topics do
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post '/up-vote' => 'votes#post_up_vote', as: :up_vote
post '/down-vote' => 'votes#post_down_vote', as: :down_vote
end
end
get 'about' => 'welcome#about'
root to: 'welcome#index'
end
And here is my partial call:
<%= render partial: 'votes/voter', locals: { post: post } %>
Now I don't think there is anything wrong with my partial call or my voter partial because everything works until I try to route it.
First, you will need to change up your routes a bit.
resources :posts, except: [:index] do
resources :comments, only: [:create, :destroy]
post 'upvote', on: :member
post 'downvote', on: :member
end
The reason these are member routes instead of collection routes is being they apply to a specific member of the collection of posts...a single Post.
Then, you will want to run the command, rake routes in your Terminal to see what the appropriate path methods are. It should be something like
upvote_post_path(:id)
which requires an object be passed in..so in your view you would use it like you currently are.
upvote_post_path(post)
I want to a searching function for users. It does not work. So I simplified the method, I just want to refresh the index page when I hit the search button. But it still does not work, it said
ActiveRecord::RecordNotFound in UsersController#show, Couldn't find User with id=search.
please tell me Why
my controller
class UsersController < ApplicationController
load_and_authorize_resource :except => [:index]
def search
redirect_to users_path
end
end
My view
<%= form_tag users_search_path, :method => 'get' do %>
<td><%= text_field_tag :username, params[:username] %></td>
<%= submit_tag "Search", :class => "buttons buttons-rounded buttons-flat-action", :id => "button-new"%>
<br><br><br>
<% end %>
My Route
Procedures::Application.routes.draw do
devise_for :users
#### USER MANAGEMENT ####
resources :users do
resources :rateofpays # professional timesheet
resources :roles
resources :biographies
resources :qualifications do
collection do
put 'complete', :action => 'complete'
end
end
resources :supervisors
end
#### users search ####
get 'users/search' => "users#search", as: 'users_search'
The line
get 'users/search' => "users#search", as: 'users_search'
...is too far down in your routes. the resources :users appears first, and it has a match path that looks like users/:id and the users/search is incorrectly matching against that.
Just move the get 'users/search' to the top... or alternatively define it as a collection method under resources :users
resources :users do
collection do
get 'search'
end
I have a one to many association between jobs and companies and it work fine now i want in the job show view to have a link to the company who has published the job for this in my job show view i put this code
<p>
<strong>company:</strong>
<%= link_to #job.company_name, company_path %>
</p>
but instead of going to http://localhost:3000/companies/company_id I’m redirecting to http://localhost:3000/companies/job_id where is the fault in my view
this is my routes
Ecole::Application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
resources :jobs
resources :companies
resources :posts, only: [:show, :create]
devise_for :users
root "welcome#index"
get "profiles/show"
get '/profile/:id' => 'profiles#show', :as => :profile
get 'profiles' => 'profiles#index'
Replace
<%= link_to #job.company_name, company_path %>
With
<%= link_to #job.company_name, company_path(#job.company) %>
I think you should be passing the company id as a param for the company_path route. Try with company_path(:id=> company_id) with company_id being the targeted company id.
I'm trying to get this plugin to work. I've read other threads, but I dont seem to get it.
I'm trying to make my article model commentable. This if what i have done so far:
Done all the installation correctly.
class CommentsController < ApplicationController
def create
#article = Article.find(params[:id])
#user_who_commented = #current_user
#comment = Comment.build_from( #article, #user_who_commented.id, "Hey guys this is my comment!" )
end
end
In articles#show:
<%= form_for #comment do |f| %>
<%= f.text_area :text %>
<%= f.submit "Post Comment" %>
<% end %>
Routes:
devise_for :users
resources :dashboard
resources :users, :only => [:index,:show,:edit,:update]
resources :events
resources :januscript do
resources :quotes
end
resources :jmail, :only => [:index, :show]
resources :album
resources :video, :only => [:index, :show]
resources :photos
scope '/archive' do
resources :quotes, :only => [:index]
resources :articles, :only => [:index,:show]
end
resources :comments
I get this error message:
undefined method `model_name' for NilClass:Class
Make sure to add acts_as_commentable to your model that you want comments on. Not seeing any model code here.
Edit:
Did you add the nested resource in your routes for articles as well?
resources :articles do
resources :comments
end
I have the following two models:
class PhotoAlbum < ActiveRecord::Base
belongs_to :project
has_many :photos, :dependent => :destroy
class Photo < ActiveRecord::Base
belongs_to :photo_album
When the user is viewing a photo, the URL is like follows: "/photo_albums/40/photos?page=6"
On this photo view page (inside an album) I want to give the user the option to update the image, so I'm using the following:
<% form_for [:photo, #photos.first], :url => photo_album_photos_path, :html => { :multipart => true } do |f| %>
<form method="post" id="edit_photo_124" enctype="multipart/form-data" class="edit_photo" action="/photo_albums/40/photos" accept-charset="UTF-8">
Which I think is right? Problem is, when I click update, I get the following error:
No route matches "/photo_albums/40/photos"
Here are my routes, which look good, no?
photo_album_photo PUT /photo_albums/:photo_album_id/photos/:id(.:format) {:action=>"update", :controller=>"photos"}
photo PUT /photos/:id(.:format) {:action=>"update", :controller=>"photos"}
Thoughts? thanks
UPDATE w config route file:
resources :photos do
resources :comments, :only => [:create, :update,:destroy], :constraint => {:context_type => "photos"}
collection do
post 'upload'
get 'search'
end
end
resources :photo_albums do
resources :comments, :only => [:create, :update,:destroy], :constraint => {:context_type => "photo_albums"}
resources :photos
collection do
get 'search'
end
end
Try to change your form to something like:
<% form_for [#photo_album, #photos.first], :html => { :multipart => true } do |f| %>
where #photo_album is your photo_album object, or
<% form_for #photos.first, :url => photo_album_photo_path(#photo_album, #photo), :html => { :multipart => true } do |f| %>
which is basically the same. To have this working, you should have the following in your route.rb:
resources :photo_albums do
resources :photos
end