私のプロジェクトに以下の宝石を実装しようとしています。https://github.com/aliibrahim/Favorite-Lists。この例では、現在のユーザーをお気に入りとしていないものとしてリストしています。私の目標は、地図マーカーを好きにしていなくてもお気に入りにログインする必要がない訪問者を持つことです。お気に入りから、私はそれらを保存し、お気に入りのマップマーカーのリストを生成します。私のモデルであり、ここでお気に入りのマップマーカーにacts_as_saveable宝石を実装し、お気に入りのリストを生成する - ruby on rails
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<!--Basic Table-->
<div class="panel panel-green margin-bottom-40">
<div class="panel-heading">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<div class = "name"></div>
</div>
<div class="panel-body">
<div class="thumbnail-img">
<div class="overflow-hidden">
<%= image_tag 'creative/main/img3.jpg', :class => 'img-responsive' %>
</div>
</div>
<div class="row">
<div class="col-sm-12 text-center">
<button class="btn-u btn-u-lg rounded-4x btn-u-green" type="button"><i class="fa fa-check-circle" aria-hidden="true"></i> Check Availability</button>
<button class="btn-u btn-u-lg rounded-4x btn-u-green" type="button"><i class="fa fa-plus-circle" aria-hidden="true"></i> Add to Favorites</button>
**<%= form_tag(controller: "maps", action: "favorite", method: "post")%>
<%= submit_tag "Favorite"%>**
</div>
</div>
</div>
<table class="table paneltb">
</table>
</div>
<!--End Basic Table-->
</div>
</div>
:今のところ、これは私がビューから始まるテストしていで、私は自分のコードに持っているものである
class Map < ApplicationRecord
include PgSearch
pg_search_scope :search, against: [:number, :tab_id, :zip_postal, :city]
acts_as_saveable
attr_accessor :file
def self.import(file)
spreadsheet = Roo::Spreadsheet.open(file.path)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
map = find_by(id: row["id"]) || new
map.attributes = row.to_hash
map.save!
end
end
end
そして最後に、これは私のコントローラです:
class MapsController < ApplicationController
def index
@maps = Map.all
@hash = Gmaps4rails.build_markers(@maps) do |map, marker|
marker.lat map.latitude
marker.lng map.longitude
marker.json({:id => map.id,
:number => map.number,
:tab_id => map.tab_id,
:face => map.face,
:street => map.street_,
:street_1 => map.street_1,
:position => map.position,
:street_2 => map.street_2,
:side_corner => map.side_corner,
:city => map.city,
:zip_postal => map.zip_postal,
:latitude => map.latitude,
:longitude => map.longitude
})
end
end
def favorite
@map = Map.new(:number => 'Favorite Site')
@map.save
redirect_to :back
flash[:success] = "favorited"
end
def unfavorite
@maps.unsave
map.save
redirect_to :back
flash[:success] = "unfavorited"
end
def import
Map.import(params[:file])
redirect_to authors_posts_path, notice: "Locations Imported!"
end
def show
@maps = Map.find(params[:id])
end
end
私のマップ上の私のモーダルのお気に入りボタンをクリックすると、それは通過しますが、私はどこにいるのか分かりません。ここでは、端末で次のようになります。ここでは
Started POST "/favorite?method=post" for 127.0.0.1 at 2017-07-16 17:28:41 -0700
Processing by MapsController#favorite as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WxMK/PsUSrd35UssF6CviTfcPoHajRkJaxnb5zZUAWFUvxHgjY/czzxwRpyp+vPMLjiDnBbSJtfCRdCNhGuSvw==", "commit"=>"Favorite", "method"=>"post"}
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO "maps" ("number", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["number", "Favorite Site"], ["created_at", 2017-07-17 00:28:41 UTC], ["updated_at", 2017-07-17 00:28:41 UTC]]
(32.5ms) COMMIT
DEPRECATION WARNING: `redirect_to :back` is deprecated and will be removed from Rails 5.1. Please use `redirect_back(fallback_location: fallback_location)` where `fallback_location` represents the location to use if the request has no HTTP referer information. (called from favorite at /home/krav/Desktop/martin_outdoor_media/app/controllers/maps_controller.rb:29)
Redirected to http://localhost:3000/maps
Completed 302 Found in 36ms (ActiveRecord: 33.0ms)
これはその一部です。 –