2016-12-08 9 views
0

ユーザー、投稿、ジムの3つの主要モデルでサイトを作成します。ユーザーは、自分のモデル(User.post)から、またはジムの管理者であれば、ジムのモデル(Gym.post)から投稿することができます。Rails:2つのモデル間で条件付きの同じ「作成」アクション

私は同じポストコントローラとポストフォームを使ってジムやユーザーの投稿をしていますが、コントローラの "作成"アクションでは2つを区別できません。

class PostsController < ApplicationController 
    before_action :logged_in_user, only: [:create, :destroy] 
    before_action :correct_user, only: :destroy 

    def create 
    if (gym.gym_admin == current_user.id) 
     @post = gym.posts.build(post_params) 
     if @post.save 
     flash[:success] = "Post!" 
     redirect_to "/gyms/#{gym.id}" 
     else 
     @feed_items = [] 
     render 'static_pages/home' 
     end 
    else 
     @post = current_user.posts.build(post_params) 
     if @post.save 
     flash[:success] = "Post!" 
     redirect_to root_url 
     else 
     @feed_items = [] 
     render 'static_pages/home' 
     end 
    end 
    end 

    def destroy 
    @post.destroy 
    flash[:notice] = "Post deleted" 
    redirect_to request.referrer || root_url 
    end 

    private 

    def post_params 
    params.require(:post).permit(:post_type, :title, :content, :picture,  :body_parts, 
           :duration, :equipment, :calories, :protein, 
           :fat, :carbs, :ingredients, :tag_list, 
           :postable_id, :postable_type) 
    end 

    def correct_user 
    @post = current_user.posts.find_by(id: params[:postable_id]) 
    redirect_to root_url if @post.nil? 
    end 

    def gym 
    @gym = Gym.find_by(params[:id]) 
    end 

end 

とモデル:

class Post < ApplicationRecord 

    belongs_to :user 
    belongs_to :gym 
    belongs_to :postable, polymorphic: true 

class User < ApplicationRecord 

    has_many :posts, as: :postable, dependent: :destroy 
    has_many :gyms 

class Gym < ApplicationRecord 

has_many :posts, as: :postable, dependent: :destroy 
belongs_to :user 

今Rught、このcreateアクションはジムのモデルからの投稿を作成します。条件の前半を削除すると、Userモデルからのみポストされます。

すべてのヘルプは大歓迎ですが、あなたが

+0

2つの異なるコントローラを使用すると、より反復的に思えるかもしれませんが、おそらくもっと簡単になります。 –

答えて

0

私は私が完全に条件ロジックを削除し、それを固定し、私はちょうどコントローラで2つの別々のカスタムアクションを作成し、それらを異なるリンクから呼び出しました。すなわち、 :actions => create_gym/create_user

0

私は好奇心旺盛になり感謝何gym.gym_admin(結果的に「DEF作成」下の行全体が)私はどこか他の参照に表示されていないため、と評価されます。

私の疑惑は、あなたがその関係が正常に機能しているいったん

if (gym.gym_admin.id == current_user.id) 

または

if (gym.gym_admin == current_user) 

if (gym.gym_admin == current_user.id) 

を変更したいということです。

また、ユーザーがジムの管理者であるかどうかに関係なく、ポスト・パラームをgym_idに送信することもできます。そして、いずれかを経由してアクセス:

/gym/:id 
@posts = Post.where('gym_id = ?', params[:id]) 

または

/user/:id 
@posts = Post.where('user_id = ?', params[:id]) 
+0

gym.gym_adminの値は、current_user.idから生成され整数になる新しい形式のgym#のhidden_​​tagです。 ''% '= f.hidden_​​field:gym_admin、:value => current_user.id%> '私は今、" create "アクションをPostsControllerのアクションに分けました:' def create_gym_post'と 'def create_user_post'私はポストパラメータをこれらの> postの投稿のうちの一つまたは他のものを通して送る方法を見つけることができるはずです: 'posts#create_gym_post' ' >' post 'posts'、 'posts#create_user_post ''しかし、私はそれを行う方法が似ているようだ... – ETSchmidt

+0

その場合、あなたは条件付きでメソッドを変更することができた/人が管理者であるかどうかに基づいて別のフォームを表示できますか? – abax

関連する問題