以前も質問させていただいた延長で、自己解決したつもりでしたが新たに上手くいかないところが出てきてしまったので質問させていただきます。
プロフィールサイトのようなモノを作ろうとしていますが、 サインイン後のルーティングに関しまして上手く構築できません。
class ApplicationController < ActionController::Base protect_from_forgery with: :exception #ログイン後にアカウントuser_idが合っている記事に行く。 #サインアップ時@articleは空でありm_show上でエラーを出すのでその場合newアクションに飛びたい def after_sign_in_path_for(resource) m_show_articles_path(user_id: resource.id) if @article.nil? new_article_path end end end
このようにしたところ、すでに@articleに情報が入っているアカウントでサインインしてもnewアクションに飛んでしまいます。
またその場合
#if @article.nil? # new_article_path #end
このようにコメントアウトしたら@articleがnilであるとエラーが出ると思ったのですがエラーも出ません。 何が起こっているのか解らず困っています。
意図していることは
- サインアップ時newアクションに飛ぶ(OK)
- サインイン時はすでに登録したマイページに飛ぶです。
この書き方ではサインアップでもサインインでもnewアクションに飛んでしまいます。自分なりに考えて書いてみたのですが上手くいきません。これを解決するにはどうしたらいいのでしょうか。
関係しうると思うコードを載せておきます。
下手な質問で申し訳ありませんが
ご教示いただけますようお願いします。
レイクルーツ
Prefix Verb URI Pattern Controller#Action m_show_articles GET /articles/m_show(.:format) articles#m_show articles GET /articles(.:format) articles#index POST /articles(.:format) articles#create new_article GET /articles/new(.:format) articles#new edit_article GET /articles/:id/edit(.:format) articles#edit article GET /articles/:id(.:format) articles#show PATCH /articles/:id(.:format) articles#update PUT /articles/:id(.:format) articles#update DELETE /articles/:id(.:format) articles#destroy new_user_session GET /users/sign_in(.:format) users/sessions#new user_session POST /users/sign_in(.:format) users/sessions#create destroy_user_session DELETE /users/sign_out(.:format) users/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
アーティクルコントローラー
class ArticlesController < ApplicationController before_action :set_article,only:[:edit,:update,:show,:destroy,] before_action :authenticate_user!, only:[:new,:m_show] def index @articles = Article.all end def new @article = Article.new end def edit end def create @article = Article.new article_params @article.user = current_user if @article.save redirect_to article_path(@article) else render 'new' end end def update if @article.update(article_params) redirect_to @article else render'edit' end end def show end def m_show @article = Article.find_by(user_id: params[:user_id]) end def destroy @article.destroy redirect_to @article end def article_params params.require(:article).permit(:title,:text,:image,:audio) end def set_article @article = Article.find params[:id] end end
m_show.html.erb
<p>メンバーページ</p> <%= link_to "更新", edit_article_path(@article) %><br> <%= link_to "削除", article_path(@article), method: :delete, data: { confirm: 'Are you sure?' } %><br> <h1><%= @article.title %></h1> <p><%= @article.text %></p> <% if @article.image? %> <%= image_tag @article.image.url %> <% end %> <% if @article.audio? %> <%= audio_tag @article.audio.url, :controls => true %> <% end %> <br> <%= link_to "一覧へ",articles_path %><br> <%= link_to 'TOP', root_path %>
アプリケーションHTML
<!DOCTYPE html> <html> #略 <% unless user_signed_in? %> <%= link_to "新規アーティスト登録",new_user_registration_path(@article) %> <%= link_to"アーティストログイン",new_user_session_path(@article) %> <% end %> <% if user_signed_in? %> <%= link_to 'ログアウト', destroy_user_session_path, method: :delete %> <%= link_to"マイページ",m_show_articles_path(user_id: current_user.id) %> <% end %> <%= yield %> </body> </html>
ルーツ
Rails.application.routes.draw do #get 'welcome/index' resources :articles do collection do get 'm_show' end end devise_for :users, controllers: { sessions: 'users/sessions' } end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/22 11:12