How to link post to topic? - ruby-on-rails

I made a latest posts box in my app at the root. In this box I have post which belongs to topic. Maybe its a stupid question but how can I redirect click on post to the topic where it belongs?
This redirects me to localhost:3000/topics, how to add topic_id's to this path?
This is how I get the latest posts:
controller:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
helper_method :latest_posts
def latest_posts
#posts ||= Post.all.order("created_at desc").limit(3)
end
end
index:
<% latest_posts.each do |posts| %>
<div class="bs-callout bs-callout-warning">
<p><%= link_to post.content.html_safe, topic_path(post.topic) %></p>
</div>
<% end %>
routes.rb:
devise_for :users
get 'categories' => 'categories#index'
resources :topics
resources :posts
resources :users
Routes:
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
categories GET /categories(.:format) categories#index
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
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
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / categories#index

You're going to want to first iterate through the posts, then link to the topic base on the single instance of post.
<h5>Here are all of my posts</h5>
<% #posts.each do |post| %>
<%= link_to post.content.html_safe, topic_path(post.topic) %>
<% end %>
Here is a solid Rails guides post on ActionView and rendering. It's definitely worth the read.

Assuming you have defined the show route in your config/routes.rb, you can use the topic_path helper.
<%= link_to posts.content.html_safe, topic_path(topic) %>
Where topic is the instance which you want to redirect to.

Related

Ruby Rails render form inheriting controller?

Hi and thanks in advance for any help.
I have an admin page: views/admin/admin_page
and a form located in: views/videos/_new
The form renders nicely inside of the admin_page however when I submit the form the url seems to inherit "admin" from either my AdminController or my views/admin folder and appends it to the url I specify in my form. For instance the form below posts to /admin/users/profile/videos.
Can someone help me fix this, please?
videos/_new.html.erb
<%= form_for #video, url: 'users/profile/videos', controller: "videos", method: 'post', :html => { multipart: true } do |f| %>
<div class="form-group">
<%= f.label :avatar %>
<%= f.file_field :avatar, class: 'form-control' %>
</div>
<%= f.submit 'Submit',class: 'btn btn-default' %>
Routes:
Rails.application.routes.draw do
devise_for :users
resources :users do
resource :profile do
resources :videos
end
end
get 'admin/ad_pg', :to => 'admin#ad_pg'
end
Rake Routes:
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
user_profile_videos GET /users/:user_id/profile/videos(.:format) videos#index
POST /users/:user_id/profile/videos(.:format) videos#create
new_user_profile_video GET /users/:user_id/profile/videos/new(.:format) videos#new
edit_user_profile_video GET /users/:user_id/profile/videos/:id/edit(.:format) videos#edit
user_profile_video GET /users/:user_id/profile/videos/:id(.:format) videos#show
PATCH /users/:user_id/profile/videos/:id(.:format) videos#update
PUT /users/:user_id/profile/videos/:id(.:format) videos#update
DELETE /users/:user_id/profile/videos/:id(.:format) videos#destroy
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
user_profile GET /users/:user_id/profile(.:format) profiles#show
PATCH /users/:user_id/profile(.:format) profiles#update
PUT /users/:user_id/profile(.:format) profiles#update
DELETE /users/:user_id/profile(.:format) profiles#destroy
POST /users/:user_id/profile(.:format) profiles#create
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / pages#home
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
contact GET /contacts/:id(.:format) contacts#show
PATCH /contacts/:id(.:format) contacts#update
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
admin_ad_pg GET /admin/ad_pg(.:format) admin#ad_pg
admin/ad_pg is my "admin page"
Thanks again,
Matt
The matching route is /users/:user_id/profile/videos(.:format) but your url is users/profile/videos which has two problems. It is not absolute (it should start with a '/' and it does not include the :user_id segment). You should probably use the helper:
<%= form_for #video, url: user_profile_videos(user_id: #video.user_id), ...
Or something similar

How can I access the profiles/show.html.erb in rails app

