Rails101练习中,所涉及网址与atom 路径中的对应关系

一 app/views/groups/show.html.erb和app/views/groups/index.html.erb比较

  1. app/views/groups/show.html.erb中: +<%= link_to("Quit Group", quit_group_path(@group), method: :post, class: "btn btn-default")%> 中的路径,对应的网址是:

+<%= link_to("Join group", join_group_path(@group), method: :post, class: "btn btn-default")%>
中的路径,对应的网址是

+ <%= link_to("Write a Post", new_group_post_path(@group), class: "btn btn-primary pull-right") %>
中的路径,对应的网址是:http://localhost:3000/groups/18/posts/new

+ <%= link_to("Edit", edit_group_path(@group), class: "btn btn-primary pull-right") %>
中的路径,对应的网址是:http://localhost:3000/groups/18/edit

<%= @group.title %>


对应的结果是

<%= @group.description %>


对应的结果是
  1. app/views/groups/index.html.erb +<%= link_to("New group", new_group_path, class: "btn btn-primary pull-right") %> 中的路径,对应的网址是:http://localhost:3000/groups/new

+

<%= link_to(group.title, group_path(group)) %>

<%= group.description %>

+

<%= group.user.email %>

+<%= link_to("Delete", group_path(group), class: "btn btn-sm btn-default", method: :delete, data: { confirm: "Are you sure?" } )%>
中的路径,对应的网址是: http://localhost:3000/groups/18

  1. 总结: 1)#http://localhost:3000/groups/18/edit 该网址在app/views/groups/show.html.erb 需写成 edit_group_path(@group) 该网址在app/views/groups/show.html.erb 需写成 edit_group_path(group) 2)#要显示group,description部分的内容 在app/views/groups/show.html.erb, 需写成 <%= @group.description %> 在app/views/groups/show.html.erb , 需写成 <%= group.description %>

二 app/views/posts/edit(new).html.erb和app/views/groups/edit(show).html.erb比较

  1. app/views/posts/edit(new).html.erb <%= simple_form_for [@group, @post] do |f| %>

2.app/views/groups/edit.html.erb 中
<%= simple_form_for @group do |f| %>

  1. 总结:

三 app/views/common/_navbar.html.erb

+

  • <%= link_to("登录", new_user_session_path) %>

  • 中的路径,对应的网址是:http://localhost:3000/users/sign_in

    +

  • <%= link_to("My Groups", account_groups_path) %>

  • 中的路径,对应的网址是:http://localhost:3000/account/groups

    +

  • <%= link_to("My Posts", account_posts_path) %>

  • 中的路径,对应的网址是:http://localhost:3000/account/posts

    +

  • <%= link_to("退出", destroy_user_session_path, method: :delete) %>

  • 中的路径,对应的网址是::http://localhost:3000/users/sign_out

    四 app/views/account/groups/index.html.erb 与 app/views/groups/index.html.erb
    1.app/views/account/groups/index.html.erb
    <% @groups.each do |group| %>


    #
    <%= link_to(group.title, group_path(group))%>
    <%= render_group_description(group) %>
    <%= group.posts.count %>
    <%= group.updated_at %>

    2.app/views/groups/index.html
    <% @groups.each do |group| %>


    #
    <%= link_to(group.title, group_path(group)) %>
    <%= render_group_description(group) %>
    <%= group.user.email %>

    3.app/views/posts/index.html
    + <% @posts.each do |post| %>


    <%= post.content %>
    <%= post.group.title %>
    <%= post.updated_at %>

    <%= link_to('Edit', edit_group_post_path(post.group, post), class: "btn btn-default btn-xs") %>
    中对应的网址是:http://localhost:3000/groups/18/posts/48/edit

    <%= link_to('Delete', group_post_path(post.group, post), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-default btn-xs") %>

    中对应的网址是:http://localhost:3000/groups/posts

    4.总结:

    四 app/controllers/groups_controller.rb 与 app/controllers/posts_controller.rb 比较
    1.app/controllers/groups_controller.rb
    +redirect_to groups_path
    中对应的网址是:http://localhost:3000/groups

    +redirect_to group_path(@group)
    中对应的网址是: http://localhost:3000/groups/18

    2.app/controllers/posts_controller.rb
    +redirect_to group_path(@group)
    中对应的网址是:http://localhost:3000/groups/18

    +redirect_to account_posts_path
    中对应的网址是:http://localhost:3000/account/posts

    五 实做练习。
    1.如何编辑atom ,得到如下图的效果:


    第一步:
    app/controllers/posts_controller.rb
    before_action :authenticate_user!

    def new
    @group = Group.find(params[:group_id])
    @post = Post.new
    end

    def create
    @group = Group.find(params[:group_id])
    @post = Post.new(post_params)
    @post.group = @group
    @post.user = current_user

    if @post.save
      redirect_to group_path(@group)
    else
      render :new
    end
    

    end

    第二步 app/views/groups/show.html.erb
    <% if current_user && current_user.is_member_of?(@group) %>
    <%= link_to("Write a Post", new_group_post_path(@group), class: "btn btn-primary pull-right") %>
    <% end %>

    3.如何编辑atom, 得到如下效果:


    第一步 app/views/groups/index.html.erb

    第一步 app/controllers/groups_controller.rb
    before_action :authenticate_user! , only: [:new, :create, :edit, :update, :destroy]
    def index
    @groups= Group.all
    end

    def new
    @group = Group.new
    end

    def create
    @group = Group.new(group_params)
    @group.user = current_user
    if @group.save
    current_user.join!(@group)
    redirect_to groups_path
    else
    render :new
    end
    end

    private
    def group_params
    params.require(:group).permit(:title, :description)
    end
    end

    3.如何编辑atom, 得到如图的效果:


    第一步 app/controllers/groups_controller.rb
    before_action :find_group_and_check_permission, only: [:edit, :update, :destroy]
    def index

    def edit
    end

    def update
    if @group.update(group_params)
    redirect_to groups_path, notice: "Update Success"
    else
    render :edit
    end
    end

    def destroy
    @group.destroy
    flash[:alert] = "Group deleted"
    redirect_to groups_path
    end

    def find_group_and_check_permission
    @group = Group.find(params[:id])

    if current_user != @group.user
      redirect_to root_path, alert: "you have no permission!"
    end
    

    end

    第二步 app/views/groups/index.html.erb
    <% if current_user && current_user == group.user %>
    <%= link_to("Edit", edit_group_path(group), class: "btn btn-sm btn-default")%>
    <%= link_to("Delete", group_path(group), class: "btn btn-sm btn-default", method: :delete, data: { confirm: "Are you sure?" } )%>
    <% end %>

    4.如何编辑atom 编辑器,得到如图效果:



    第一步,如3.
    第二部,第二步 app/views/groups/show.html.erb
    <% if current_user && current_user == @group.user %>
    <%= link_to("Edit", edit_group_path(@group), class: "btn btn-primary pull-right") %>
    <% end %>

    Comments

    comments powered by Disqus