Home>

I would be grateful if you could answer! Please! !!

Premise/I want to realize

I want to implement it so that I can join a group created by another user by pressing the "Join" button.
I have already implemented it so that the "Join" button is not displayed for the group I created.

Corresponding source code
# groups/index.html.erb
 Group list 

<% @ groups.each do | group |%>
    
    <% unless GroupUser.exists? (Group_id: group.id, user_id: current_user.id)%>
        
           Join  ### I want to implement it so that I can participate by clicking this button
        
    <% else%>
        
           Participating 
        
    <% end%>
      
      <% = group.name%>
      
      
      <% = group.content%>
      
    
<% end%>
Rails.application.routes.draw do
  devise_for: users
  root to: "tweets # index"
  resources: groups, only: [: index,: new,: create]
end
# groups_controller.rb
class GroupsController</pre>
<ul>
<li>I don't know if it's necessary, but just in case ...</li>
</ul>
<pre><code data-language = "model">#user
class User</pre>
<pre><code data-language = "migration">#groups table
class CreateGroups</pre>

<p>After writing the following code, other users could join an existing group, but the creator of the group left.<br />
I also got the error no template for interactive request ?.</p>
<pre><code data-language = "routes">resources: groups, only: [: index,: new,: create] do
  member do
    get: join
  end
end
#groups
(abridgement)
def join
  @group = Group.find (params [: id])
  @ group.users<<current_user
  @ group.save
end
(abridgement)
# groups/index.html.erb
<% = link_to'join', join_group_path (group)%>
version

rails 6.0.0
mysql

  • Answer # 1

    I was able to resolve myself safely. The reasons and thoughts that the error was resolved are as follows.
    ・ If you press the join button, an error will occur.
    ・ When you return to the top page, you can confirm that you are participating.
    ・ Is it an error because there is no processing after joining?
    Adding rediret_to root_path to controller fixed the error!

    Rails.application.routes.draw do
      devise_for: users
      root to: "tweets # index"
      resources: groups, only: [: index,: new,: create] do
        member do
          get: join
        end
      end
    end
    #groups
    (abridgement)
    def join
      @group = Group.find_by (id: params [: id])
      if! @ group.users.include? (Current_user)
        @ group.users<<current_user
        redirect_to root_path
      end
    end
    (abridgement)
    #index
     Group list 
    <% @ groups.each do | group |%>
        
        <% if user_signed_in?%>
          <% unless GroupUser.exists? (Group_id: group.id, user_id: current_user.id)%>
              
              <% = link_to'join', join_group_path (group)%>
              
          <% else%>
              
                 Participating  ## Planned to be implemented using link_to so that you can see the details of the group
              
          <% end%>
        <% end%>
          
          <% = group.name%>
          
          
          <% = group.content%>
          
        
    <% end%>