I am having trouble with the form in rails - ruby-on-rails

How can i store image Tags in Tag model. I have albums controller in which i have images on show page .. and in those particular images i want to save tags when i enter.
My albums controller :
class AlbumsController < ApplicationController
before_action :authenticate_user!
def index
#albums = current_user.albums.all
end
def show
#album= current_user.albums.find(params[:id])
end
def new
#album = current_user.albums.new
end
def create
#album = current_user.albums.new(album_params)
if #album.save
redirect_to albums_path
else
render :new
end
end
def edit
#album =current_user.albums.find(params[:id])
end
def update
#album = current_user.albums.find(params[:id])
if #album.update(album_params)
redirect_to albums_path
else
render :edit
end
end
def destroy
#album = current_user.albums.find(params[:id])
#album.destroy
redirect_to albums_path
end
def delete_image_attachment
#image = ActiveStorage::Attachment.find(params[:id])
#image.purge
redirect_to albums_path
end
private
def album_params
params.require(:album).permit(:title, :desciption, images: [])
end
end
My routes.rb file :
Rails.application.routes.draw do
devise_for :users
root to: "albums#index"
resources :albums do
member do
delete :delete_image_attachment
end
resources :comments
resources :tags
end
end
And rails routes are:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
root GET / albums#index
delete_image_attachment_album DELETE /albums/:id/delete_image_attachment(.:format) albums#delete_image_attachment
album_comments GET /albums/:album_id/comments(.:format) comments#index
POST /albums/:album_id/comments(.:format) comments#create
new_album_comment GET /albums/:album_id/comments/new(.:format) comments#new
edit_album_comment GET /albums/:album_id/comments/:id/edit(.:format) comments#edit
album_comment GET /albums/:album_id/comments/:id(.:format) comments#show
PATCH /albums/:album_id/comments/:id(.:format) comments#update
PUT /albums/:album_id/comments/:id(.:format) comments#update
DELETE /albums/:album_id/comments/:id(.:format) comments#destroy
album_tags GET /albums/:album_id/tags(.:format) tags#index
POST /albums/:album_id/tags(.:format) tags#create
new_album_tag GET /albums/:album_id/tags/new(.:format) tags#new
edit_album_tag GET /albums/:album_id/tags/:id/edit(.:format) tags#edit
album_tag GET /albums/:album_id/tags/:id(.:format) tags#show
PATCH /albums/:album_id/tags/:id(.:format) tags#update
PUT /albums/:album_id/tags/:id(.:format) tags#update
DELETE /albums/:album_id/tags/:id(.:format) tags#destroy
albums GET /albums(.:format) albums#index
POST /albums(.:format) albums#create
new_album GET /albums/new(.:format) albums#new
edit_album GET /albums/:id/edit(.:format) albums#edit
album GET /albums/:id(.:format) albums#show
PATCH /albums/:id(.:format) albums#update
PUT /albums/:id(.:format) albums#update
DELETE /albums/:id(.:format) albums#destroy
rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create
rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create
rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create
rails_mandrill_inbound_health_check GET /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#health_check
rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create
rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create
rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index
POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create
new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new
edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit
rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show
PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy
new_rails_conductor_inbound_email_source GET /rails/conductor/action_mailbox/inbound_emails/sources/new(.:format) rails/conductor/action_mailbox/inbound_emails/sources#new
rails_conductor_inbound_email_sources POST /rails/conductor/action_mailbox/inbound_emails/sources(.:format) rails/conductor/action_mailbox/inbound_emails/sources#create
rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create
rails_service_blob GET /rails/active_storage/blobs/redirect/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
rails_service_blob_proxy GET /rails/active_storage/blobs/proxy/:signed_id/*filename(.:format) active_storage/blobs/proxy#show
GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
rails_blob_representation GET /rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
rails_blob_representation_proxy GET /rails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/proxy#show
GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
My Tags Controller:
class TagsController < ApplicationController
def create
#image = ActiveStorage::Blob.find(params[:image_id])
tags = params[:tag_name]
tags.each do |tag|
image = params[:image_id]
#tag = Tag.create(tag_name:tag,image_id:image)
redirect_to album_path(#album)
end
end
end
My Tag model:
class Tag < ApplicationRecord
belongs_to :image
end
and this is my show page(well half of it) in which error is coming:
<%= form_for(#tag, url: album_tags_path(#album) ) do |form|%>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Enter Tags for your Image here</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="tag-container d-flex flex-wrap">
<input class="form-control py-4 px-3" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<%= form.submit class:"btn btn-info"%>
</div>
</div>
</div>
</div>
<% end %>
what is wrong in form_for url here???
I am getting error
ArgumentError in Albums#show

How about letting Rails deduce the route
<%= form_for([#album , #tag]) do |form| %>

Related

Missing path helper after associating two models

Background
I am following an outdated beginner's Ruby course and got stuck. During this course, I am building a Ruby on rails application similar to Yelp, so it is for listing and reviewing restaurants. Currently, I am linking the reviews to the restaurants.
My ruby version is: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin22]
My rails version is: Rails 7.0.4.2
Steps
I am linking the reviews to the restaurants by executing the following steps:
Run rails generate migration AddRestaurantIDToReviews restaurant_id:integer
Run rake db:migrate
Add belongs_to :restaurant to reviews.rb
Add has_many :reviews to restaurants.rb
Check rails routes
Adjusted routes.rb file
Adjusted reviews_form.html.erb
Adjusted review_controller.rb file
set_restaurant and set_restaurant before action
Add #review statement for restaurant_id (line …)
Removed links that point to old URLs
Restart server
Error message
This resulted in the following error: undefined method `reviews_path' for #ActionView::Base:0x00000000025800 and this is triggered by this line of code: <%= form_with(model: [#resturant, #review], local: true) do |form| %> (from the reviews _form.html.erb file). In this thread a similar issue is explained, but I still can't figure it out.
Could someone help me with this? And if you need more info, please let me know!
Review.rb
class Review < ApplicationRecord
belongs_to :user
belongs_to :restaurant
end
Restaurant.rb
class Restaurant < ApplicationRecord
mount_uploader :image, ImageUploader
has_many :reviews
end
Routes.rb
Rails.application.routes.draw do
devise_for :users
resources :restaurants do
resources :reviews, except: [:show, :index]
end
get 'pages/about'
get 'pages/contact'
root 'restaurants#index'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Defines the root path route ("/")
# root "articles#index"
end
**Routes**
` Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
restaurant_reviews POST /restaurants/:restaurant_id/reviews(.:format) reviews#create
new_restaurant_review GET /restaurants/:restaurant_id/reviews/new(.:format) reviews#new
edit_restaurant_review GET /restaurants/:restaurant_id/reviews/:id/edit(.:format) reviews#edit
restaurant_review PATCH /restaurants/:restaurant_id/reviews/:id(.:format) reviews#update
PUT /restaurants/:restaurant_id/reviews/:id(.:format) reviews#update
DELETE /restaurants/:restaurant_id/reviews/:id(.:format) reviews#destroy
restaurants GET /restaurants(.:format) restaurants#index
POST /restaurants(.:format) restaurants#create
new_restaurant GET /restaurants/new(.:format) restaurants#new
edit_restaurant GET /restaurants/:id/edit(.:format) restaurants#edit
restaurant GET /restaurants/:id(.:format) restaurants#show
PATCH /restaurants/:id(.:format) restaurants#update
PUT /restaurants/:id(.:format) restaurants#update
DELETE /restaurants/:id(.:format) restaurants#destroy
pages_about GET /pages/about(.:format) pages#about
pages_contact GET /pages/contact(.:format) pages#contact
root GET / restaurants#index
turbo_recede_historical_location GET /recede_historical_location(.:format) turbo/native/navigation#recede
turbo_resume_historical_location GET /resume_historical_location(.:format) turbo/native/navigation#resume
turbo_refresh_historical_location GET /refresh_historical_location(.:format) turbo/native/navigation#refresh
rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create
rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create
rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create
rails_mandrill_inbound_health_check GET /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#health_check
rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create
rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create
rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index
POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create
new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new
edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit
rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show
PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy
new_rails_conductor_inbound_email_source GET /rails/conductor/action_mailbox/inbound_emails/sources/new(.:format) rails/conductor/action_mailbox/inbound_emails/sources#new
rails_conductor_inbound_email_sources POST /rails/conductor/action_mailbox/inbound_emails/sources(.:format) rails/conductor/action_mailbox/inbound_emails/sources#create
rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create
rails_conductor_inbound_email_incinerate POST /rails/conductor/action_mailbox/:inbound_email_id/incinerate(.:format) rails/conductor/action_mailbox/incinerates#create
rails_service_blob GET /rails/active_storage/blobs/redirect/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
rails_service_blob_proxy GET /rails/active_storage/blobs/proxy/:signed_id/*filename(.:format) active_storage/blobs/proxy#show
GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
rails_blob_representation GET /rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
rails_blob_representation_proxy GET /rails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/proxy#show
GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create`
Review _form.html.erb
<%= form_with(model: [#resturant, #review], local: true) do |form| %>
<% if review.errors.any? %>
<div style="color: red">
<h2><%= pluralize(review.errors.count, "error") %> prohibited this review from being saved:</h2>
<ul>
<% review.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= form.label :rating, style: "display: block" %>
<%= form.number_field :rating, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :comment, style: "display: block" %>
<%= form.text_area :comment, class: "form-control" %>
</div>
<div>
<%= form.submit class: "btn btn-primary" %>
</div>
<% end %>
Reviews_controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: %i[ edit update destroy ]
before_action :set_restaurant
before_action :authenticate_user!
# GET /reviews/new
def new
#review = Review.new
end
# GET /reviews/1/edit
def edit
end
# POST /reviews or /reviews.json
def create
#review = Review.new(review_params)
#review.user_id = current_user.id
#review.restaurant_id = #restaurant.id
respond_to do |format|
if #review.save
format.html { redirect_to root_path, notice: "Review was successfully created." }
format.json { render :show, status: :created, location: #review }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reviews/1 or /reviews/1.json
def update
respond_to do |format|
if #review.update(review_params)
format.html { redirect_to review_url(#review), notice: "Review was successfully updated." }
format.json { render :show, status: :ok, location: #review }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1 or /reviews/1.json
def destroy
#review.destroy
respond_to do |format|
format.html { redirect_to reviews_url, notice: "Review was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
#review = Review.find(params[:id])
end
def set_restaurant
#restaurant = Restaurant.find(params[:restaurant_id])
end
# Only allow a list of trusted parameters through.
def review_params
params.require(:review).permit(:rating, :comment)
end
end
The url: restaurant_reviews_path addition to the form_with made the difference.
<%= form_with(model: #review, url: restaurant_reviews_path, local: true) do |form| %>

Nested routes with polymorphic association

I'm trying to implement the act_as_votable gem in my rails application. I got it to work on the blogs model, but the comments model is where I'm stuck. I get undefined methodlike_comment_path'` when I try to run it, and it's because there are no routes being generated like there are for the blog.
Here is the routes file:
resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
resources :comments
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
end
On the blog index, I am able to call the blog like path just fine.
<div class="btn-group">
<%= link_to like_blog_path(blog), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-up"></span>
Upvote
<%= blog.get_upvotes.size %>
<% end %>
<%= link_to dislike_blog_path(blog), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-down"></span>
Downvote
<%= blog.get_downvotes.size %>
<% end %>
</div>
I run into trouble, though, with the comment part.
<div class="btn-group">
<%= link_to like_comment_path(#commentable, comment), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-up"></span>
Upvote
<%= comment.get_upvotes.size %>
<% end %>
<%= link_to dislike_comment_path(#commentable, comment), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-down"></span>
Downvote
<%= comment.get_downvotes.size %>
<% end %>
</div>
When I run rake routes, nothing for the comments comes up.
Prefix Verb URI Pattern Controller#Action
root GET / glips#index
pages_home GET /pages/home(.:format) pages#home
pages_about GET /pages/about(.:format) pages#about
pages_contact GET /pages/contact(.:format) pages#contact
tag GET /tags/:tag(.:format) glips#index
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
new_user_password GET /password/new(.:format) devise/passwords#new
edit_user_password GET /password/edit(.:format) devise/passwords#edit
user_password PATCH /password(.:format) devise/passwords#update
PUT /password(.:format) devise/passwords#update
POST /password(.:format) devise/passwords#create
cancel_user_registration GET /cancel(.:format) devise/registrations#cancel
new_user_registration GET /register(.:format) devise/registrations#new
edit_user_registration GET /edit(.:format) devise/registrations#edit
user_registration PATCH / devise/registrations#update
PUT / devise/registrations#update
DELETE / devise/registrations#destroy
POST / devise/registrations#create
like_blog PUT /blogs/:id/like(.:format) blogs#upvote
dislike_blog PUT /blogs/:id/dislike(.:format) blogs#downvote
blog_comments GET /blogs/:blog_id/comments(.:format) comments#index
POST /blogs/:blog_id/comments(.:format) comments#create
new_blog_comment GET /blogs/:blog_id/comments/new(.:format) comments#new
edit_blog_comment GET /blogs/:blog_id/comments/:id/edit(.:format) comments#edit
blog_comment GET /blogs/:blog_id/comments/:id(.:format) comments#show
PATCH /blogs/:blog_id/comments/:id(.:format) comments#update
PUT /blogs/:blog_id/comments/:id(.:format) comments#update
DELETE /blogs/:blog_id/comments/:id(.:format) comments#destroy
PUT /blogs/:id/like(.:format) comments#upvote
PUT /blogs/:id/dislike(.:format) comments#downvote
blogs GET /blogs(.:format) blogs#index
POST /blogs(.:format) blogs#create
new_blog GET /blogs/new(.:format) blogs#new
edit_blog GET /blogs/:id/edit(.:format) blogs#edit
blog GET /blogs/:id(.:format) blogs#show
PATCH /blogs/:id(.:format) blogs#update
PUT /blogs/:id(.:format) blogs#update
DELETE /blogs/:id(.:format) blogs#destroy
users GET /users(.:format) users#index
GET /users/:id(.:format) users#show
user GET /users/:id(.:format) users#show
I cannot figure out how to make the comment path work with the routes. What am I doing wrong?
Are you looking for nesting the :comments resource inside :blogs? Otherwise your routes should look like:
resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
end
resources :comments do
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
end
(note you had a missing do at beginning of the :comments block which resulted in a weird block ending/nesting)

routing to users device

iam trying to remember how to code since 3 months of stoping, so in my new project trying to add a link_to that goes to the device users sign_in page..kept getting this error.
NameError in Topics#index
undefined local variable or method `new_user_session' for #<#<Class:0x74e5db0>:0x54af248>
route.rb
Rails.application.routes.draw do
resources :topics
devise_for :admins, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users
root 'topics#index'
here is my routes
Prefix Verb URI Pattern Controller#Action
topics GET /topics(.:format) topics#index
POST /topics(.:format) topics#create
new_topic GET /topics/new(.:format) topics#new
edit_topic GET /topics/:id/edit(.:format) topics#edit
topic GET /topics/:id(.:format) topics#show
PATCH /topics/:id(.:format) topics#update
PUT /topics/:id(.:format) topics#update
DELETE /topics/:id(.:format) topics#destroy
new_admin_session GET /admin/login(.:format) active_admin/devise/sessions#new
admin_session POST /admin/login(.:format) active_admin/devise/sessions#create
destroy_admin_session DELETE|GET /admin/logout(.:format) active_admin/devise/sessions#destroy
admin_password POST /admin/password(.:format) active_admin/devise/passwords#create
new_admin_password GET /admin/password/new(.:format) active_admin/devise/passwords#new
edit_admin_password GET /admin/password/edit(.:format) active_admin/devise/passwords#edit
PATCH /admin/password(.:format) active_admin/devise/passwords#update
PUT /admin/password(.:format) active_admin/devise/passwords#update
admin_root GET /admin(.:format) admin/dashboard#index
batch_action_admin_admins POST /admin/admins/batch_action(.:format) admin/admins#batch_action
admin_admins GET /admin/admins(.:format) admin/admins#index
POST /admin/admins(.:format) admin/admins#create
new_admin_admin GET /admin/admins/new(.:format) admin/admins#new
edit_admin_admin GET /admin/admins/:id/edit(.:format) admin/admins#edit
admin_admin GET /admin/admins/:id(.:format) admin/admins#show
PATCH /admin/admins/:id(.:format) admin/admins#update
PUT /admin/admins/:id(.:format) admin/admins#update
DELETE /admin/admins/:id(.:format) admin/admins#destroy
admin_dashboard GET /admin/dashboard(.:format) admin/dashboard#index
batch_action_admin_users POST /admin/users/batch_action(.:format) admin/users#batch_action
admin_users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
new_admin_user GET /admin/users/new(.:format) admin/users#new
edit_admin_user GET /admin/users/:id/edit(.:format) admin/users#edit
admin_user GET /admin/users/:id(.:format) admin/users#show
PATCH /admin/users/:id(.:format) admin/users#update
PUT /admin/users/:id(.:format) admin/users#update
DELETE /admin/users/:id(.:format) admin/users#destroy
admin_comments GET /admin/comments(.:format) admin/comments#index
POST /admin/comments(.:format) admin/comments#create
admin_comment GET /admin/comments/:id(.:format) admin/comments#show
DELETE /admin/comments/:id(.:format) admin/comments#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / topics#index
index.erb
<div id="pre_header" class="visible-lg" style="background-color:#E4AB7E; height:5px;"></div>
<div id="header" class="container" style="background-color:#fff; background-image:none;">
<div class="row">
<!-- Logo -->
<div class="logo">
<%= link_to (image_tag 'logo.jpg',:style=>'padding:20px 50px;'), root_path %>
</div>
<!-- End Logo -->
<!-- Top Menu -->
<div class="col-md-12 margin-top-30">
<div id="hornav" class="pull-right visible-lg">
<ul class="nav navbar-nav">
<li><%= link_to 'Laman Utama', topics_path %></li>
<li>Senarai Ilmu</li>
<li>Hubungi Kami</li>
<li>Info Lanjut</li>
<li><%= link_to 'Log Masuk',new_user_session %></li>
</ul>
</div>
</div>
<div class="clear"></div>
<!-- End Top Menu -->
</div>
</div>
<div id="pre_header" class="visible-lg" style="background-color:#E4AB7E; height:5px;"></div>
It's a typo I guess. path is missing in the link. It would be new_user_session_path
<li><%= link_to 'Log Masuk',new_user_session_path %></li>

Comments to my reddit clone ruby pulls a NameError

I'm trying to add comment functionality of my Reddit clone. This is the comments controller that creates a comments and adds it to a post.
class CommentsController < ApplicationController
def new
#topic = Topic.find(params[:topic_id])
#post = Post.find(params[:id])
#comment = Comment.new
#authorize #comment # from include Pundit in the application controller, authorize is an inherited method
end
def create
#topic = Topic.find(params[:topic_id])
#post = Post.find(params[:id])
#comment = current_user.comments.build(comment_params)
end
private
def comment_params
params.require(:comment).permit(:text)
end
end
I'm trying to add a comments field for every post page by using a form partial that looks like this:
<%= form_for [topic, post] do |f| %>
<%= form_group_tag(comment[:text]) do %>
<%= f.label :text %>
<%= f.text_area :text, rows: 10, class: 'form-control', placeholder: "Enter your comment" %>
<% end %>
<div class = "form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<% end %>
This form partial should appear at the post.show.html.erb so I put it there
<h1><%= markdown #post.title %></h1>
<div class="row"> <!-- what others are there besides row? -->
<div class="col-md-8">
<p><%= markdown #post.body %></p>
</div>
<div class="col-md-4">
<% if policy(#post).edit? %>
<%= link_to "Edit", edit_topic_post_path(#topic, #post), class: 'btn btn-success' %>
<% end %>
</div>
<div class="col-md-8">
<%= render partial: 'comments/form', locals: { topic: #topic, post: #post, text: #post.comments.new } %>
</div>
</div>
but I'm getting a NameError for my 'comment' on the form_group_tag line. Most of what I defined here comes from my code for adding new posts, which seemed to work. Is there something missing here?
I fixed my name error by adding comments to the form_for line, but I'm getting NoMethodError for my topic,post,comment path, so I thought it'd be helpful to add what rake routes is pulling up.
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
topic_posts POST /topics/:topic_id/posts(.:format) posts#create
new_topic_post GET /topics/:topic_id/posts/new(.:format) posts#new
edit_topic_post GET /topics/:topic_id/posts/:id/edit(.:format) posts#edit
topic_post GET /topics/:topic_id/posts/:id(.:format) posts#show
PATCH /topics/:topic_id/posts/:id(.:format) posts#update
PUT /topics/:topic_id/posts/:id(.:format) posts#update
DELETE /topics/:topic_id/posts/:id(.:format) posts#destroy
topics GET /topics(.:format) topics#index
POST /topics(.:format) topics#create
new_topic GET /topics/new(.:format) topics#new
edit_topic GET /topics/:id/edit(.:format) topics#edit
topic GET /topics/:id(.:format) topics#show
PATCH /topics/:id(.:format) topics#update
PUT /topics/:id(.:format) topics#update
DELETE /topics/:id(.:format) topics#destroy
post_comments POST /posts/:post_id/comments(.:format) comments#create
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
about GET /about(.:format) welcome#about
root GET / welcome#index
BTW: how does this routes.rb file look?
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index]
end
resources :posts do
resources :comments, only: [:create]
end
end
Try:
<%= render partial: 'comments/form', locals: { topic: #topic, post: #post, comment: #post.comments.new } %>
...
<%= form_for [post, comment] do |f| %>
<%= f.label :text %>
<%= f.text_area :text, rows: 10, class: 'form-control', placeholder: "Enter your comment" %>
<%= f.submit "Save", class: 'btn btn-success' %>
<% end %>
...
If it does not help, try to temporary comment form_group_tag and send error here
It seems like you're sending the comment down to the partial as text.
... locals: { topic: #topic, post: #post, text: #post.comments.new } %>
^^^^
And by the way you're not saving the comment in the create action.

What route to use to link to resource creator?

My goal is to link the <%= #thing.user.username %> to the user's profile. I know it's something like:
<%= link_to #thing.user.username, %>
But what goes after the comma?
I can currently navigate to a user by going to "localhost:3000/users/UserNameHere". How do I link to that page dynamically?
Each of my Things has a line showing what user posted it. Here is how Things are displayed:
<div class='row'>
<div class='col-md-offset-2 col-md-8'>
<div class='panel panel-default'>
<div class='panel-heading text-center'>
<%= image_tag #thing.image.url(:medium) %>
</div>
<div class='panel-body'>
<p>
<strong><%= #thing.title %></strong>
</p>
<p>
<%= #thing.description %>
</p>
<p>
<%= #thing.user.username %>
</p>
<% if #thing.user == current_user %>
<%= link_to edit_thing_path(#thing) do %>
<span class='glyphicon glyphicon-edit'></span>
<% end %>
<% end %>
</div>
</div>
</div>
Here is my UsersController:
class UsersController < ApplicationController
def show
#user = User.find_by_id(params[:id])
end
end
Here are my routes:
Stynyl::Application.routes.draw do
resources :things
devise_for :users
get '/about', to: 'pages#about'
root 'things#index'
get 'users/:username' => "users#show"
end
Here is my rake routes output:
Prefix Verb URI Pattern Controller#Action
things GET /things(.:format) things#index
POST /things(.:format) things#create
new_thing GET /things/new(.:format) things#new
edit_thing GET /things/:id/edit(.:format) things#edit
thing GET /things/:id(.:format) things#show
PATCH /things/:id(.:format) things#update
PUT /things/:id(.:format) things#update
DELETE /things/:id(.:format) things#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
about GET /about(.:format) pages#about
root GET / things#index
GET /users/:username(.:format) users#show
You should stick to REST conventions in Rails, so use resources.
Replace:
get 'users/:username' => "users#show"
with:
resources :users, only: [:show]
then:
<%= link_to #thing.user.username, user_path(#thing.user.username) %>
or:
<%= link_to #thing.user.username, #thing.user %>
#and in model
def to_param
username
end
And replace params[:username] with params[:id] in your action.

Resources