ページによって飛び掲示板を変えたいのに、全部同じ掲示板に飛んでいく。
自分の大学の大学生が使うサービスで、講義の感想を書く。講義の感想を投稿するページからその講義に関する感想を投稿する別の掲示板に飛びたい。
やったこと
rails g controller topics index show
rails g model topic title:string body:content
現状
トップページ→→学年→科目の感想を投稿し、表示するページ(ここからさらにこの学年、科目についての別の掲示板に飛びたい)
views
top.html.erb
top.html.erb
1<div class="gakunen-main"> 2<h3>学年を選択してください</h3> 3 4<p class="text-center"> 5<%= link_to "1年生", "/years/1", class: "btn btn-lg btn-primary btn-block" %></br><%= link_to "2年生", "/years/2", class: "btn btn-lg btn-primary btn-block" %></br><%= link_to "3年生", "/years/3", class: "btn btn-lg btn-primary btn-block" %><%= link_to "4年生", "/years/4, class: "btn btn-lg btn-primary btn-block" %></br></p> 6</div>
show.html.erb
show.html.erb
1<div class="gakunen-main"> 2<% provide(:title, "クラスの選択") %> 3<h3>クラスを選択してください</h3> 4 5<p class="text-center"> 6 <%= link_to "#{@a}年1組", "/#{@a}/1", class: "btn btn-lg btn-primary btn-block" %></br><%= link_to "#{@a}年2組", "/#{@a}/2", class: "btn btn-lg btn-primary btn-block" %></br><%= link_to "#{@a}年3組", "/#{@a}/3", class: "btn btn-lg btn-primary btn-block" %></br><%= link_to "#{@a}年4組", "/#{@a}/4", class: "btn btn-lg btn-primary btn-block" %></br><%= link_to "#{@a}年5組", "/#{@a}/5", class: "btn btn-lg btn-primary btn-block" %></br><%= link_to "#{@a}年6組", "/#{@a}/6", class: "btn btn-lg btn-primary btn-block" %></br><%= link_to "#{@a}年7組", "/#{@a}/7", class: "btn btn-lg btn-primary btn-block" %></br><%= link_to "#{@a}年8組", "/#{@a}/8", class: "btn btn-lg btn-primary btn-block" %></br><%= link_to "#{@a}年9組", "/#{@a}/9", class: "btn btn-lg btn-primary btn-block" %></br></p> 7</div> 8
index.html.erb
index.html.erb
1<div class="subject-main"> 2<% provide(:title, "講義一覧") %> 3 4<h3>講義を選択してください</h3> 5<% @subjects.each do |subject| %> 6 7<div class="aaa"> 8<p class="text-center"> 9<%= link_to subject.name, "/subjects/#{subject.id}", class: "btn btn-lg btn-primary btn-block" %></br></p> 10 11<% end %> 12</div>
show.html.erb
show.html.erb
1<div class="note-main"> 2 3<% provide(:title, @subject.name+"のページ") %> 4 5<h3><div class="name"><%= @subject.name %>の教授の講義について<div></h3> 6 <% if logged_in? %> 7 <h1>投稿する</h1> 8 <%= form_tag("/posts/create") do %> 9 <textarea name="content"></textarea> 10 <%= hidden_field_tag "subject_id", @subject.id %> 11 <input type="submit" value="投稿" class: "btn btn-primary"> 12 <% end %> 13 <% else %> 14 <h3><%= link_to "投稿するにはログインが必要です", login_path %></h3> 15 <% end %> 16 17 18<%= will_paginate @posts %> 19 20 21 22 <% @posts.each do |post| %> 23 <div class="posts-index-item"> 24 <strong>『<%= post.content %>』</strong> by <%= post.user.name %> 25 26 27 28 <% if logged_in? %> 29 <% if Like.find_by(user_id: @current_user.id, post_id: post.id) %> 30 <<%= link_to("/likes/#{post.id}/destroy", {method: "post"}) do %> 31 <span class="fa fa-heart like-btn-unlike"></span> 32 <% end %> 33 <% else %> 34 <%= link_to("/likes/#{post.id}/create", {method: "post"}) do %> 35 <span class="fa fa-heart like-btn"></span> 36 <% end %> 37 <% end %> 38 <%= Like.where(post_id: post.id).count %> 39 <% else %> 40 いいねの数: <%= Like.where(post_id: post.id).count %> 41 <% end %> 42 <p align=right><%= post.created_at.localtime %></p> 43 </div> 44 <% end %> 45 46 47 48<%= will_paginate @posts %> 49 50</div> 51
routes
routes
1Rails.application.routes.draw do 2 3 4 5 post "likes/:post_id/create" => "likes#create" 6 post "likes/:post_id/destroy" => "likes#destroy" 7 8 get "posts/index" => "posts#index" 9 get "posts/new" => "posts#new" 10 get "posts/:id" => "posts#show" 11 post "posts/create" => "posts#create" 12 13 get 'kumis/show' 14 15 get 'years/show' 16 17 get 'subjects/index' 18 get 'subjects/:id' => 'subjects#show' 19 20 get 'kumis/show' 21 22 23 24 root 'static_pages#home' 25 get '/help', to: 'static_pages#help' 26 get '/screen', to: 'static_pages#screen' 27 get '/configuration', to: 'static_pages#configuration' 28 get '/about', to: 'static_pages#about' 29 get '/contact', to: 'static_pages#contact' 30 get '/top', to: 'static_pages#top' 31 get '/signup', to: 'users#new' 32 get '/login', to: 'sessions#new' 33 post '/login', to: 'sessions#create' 34 delete '/logout', to: 'sessions#destroy' 35 36 get '/years/:year', to: 'years#show' 37 38 39 40 resources :users do 41 member do 42 get :following, :followers 43 end 44 end 45 resources :account_activations, only: [:edit] 46 resources :password_resets, only: [:new, :create, :edit, :update] 47 resources :microposts, only: [:create, :destroy] 48 resources :relationships, only: [:create, :destroy] 49 50 51 get '/:year/:kumi', to: 'subjects#index' 52 53 end 54
subjects_controller.rb
subjects_controller.rb
1class SubjectsController < ApplicationController 2 def index 3 @a = params[:year] 4 @b = params[:kumi] 5 @subjects = Subject.where(year: @a,kumi: @b) 6 render 'index' 7 end 8 9 10 def show 11 @subject = Subject.find_by(id: params[:id]) 12 @posts = Post.where(subject_id: @subject.id).paginate(:page => params[:page], :per_page => 10).order(created_at: :desc) 13 @current_user = User.find_by(id: session[:user_id]) 14 end 15end
years_controller.rb
``years_controller.rb
class YearsController < ApplicationController
def show @a = params[:year] render 'show' end
end
show.html.erbのなかにこの講義についての掲示板へのリンクを貼りたいのですが、何をどうすればいいのでしょう? 私のやり方では全て同じtopic_indexへ飛んでしまいます よろしおく願いいたします
あなたの回答
tips
プレビュー