Rails如何在交叉模型系统中传递参数

时间:2015-05-31 19:36:56

标签: ruby-on-rails ruby params

我无法弄清楚将params传递给链接标记的方法。我想要做的是当有人点击Join Group链接时,Membership模型将有一个新行,其中group_id为当前组ID,用户id为当前用户ID。 Membership模型当前包含两列:user_id和group_id,用于将用户映射到组。任何人都可以帮助我解决我所犯的错误。 这是代码 组:show.html.erb

    <p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @group.title %>
</p>

<p>
  <strong>Desc:</strong>
  <%= @group.desc %>
</p>

<p>
  <strong>Creator:</strong>
  <%= @creator.first_name %>
</p>
<%= link_to 'Join Group', memberships_path(:group_id => @group.id, :user_id => current_user.id ), method: :post %>
<%= link_to 'Edit', edit_group_path(@group) %> |
<%= link_to 'Back', groups_path %>

这里是会员控制器

class MembershipsController < ApplicationController
  before_action :set_membership, only: [:show, :edit, :update, :destroy]

  # GET /memberships
  # GET /memberships.json
  def index
    @memberships = Membership.all
  end

  # GET /memberships/1
  # GET /memberships/1.json
  def show
    @membership = Membership.find(params[:id])
    @user = User.find(@membership.user_id)
    @group = Group.find(@membership.group_id)
  end

  # GET /memberships/new
  def new
    @membership = Membership.new
  end

  # GET /memberships/1/edit
  def edit
  end

  # POST /memberships
  # POST /memberships.json
  def create
    @membership = Membership.new(membership_params)

    respond_to do |format|
      if @membership.save
        format.html { redirect_to @membership, notice: 'Membership was successfully created.' }
        format.json { render :show, status: :created, location: @membership }
      else
        format.html { render :new }
        format.json { render json: @membership.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /memberships/1
  # PATCH/PUT /memberships/1.json
  def update
    respond_to do |format|
      if @membership.update(membership_params)
        format.html { redirect_to @membership, notice: 'Membership was successfully updated.' }
        format.json { render :show, status: :ok, location: @membership }
      else
        format.html { render :edit }
        format.json { render json: @membership.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /memberships/1
  # DELETE /memberships/1.json
  def destroy
    @membership.destroy
    respond_to do |format|
      format.html { redirect_to memberships_url, notice: 'Membership was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_membership
      @membership = Membership.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def membership_params
      params.require(:membership).permit(:user_id, :group_id)
    end
end

2 个答案:

答案 0 :(得分:0)

您的控制器中应该有一个方法可以处理将用户添加到组中的方法,例如:

class MembershipsController < ApplicationController

    ...

    def join_group
        @user = User.find(params[:user]
        @user.group_id = params[:group_id].to_i
    end

    ...

end

然后routes.rb需要一个指向该控制器方法的路径:

get 'memberships/join_group' => 'memberships#join_group'

将导致您将在link_to中使用memberships_join_group_path之类的路径:

<%= link_to 'Join Group', memberships_join_group_path(:group_id => @group.id, :user_id => current_user.id ) %>

您当前使用的路径是向您的index方法发送参数。

答案 1 :(得分:0)

def create

    @member = current_user.members.build(:group_id => params[:group_id])
    if @member.save
      flash[:notice] = "You have joined this group."
      redirect_to members_path
    else
      flash[:error] = "Unable to join."
      redirect_to members_path
    end
  end

并使用会员资格而非会员