如何绕过表单并使用Rails中的link_to直接将信息传递给创建操作

时间:2018-01-09 21:06:57

标签: ruby-on-rails ruby ruby-on-rails-3

设置

App有3个型号。成员模型,支持模型和参与模型。成员模型跟踪我的Web应用程序的所有用户。支持模型跟踪用户可以支持我们组织的许多不同方式。 Involvement模型通过跟踪member_id和support_id来显示成员处于活动状态的支持区域,从而连接其他两个模型。

当前功能

在会员的个人资料中,他们可以查看支持选项卡,该选项卡可以从支持模型查询他们可以支持的所有不同区域。然后,它检查是否存在包含其id和该支持区域的id的参与,并说明它们当前是否支持。

期望的结果

我希望有一个可以点击的链接,可以创建或删除参与。因此,如果他们目前在财务方面提供支持,它将声明他们是并提供一个链接来停止支持这种方式,点击后将删除参与记录;反之亦然,如果他们不支持财务,则会显示一个链接以开始支持,该链接将创建参与记录。 (最终我将集成一个切换功能,允许成员来回切换,但我需要先解决这个问题。)

问题

有没有办法通过link_to向'创建'发送参数。或者'销毁'实现此功能的行动?目前我正在执行以下操作但收到错误:

<%= 
    link_to "Start Supporting", 
    involvements_path(member_id: current_member.id, 
                      support_id: support_area.id), 
    method: :post 
%>

错误

ActionController::ParameterMissing in InvolvementsController#create
param is missing or the value is empty: involvement

当我执行bybbug时,我得到params的以下输出:

(byebug) params
<ActionController::Parameters {"_method"=>"post", 
                               "member_id"=>"1", 
                               "support_id"=>"2",                                                                                                                                             
                               "controller"=>"involvements", 
                               "action"=>"create"} 
                                permitted: false>

所以,看起来params是空的......它不是通过白名单吗?有什么想法吗?

代码

rake routes

    involvements GET    /involvements(.:format)           involvements#index
                 POST   /involvements(.:format)           involvements#create
 new_involvement GET    /involvements/new(.:format)       involvements#new
edit_involvement GET    /involvements/:id/edit(.:format)  involvements#edit
     involvement GET    /involvements/:id(.:format)       involvements#show
                 PATCH  /involvements/:id(.:format)       involvements#update
                 PUT    /involvements/:id(.:format)       involvements#update
                 DELETE /involvements/:id(.:format)       involvements#destroy

应用程序/模型/ involvement.rb

class Involvement < ApplicationRecord
  belongs_to :member
end

分贝/ schema.rb

create_table "involvements", force: :cascade do |t|
   t.bigint "member_id"
   t.integer "support_id"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.index ["member_id"], name: "index_involvements_on_member_id"
end

应用程序/视图/简档/ show.html.erb

<% if @user == 'Owner' %>

  <% if @show_page == 'account' %>
    <%= render 'profile_account' %>
  <% elsif @show_page == 'main' %>
    <%= render 'profile_main', locals: { profile: @profile, 
                                         user: @user } %>
  <% elsif @show_page == 'support' %>
    <%= render 'profile_support', locals: { profile: @profile, 
                                            user: @user, 
                                            supports: @supports, 
                                            involvement: @involvements 
                                          } %>
  <% else %>
    <script type="text/javascript">
        window.location.href="<%= 
               member_profile_path(current_member.profile.u_name) 
        %>"
    </script>
  <% end %>

<% else %>
  <%= render 'profile_main', locals: { profile: @profile, 
                                       user: @user } %>
<% end %>

应用程序/视图/简档/ _profile_support.html.erb

<h1>This is the Support Page</h1>

<table style="width:50%">
  <tr>
    <th>Support Area</th>
    <th>Support Status</th>
    <th>Change Status</th>
  </tr>
  <% @supports.each do |support_area| %>
    <tr>  
      <th>
        <%= link_to support_area.title, support_page_path(support_area.title) %>
      </th>
      <th>
        <% @presence_check = Involvement.exists?(member_id: current_member.id, 
                                                 support_id: support_area.id) %>
        <% if @presence_check %>
          Supporting
        <% else %>
          Not Supporting
        <% end %>
      </th>
      <th>
        <%= link_to "Start Supporting", 
                    involvements_path(member_id: current_member.id, 
                                      support_id: support_area.id), 
                    method: :post %>
      </th>
    </tr>
  <% end %>
</table>

应用程序/控制器/ involvements_controller.rb

class InvolvementsController < ApplicationController
  before_action :set_involvement, only: [:show, :edit, :update, :destroy]

  def index
    @involvements = Involvement.all
  end

  def show
  end

  def new
    @involvement = Involvement.new
  end

  def edit
  end

  def create    
    @involvement = Involvement.new(involvement_params)
    # byebug
    respond_to do |format|
      if @involvement.save
        format.html { redirect_to @involvement, 
                      notice: 'Involvement was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

  def update
    respond_to do |format|
      if @involvement.update(involvement_params)
        format.html { redirect_to @involvement, 
                      notice: 'Involvement was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  def destroy
    @involvement.destroy
    respond_to do |format|
      format.html { redirect_to involvements_url, 
                    notice: 'Involvement was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, 
    # only allow the white list through.

    def involvement_params
      # byebug
      params.require(:involvement).permit(:member_id, :support_id)
    end
end

1 个答案:

答案 0 :(得分:2)

问题在于如何将参数传递给控制器​​,以及如何在控制器中使用它们。你的控制器这样做:

def involvement_params
  params.require(:involvement).permit(:member_id, :support_id)
end

这需要:member_id:support_id嵌套在involvement下,但您将它们作为顶级参数传递给网址。请尝试更新您的链接:

<%= link_to "Start Supporting",
  involvements_path(
    involvement: {
      member_id: current_member.id,
      support_id: support_area.id
    }
  ),
  method: :post 
%>

这应该正确嵌套参数。