I´m using Devise, and after signing up, I want my users to access the profiles/show.html.erb
I have an registrations_controller.rb and in there I have this piece of code
def after_sign_up_path_for(resource)
new_user_profile_path(current_user)
end
It directs the user to profiles/new.html.erb but I want the user to go to profiles/show.html.erb
the rake routesshows this paths:
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
how would I modify this chunk of code to direct to the profiles/show.html.erb?
I'm to unexperienced to figure this out by my self, any help would be greate
this is my routes.rbfile
Rails.application.routes.draw do
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users do
resource :profile
end
root 'pages#index'
end
** Edit**
the rake routes output
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
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) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user_profile POST /users/:user_id/profile(.:format) profiles#create
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
PATCH /users/:user_id/profile(.:format) profiles#update
PUT /users/:user_id/profile(.:format) profiles#update
DELETE /users/:user_id/profile(.:format) profiles#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
GET /%20/users/:user_id/profile(.:format)%20 profiles#show
root GET / pages#index
I fixed it, check your github.
Spelling of your resource is wrong
It should be resources :profile not resource :profile
that is why it was not showing you the paths to the profile resources through rake routes command.
I used the profile_path(current_user) in the show action at profile_controller.rb file
In your controller you need to create
def show
end
You can also try replacing your code in registration controller with.
def after_sign_up_path_for(resource)
show_user_profile_path(current_user)
end
remove 'do' from resources :users do in routes.rb file.
def after_sign_up_path_for(resource)
user_path(current_user)
end
rake routes shows user GET /users/:id(.:format) users#show
routes.rb
get 'profiles/show', to: 'profiles/show', as: :profile
profiles_controller.rb
def show
unless params[:user_id] #user = User.find(current_user.id)
#user = User.find(params[:user_id])
end
and then
def after_sign_up_path_for(resource)
profile_path
end

Adding customer action for devise

