Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

看不懂中级作业里面groups_controller和posts_controller下面CRUD中”create”的代码,麻烦请解释一下 #80

Open
dyz2102 opened this issue Oct 24, 2016 · 1 comment

Comments

@dyz2102
Copy link

dyz2102 commented Oct 24, 2016

No description provided.

@dyz2102
Copy link
Author

dyz2102 commented Oct 24, 2016

【来自ray16897188】
就那groups的来说:

def create 
    @group = Group.new(group_params)   #根据你页面(view)发过来的参数,创建一个新的group数据条目(model)
    @group.user = current_user            #把这条group条目的user属性设置成当前的用户current_user

    if @group.save                   #尝试把这个group条目存到数据库(sqlite3)中,如果存成功了的话就
        current_user.join!(@group)               #把这个group添加到当前用户所加入的groups中去
        redirect_to groups_path                       #再让页面返回到所有群组列表的那个页面
    else                                            #如果存失败了(比如validates没有通过,就不会把这条group数据条目存到sqlite3数据库中)那么就
        render :new                      #直接让页面再显示创建新群组的那个页面
    end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant