2014-03-25 3 views
0

다음을 설정하려고합니다. 사용자가 그룹을 통해 많은 그룹을 가지고 있고 그룹에 많은 이벤트가 있고 이벤트에 게시물이 여러 개 있습니다.중첩 된 속성에 대한 조건부 레일

모든 이벤트가있는 그룹을 보여주기 위해 드롭 다운에서 올바른 그룹을 선택하고 댓글을 쓰고 제출하여 새 게시물을 작성할 수 있기를 바랍니다. 즉, 게시물이 생성되지만,이 event_ids (또는 의견)이없는 나는 현재 게시물을 만들기 위해 collection_select를 사용하고 있지만, EVENT_ID는 액티브 전달 받고되지 않습니다

class User < ActiveRecord::Base 
    has_many :memberships 
    has_many :groups, through: :memberships 
    has_many :posts 
end 

class Membership < ActiveRecord::Base 
    belongs_to :group 
    belongs_to :user 
end 

class Group < ActiveRecord::Base 
    has_many :memberships 
    has_many :events, dependent: :destroy 
    has_many :users, through: :memberships 
end 

class Event < ActiveRecord::Base 
    belongs_to :group 
    has_many :posts 
end 

class Post < ActiveRecord::Base 
    belongs_to :event 
    belongs_to :user 
end 



class GroupsController < ApplicationController 
    def show 
    #define new post 
    @new_post = Post.new 
    end 
end 


class PostsController < ApplicationController 
    def create 
    if @post = Post.create(params[post_params]) 
     flash[:success] = "Post Created!" 
    else 
     redirect_to group_url 
    end 
    end 

    private 
    def post_params 
     params.require(:post).permit(:event_id, :comment) 
    end 
end 


<h1>New Post:</h1> 
<%=form_for([@new_post]) do |f| %> 
<%= render 'shared/error_messages', object: f.object %> 
    <div class = "field"> 
     <%= f.label :event_name %> 
     <%= f.collection_select(:event_id, Event.all, :id, :title) %> 
    </div> 
    <div class = "field"> 
     <%= f.text_area :comment, placeholder: "New Post..." %> 
    </div> 
    <%=f.submit "Submit", class: "btn btn-large btn-primary" %> 
<%end%> 

나는 느낌이 route가 중첩되어 있기 때문에 group_id가 Posts 컨트롤러로 전달되지 않으므로 결코 설정할 수 없습니다. 하지만

+0

주처럼보기 뭔가에 추가해야 을 user_id를 추가하려면 다시

를 PARAMS에 전달하지 말아야 있도록 PARAMS에서 추출 된 해시 : 나는 결국 CURRENT_USER를 추가 할 수 있습니다 이드도 포스트에 올랐지 만, 첫 번째로 전달 된 event_id 및 주석 – kyle

+1

Post.create (post_params) 대신 Post.create (post_params)를 전달할 수 있습니까? (params [post_params]) –

+0

좋습니다! 그 덕분에, 고마워! 그 차이점을 이해하도록 도와 줄 수 있습니까? – kyle

답변

1

당신이

post_params 실제로 가득 (PARAMS [post_params]) 대신 Post.create의 Post.create (post_params를) 통과를 시도 할 수 있습니다 ... 그 이상 잘못이 많이있을거야 당신이

<%= f.hidden_field :user_id, value: current_user.id %> 
+0

고마워, 그건 속임수 였어! 숨겨진 필드를 통해 user_id를 추가하는 방법에 대한 아이디어가 있습니까? – kyle

+0

병을 지금 추가하십시오. –

+0

user_id 너무 훌륭합니다. 감사! – kyle