I'm trying to add a simple custom action cancelaccount to a user model generated by devise, but having a hard time getting the routing right.
Here is what's in route.rb
devise_for :users, controllers: { registrations: "userregistrations", sessions: "sessions" } do
get '/users/:id/cancelaccount', to: 'userregistrations#cancelaccount', as: 'cancelaccount'
end
Here is the controller
class UserregistrationsController < Devise::RegistrationsController
def create
...
end
def cancelaccount
authenticate_user!
Rails.logger.debug {"&& cancel"}
#user = User.find(params[:id])
unless (#user == current_user)
redirect_to :back, :alert => "Access denied."
end
end
end
Here is the link_to line in the HTML:
<li><%= link_to "Cancel Account", {controller: "userregistrations", action: "cancelaccount", id: current_user.id} %></li>
This is the error I get:
No route matches {:action=>"cancelaccount", :controller=>"userregistrations", :id=>12}
If I change the link_to link to:
<li><%= link_to "Cancel Account", cancelaccount_path(#user) %></li>
The error is:
undefined method `cancelaccount_path' for #<#:0x00000006a5d4c0>
Any help is highly appreciated.
Here is the result of "rake routes":
new_user_session GET /users/sign_in(.:format) sessions#new
user_session POST /users/sign_in(.:format) sessions#create
destroy_user_session DELETE /users/sign_out(.:format) sessions#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) devise/omniauth_callbacks#passthru {:provider=>/(?!)/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) devise/omniauth_callbacks#:action
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) userregistrations#cancel
user_registration POST /users(.:format) userregistrations#create
new_user_registration GET /users/sign_up(.:format) userregistrations#new
edit_user_registration GET /users/edit(.:format) userregistrations#edit
PATCH /users(.:format) userregistrations#update
PUT /users(.:format) userregistrations#update
DELETE /users(.:format) userregistrations#destroy
root GET / static_pages#home
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
userpreferences GET /userpreferences(.:format) userpreferences#index
POST /userpreferences(.:format) userpreferences#create
new_userpreference GET /userpreferences/new(.:format) userpreferences#new
edit_userpreference GET /userpreferences/:id/edit(.:format) userpreferences#edit
userpreference GET /userpreferences/:id(.:format) userpreferences#show
PATCH /userpreferences/:id(.:format) userpreferences#update
PUT /userpreferences/:id(.:format) userpreferences#update
DELETE /userpreferences/:id(.:format) userpreferences#destroy
signup GET /signup(.:format) devise/registrations#new
edit_profile GET /edit_profile(.:format) devise/registrations#edit
change_password GET /change_password(.:format) devise/passwords#edit
sign_in GET /sign_in(.:format) devise/sessions#new
sign_out GET /sign_out(.:format) devise/sessions#destroy
confirmation GET /confirmation(.:format) devise/confirmations#new
unlock_account GET /unlock_account(.:format) devise/unlocks#new
GET /signup(.:format) trainer/registrations#new
edit GET /edit(.:format) trainer/registrations#edit
GET /sign_in(.:format) devise/sessions#new
GET /sign_out(.:format) devise/sessions#destroy
GET /confirmation(.:format) devise/confirmations#new
GET /unlock_account(.:format) devise/unlocks#new
I am not sure what is wrong with your devise_for block, but you can easily add this route outside of it:
devise_for :users, controllers: { registrations: "userregistrations", sessions: "sessions" }
match '/users/:id/cancelaccount' => 'registrations#cancelaccount', via: 'get', as: 'cancelaccount'
Which will add the route you want:
cancelaccount GET /users/:id/cancelaccount(.:format) registrations#cancelaccount

Devise: RoutingError prevents sign up of new users

I type in a new email, password, and password confirmation in my Devise app. When I go to actually do the signup, I get the following error:
RoutingError
No route matches [POST] "/users/sign_up"
Try running rake routes for more information on available routes.
It looks like new_registration_path takes you to users/sign_up, which it doesn't know about (at least when getting a POST). How do I make it recognize this?
Below are some relevant bits of code.
Here's the (possibly) relevant routes.rb:
devise_for :users
resources :users
Here's the output of rake routes:
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
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
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
static_pages_home GET /static_pages/home(.:format) static_pages#home
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
businesses GET /businesses(.:format) businesses#index
POST /businesses(.:format) businesses#create
new_business GET /businesses/new(.:format) businesses#new
ed it_business GET /businesses/:id/edit(.:format) businesses#edit
business GET /businesses/:id(.:format) businesses#show
PUT /businesses/:id(.:format) businesses#update
DELETE /businesses/:id(.:format) businesses#destroy
root / static_pages#home
Here's the form in registrations/new.html.erb:
<%= form_for(resource, :as => resource_name, :url => new_registration_path(resource_name)) do |f| %>
...
<% end %>
I think you should use registration_path instead of new_registration_path because sign up is performed via POST request to /users.
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
...
<% end %>

Rails Mongoid form_for Not Using the Correct Verb

I've got an embedded resource inside of a singular resource (/profile/workout) and am having some problems with the form_for helper.
I've defined the following helper, since profile is just based off the current_user (just redirects to the right url):
def workout_path(*args)
profile_workout_path(*args)
end
I've got the following model:
class Workout
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
embedded_in :user
embeds_many :movements
accepts_nested_attributes_for :movements
end
controller:
def new
#workout = current_user.workouts.build
#workout.movements.build
end
routes:
ComposerDelete::Application.routes.draw do
authenticated :user do
root :to => 'home#index'
end
root :to => "home#index"
devise_for :users
resources :users do
end
resource :profile do
resources :workouts
end
end
and form
= form_for #workout do |f|
%fieldset
= f.label :name
= f.text_field :name
= f.fields_for :movements do |builder|
= render "movement_fields", f: builder
= link_to_add_fields "Add Movement", f, :movements
= f.submit "Create"
When I visit the url: http://localhost:3500/profile/workouts/50b99b70f0f800cd53000002/edit, the form has the following header:
<form accept-charset="UTF-8" action="/profile/workouts/50b99b70f0f800cd53000002" class="edit_workout" id="edit_workout_50b99b70f0f800cd53000002" method="post">
It gets the right id (edit_<model>_<id>), but the wrong method (post, should be put), also, the submit button says Create instead of Update. The form works correctly, and updates the workout.
rake routes:
root / home#index
root / home#index
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
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
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
profile_workouts GET /profile/workouts(.:format) workouts#index
POST /profile/workouts(.:format) workouts#create
new_profile_workout GET /profile/workouts/new(.:format) workouts#new
edit_profile_workout GET /profile/workouts/:id/edit(.:format) workouts#edit
profile_workout GET /profile/workouts/:id(.:format) workouts#show
PUT /profile/workouts/:id(.:format) workouts#update
DELETE /profile/workouts/:id(.:format) workouts#destroy
profile POST /profile(.:format) profiles#create
new_profile GET /profile/new(.:format) profiles#new
edit_profile GET /profile/edit(.:format) profiles#edit
GET /profile(.:format) profiles#show
PUT /profile(.:format) profiles#update
DELETE /profile(.:format) profiles#destroy
The method will always be POST, not PUT - your browser does not support PUT natively, so Rails hacks around this by using a hidden form field inside the form with _method=PUT. See here for some documentation on this.
also, your own code says = f.submit "Create" so it's no wonder the button says 'Create'.

Resources