Last active
August 29, 2015 14:06
-
-
Save px-amaac/57d72fe1effbb10fb030 to your computer and use it in GitHub Desktop.
Many to Many relationship.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"utf8"=>"✓", | |
"authenticity_token"=>"3WtF72DqfVNI9PSGMdiR5ZgWrzPuZAp+89hV+nZp6eA=", | |
"tag"=>{"key"=>"aoe", | |
"value"=>"aoe"}, | |
"commit"=>"Tag Photo", | |
"photo_id"=>"5"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PhotosController < ApplicationController | |
before_action :set_photo, only: [:show] | |
before_action :correct_user, only: [ :show, :edit, :update, :destroy ] | |
before_action :authenticate_user! | |
# GET /photos | |
# GET /photos.json | |
def index | |
@photos = current_user.photos | |
end | |
# GET /photos/1 | |
# GET /photos/1.json | |
def show | |
@tags = @photo.tags | |
@tag = Tag.new | |
end | |
# GET /photos/new | |
def new | |
@photo = current_user.photos.build | |
@tag = @photo.tags.build | |
end | |
# GET /photos/1/edit | |
def edit | |
end | |
# POST /photos | |
# POST /photos.json | |
def create | |
@photo = current_user.photos.create(photo_params) | |
respond_to do |format| | |
if @photo.save | |
format.html { redirect_to @photo, notice: 'Photo was successfully created.' } | |
format.json { render :show, status: :created, location: @photo } | |
else | |
format.html { render :new } | |
format.json { render json: @photo.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /photos/1 | |
# PATCH/PUT /photos/1.json | |
def update | |
respond_to do |format| | |
if @photo.update(edit_photo_params) | |
format.html { redirect_to @photo, notice: 'Photo was successfully updated.' } | |
format.json { render :show, status: :ok, location: @photo } | |
else | |
format.html { render :edit } | |
format.json { render json: @photo.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /photos/1 | |
# DELETE /photos/1.json | |
def destroy | |
@photo.image = nil | |
@photo.save | |
@photo.destroy | |
respond_to do |format| | |
format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' } | |
format.json { head :no_content } | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_photo | |
@photo = Photo.find(params[:id]) | |
end | |
def correct_user | |
@photo = current_user.photos.find_by(id: params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def photo_params | |
params.require(:photo).permit(:title, :image, :tags_attributes => [:id, :key, :value, :_destroy]) | |
end | |
def edit_photo_params | |
params.require(:photo).permit(:title) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="panel row"> | |
<p id="notice"><%= notice %></p> | |
<p> | |
<strong>Title:</strong> | |
<%= @photo.title %> | |
</p> | |
<%= image_tag (@photo.image.url(:medium)) %> | |
<p> | |
<strong>User:</strong> | |
<%= @photo.user.username %> | |
</p> | |
<%= link_to 'Edit', edit_photo_path(@photo), :class => "button radius round tiny" %> | | |
<%= link_to 'Destroy', @photo, method: :delete, data: { confirm: 'Are you sure?' }, :class => "button tiny alert radius round tiny" %> | | |
<%= link_to 'Back', photos_path, :class => "button [succes tiny radius round ]" %> | |
<h3>Tags</h3> | |
<p> | |
<%= form_for([@photo, @tag]) do |f| %> | |
<div class="field"> | |
<%= f.label :key %> | |
<%= f.text_field :key %> | |
</div> | |
<div class="field"> | |
<%= f.label :value %> | |
<%= f.text_field :value %> | |
</div> | |
<%= f.submit "Tag Photo", class: "button success" %> | |
<% end %> | |
</p> | |
<% @tags.each do |tag| %> | |
<p>Key: <%= tag.key %></p> | |
<p>Value: <%= tag.value %></p> | |
<% end %> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(0.1ms) begin transaction | |
SQL (2.4ms) INSERT INTO "tags" ("created_at", "key", "updated_at", "value") VALUES (?, ?, ?, ?) [["created_at", "2014-09-05 04 | |
:02:04.349445"], ["key", "heydude"], ["updated_at", "2014-09-05 04:02:04.349445"], ["value", "dudehey"]] | |
(4.3ms) commit transaction |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for | |
If your resource has associations defined, for example, you want to add comments to the document given that the routes are set correctly: | |
<%= form_for([@document, @comment]) do |f| %> | |
... | |
<% end %> | |
Where @document = Document.find(params[:id]) and @comment = Comment.new. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment