0

导轨版本 5.1.4

你好,

我无法将 simple_form_for 的字段值immo_type保存到我的控制器页面

#app\controllers\pages_controller.rb
class PagesController < ApplicationController
  skip_before_action :configure_permitted_parameters, only: [:home]

  def home
    @purchase = Purchase.new
    @immo_type = params[:immo_type]

  end
end

风景 :

#app\views\pages\home.html.erb    
<%= simple_form_for @purchase, url: purchases_path(@purchase), :method => :post do |f| %>
  <%= f.input :address %>
  <%= f.input :immo_type %>
  <%= f.button :submit, "Search" %>

该模型 :

#app\models\purchase.rb
class Purchase < ApplicationRecord
    belongs_to :user
    has_many :users, :through => :searches
end

抱歉,我是 ruby​​ on rails 的新手!!!

我尝试<%= debug params %>在我的 Pages 控制器中放置或“加注”。我有这个消息

--- !ruby/object:ActionController::Parameters 参数:!ruby/hash:ActiveSupport::HashWithIndifferentAccess 控制器:页面操作:允许主页:false

请问你能帮帮我吗 ?

4

2 回答 2

0
class PagesController < ApplicationController
  skip_before_action :configure_permitted_parameters, only: [:home]

  def create
      purchase=Purchase.new(purchase_params)
      if purchase.save
       flash[:notice] = "purchase saved successfully."
       redirect_to root_path
      else
       flash[:error] = purchase.errors.full_messages.to_sentence
       redirect_to :back
      end
    end

    private
    def purchase_params
      params.require(:purchase).permit(:address,immo_type)
    end
end
于 2017-11-29T19:48:42.770 回答
0

所以,首先我需要使用带有主页和索引视图的搜索控制器

class SearchesController < ApplicationController

    def home
        @searches = params[:immo_type]
    end

    def index
        @purchases = Purchase.search_by_immo_type_and_address("#{params[:purchase][:immo_type]}")
    end
end

这是我使用 Pg_Search gem 购买的模型

class Purchase < ApplicationRecord
    belongs_to :user
    has_many :users, :through => :searches

    include PgSearch
    pg_search_scope :search_by_immo_type_and_address, against: [:immo_type]
end

和 simple_form_for 的视图

<%= simple_form_for :purchase, url: searches_url, method: :get do |f| %>
            <%= f.input :address %> 
            <%= f.input :immo_type %>
            <%= f.button :submit, "Rechercher"%>
<% end %>

我希望它可以帮助你

于 2017-12-12T12:01:01.710 回答