From 6c0827a1706ebaf625d3bd509da36434e7cba426 Mon Sep 17 00:00:00 2001 From: YOSHIDA Hiroki Date: Mon, 25 Aug 2014 02:50:42 +0900 Subject: [PATCH] Improve methods for routes --- app/controllers/poteti/application_controller.rb | 10 ---------- app/controllers/poteti/tips_controller.rb | 8 +++++--- app/controllers/poteti/users_controller.rb | 2 ++ app/models/poteti/user.rb | 4 ++++ app/views/poteti/users/show.slim | 4 ++-- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/app/controllers/poteti/application_controller.rb b/app/controllers/poteti/application_controller.rb index 476a006..b672e75 100644 --- a/app/controllers/poteti/application_controller.rb +++ b/app/controllers/poteti/application_controller.rb @@ -2,15 +2,5 @@ module Poteti class ApplicationController < ActionController::Base # refs: http://stackoverflow.com/questions/5729476/render-without-layout-when-format-is-js-needs-drying layout -> (c) { c.request.xhr? ? false : 'poteti/application' } - - def default_url_options - { user_name: (@user || current_user).try(:name) } - end - - alias_method :tip_path, :user_tip_path - alias_method :tips_path, :user_tips_path - alias_method :tip_url, :user_tip_url - alias_method :tips_url, :user_tips_url - helper_method :tip_path, :tips_path, :tip_url, :tips_url end end diff --git a/app/controllers/poteti/tips_controller.rb b/app/controllers/poteti/tips_controller.rb index 49acced..4ee5d9a 100644 --- a/app/controllers/poteti/tips_controller.rb +++ b/app/controllers/poteti/tips_controller.rb @@ -1,3 +1,5 @@ +require_dependency 'poteti/application_controller' + module Poteti class TipsController < ApplicationController def show @@ -12,10 +14,10 @@ def create respond_to do |format| if @tip.save - format.html { redirect_to @tip, notice: 'Tip was successfully created.' } - format.js { render status: :created, location: @tip } + format.html { redirect_to [current_user, @tip], notice: 'Tip was successfully created.' } + format.js { render status: :created, location: [current_user, @tip] } else - format.html { redirect_to tips_path } + format.html { redirect_to user_tips_path } format.js { render json: @tip.errors, status: :unprocessable_entity } end end diff --git a/app/controllers/poteti/users_controller.rb b/app/controllers/poteti/users_controller.rb index 681b8fe..27329ca 100644 --- a/app/controllers/poteti/users_controller.rb +++ b/app/controllers/poteti/users_controller.rb @@ -1,3 +1,5 @@ +require_dependency 'poteti/application_controller' + module Poteti class UsersController < ApplicationController def show diff --git a/app/models/poteti/user.rb b/app/models/poteti/user.rb index 8181c12..3026de2 100644 --- a/app/models/poteti/user.rb +++ b/app/models/poteti/user.rb @@ -13,5 +13,9 @@ class User < ActiveRecord::Base devise(*DEVISE_OPTIONS) has_many :tips + + def to_param + name + end end end diff --git a/app/views/poteti/users/show.slim b/app/views/poteti/users/show.slim index 9e478a6..255a2f9 100644 --- a/app/views/poteti/users/show.slim +++ b/app/views/poteti/users/show.slim @@ -1,13 +1,13 @@ section.tips .tip.new - tip = @user.tips.new - = form_for tip, remote: true do |f| + = form_for [@user, tip], remote: true do |f| p = f.text_field :title, placeholder: tip.class.human_attribute_name(:title) p = f.text_area :body, placeholder: tip.class.human_attribute_name(:body) = f.submit - @tips.each do |tip| - = link_to tip do + = link_to [@user, tip] do = render 'poteti/tips/show_with_folding', tip: tip aside