使用按钮将记录添加到'has_many:through'关联

时间:2022-09-26 10:09:34

This is my first Rails app and have hit another wall. I have a User model and a Country model. They have a many-to-many relationship, which I join together with a Trip model.

这是我的第一个Rails应用程序,并打了另一面墙。我有一个用户模型和一个国家模型。他们有多对多的关系,我和Trip模型一起加入。

A user can maintain a list of countries that they have been to. On the Country page, I want to have a simple bootstrap button so the current_user can add or remove the country to their list.

用户可以维护他们去过的国家列表。在“国家/地区”页面上,我想要一个简单的引导按钮,以便current_user可以在其列表中添加或删除国家/地区。

I am using a partial that looks like the below to at least render buttons on all the pages.

我正在使用看起来像下面的部分至少在所有页面上渲染按钮。

_add_remove_countries.html.erb

<% if @user.countries.exists?(@country.id) %>
    <%= form_for(@user) do |f| %>
      <%= f.submit "Remove Country", class: "btn btn-info" %>
    <% end %>
<% else %>  
    <%= form_for(@user) do |f| %>
      <%= f.submit "Add Country", class: "btn btn-info" %>
    <% end %>
<% end %>

I have tried a few different things, with no luck so I have just reverted to the basic structure. I am currently using a form_for, however that is just what has worked best so far, I am not tied to that solution.

我尝试了一些不同的东西,没有运气,所以我刚刚恢复到基本结构。我目前正在使用form_for,但这是目前为止效果最好的,我并不依赖于该解决方案。

Below are my controllers if needed, I have not set up a Trips controller as I am only using it to join the User and Country Model (maybe I need to set one up?).

下面是我的控制器,如果需要,我还没有设置Trips控制器,因为我只使用它来加入用户和国家模型(也许我需要设置一个?)。

users_controller.rb

class UsersController < ApplicationController

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    @countries = Country.all
  end 

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      session[:user_id] = @user.id
      redirect_to @user
    else
      render 'new'
    end
  end

  def update
      redirect_to user_path
  end


  private 

    def user_params
        params.require(:user).permit(:username, :email, :password, :password_confirmation)
    end


end

countries_controller.rb

class CountriesController < ApplicationController

    before_action :require_user, only: [:index, :show]

  def index
    @countries = Country.all
    @sort = CS.countries.sort_by {|key, value| value}
    @sort = @sort.first @sort.size - 2

  end

  def show
    @country = Country.find(params[:id])
    @user = User.find(session[:user_id])
  end

end

1 个解决方案

#1


0  

my suggestion using collection_select (and click link in case you would like to know more about collection_select) to add countries to user while editing user, below is sample code to help (using edit method)

我的建议使用collection_select(如果你想了解更多关于collection_select的点击链接)在编辑用户时向用户添加国家,下面是帮助的示例代码(使用编辑方法)

user_controller

class UsersController < ApplicationController

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    @countries = Country.all
  end 

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      session[:user_id] = @user.id
      redirect_to @user
    else
      render 'new'
    end
  end

  # ---> here additional code to edit method
  def edit
    @user = User.find(params[:id])
    @countries = Country.all
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      redirect_to user_path
    else
      render 'new'
    end
  end

  private 

    def user_params
        params.require(:user).permit(:username,
          :email,
          :password,
          :password_confirmation,
          :country_ids => [])
        # country_ids is an array that will save data for countries that user have been to
    end
end

now this is the fun one, in your views\user\edit.html.erb

现在这是一个有趣的,在您的views \ user \ edit.html.erb中

<%= form_for @user do |f| %>
  <!--  simple  -->
  <p>Email : </p>
  <p><%= f.text_field :email %></p>

  <!-- if you using bootstrap -->
  <div class="row form-group">
    <%= f.label "email", :class => 'control-label col-sm-3' %>
    <div class="col-sm-5">
      <%= f.text_field :email, :class => 'form-control' %>
    </div>
  </div>
  <!-- other inputs (password / password_confirmation) -->
  <%= f.collection_select :country_ids, @countries, :id, :name, {}, { multiple: true, class: 'form-control' } %>
<% end %>

#1


0  

my suggestion using collection_select (and click link in case you would like to know more about collection_select) to add countries to user while editing user, below is sample code to help (using edit method)

我的建议使用collection_select(如果你想了解更多关于collection_select的点击链接)在编辑用户时向用户添加国家,下面是帮助的示例代码(使用编辑方法)

user_controller

class UsersController < ApplicationController

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
    @countries = Country.all
  end 

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      session[:user_id] = @user.id
      redirect_to @user
    else
      render 'new'
    end
  end

  # ---> here additional code to edit method
  def edit
    @user = User.find(params[:id])
    @countries = Country.all
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      redirect_to user_path
    else
      render 'new'
    end
  end

  private 

    def user_params
        params.require(:user).permit(:username,
          :email,
          :password,
          :password_confirmation,
          :country_ids => [])
        # country_ids is an array that will save data for countries that user have been to
    end
end

now this is the fun one, in your views\user\edit.html.erb

现在这是一个有趣的,在您的views \ user \ edit.html.erb中

<%= form_for @user do |f| %>
  <!--  simple  -->
  <p>Email : </p>
  <p><%= f.text_field :email %></p>

  <!-- if you using bootstrap -->
  <div class="row form-group">
    <%= f.label "email", :class => 'control-label col-sm-3' %>
    <div class="col-sm-5">
      <%= f.text_field :email, :class => 'form-control' %>
    </div>
  </div>
  <!-- other inputs (password / password_confirmation) -->
  <%= f.collection_select :country_ids, @countries, :id, :name, {}, { multiple: true, class: 'form-control' } %>
<% end %>