Last active
August 29, 2015 14:14
-
-
Save jacoyutorius/f2858714a54548ef3bba to your computer and use it in GitHub Desktop.
Railsで更新も編集も削除も、全部indexページでやりたい ref: http://qiita.com/jacoyutorius/items/ea0673fe30cfe7cdac8f
This file contains 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
<td><%= link_to 'Edit', edit_user_path(user) %></td> | |
↓ | |
<td><%= link_to 'Edit', users_path(id: user.id) %></td> |
This file contains 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
$ rails g scaffold User name:string age:decimal note:text |
This file contains 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
def update | |
respond_to do |format| | |
if @user.update(user_params) | |
format.html { redirect_to request.referer, notice: 'User was successfully updated.' } | |
format.json { render :show, status: :ok, location: @user } | |
else | |
format.html { render :edit } | |
format.json { render json: @user.errors, status: :unprocessable_entity } | |
end | |
end | |
end |
This file contains 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
Rails.application.routes.draw do | |
root "users#index" | |
resources :users | |
end |
This file contains 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
def index | |
@users = User.all | |
@user = User.new #追記 | |
end | |
This file contains 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
redirect_to users_path |
This file contains 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
def create | |
@user = User.new(user_params) | |
respond_to do |format| | |
if @user.save | |
format.html { redirect_to users_path, notice: 'User was successfully created.' } | |
format.json { render :show, status: :created, location: @user } | |
else | |
format.html { render :new } | |
format.json { render json: @user.errors, status: :unprocessable_entity } | |
end | |
end | |
end |
This file contains 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
def index | |
@users = User.all | |
if params[:id].present? | |
set_user | |
else | |
@user = User.new | |
end | |
end |
This file contains 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
# -------------ここから---------------- | |
<h1>Listing users</h1> | |
<%= form_for(@user) do |f| %> | |
<% if @user.errors.any? %> | |
<div id="error_explanation"> | |
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2> | |
<ul> | |
<% @user.errors.full_messages.each do |message| %> | |
<li><%= message %></li> | |
<% end %> | |
</ul> | |
</div> | |
<% end %> | |
<div class="field"> | |
<%= f.label :name %><br> | |
<%= f.text_field :name %> | |
</div> | |
<div class="field"> | |
<%= f.label :age %><br> | |
<%= f.text_field :age %> | |
</div> | |
<div class="field"> | |
<%= f.label :note %><br> | |
<%= f.text_area :note %> | |
</div> | |
<div class="actions"> | |
<%= f.submit %> | |
</div> | |
<% end %> | |
# -------------ここまで---------------- | |
<table> | |
<thead> | |
<tr> | |
<th>Name</th> | |
<th>Age</th> | |
<th>Note</th> | |
<th colspan="3"></th> | |
</tr> | |
</thead> | |
<tbody> | |
<% @users.each do |user| %> | |
<tr> | |
<td><%= user.name %></td> | |
<td><%= user.age %></td> | |
<td><%= user.note %></td> | |
<td><%= link_to 'Show', user %></td> | |
<td><%= link_to 'Edit', edit_user_path(user) %></td> | |
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> | |
<br> | |
<%= link_to 'New User', new_user_path %> | |
This file contains 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
resources :users, expect: [:show, :new, :edit] |
This file contains 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
resources :users, expect: [:show, :new, :edit] |
This file contains 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
[ | |
{name: "Spike Spiegel", age: 27, birth: "2044-06-26", note: "SwordFish2"}, | |
{name: "Jet Black", age: 36, birth: "2035-12-03", note: "Hammerhead"}, | |
{name: "Faye Valentine", age: 77, note: "1994-08-14", note: "redtail"}, | |
{name: "Edward Wong Hau Pepelu Tivrusly 4", age: 13, birth: "2058-01-01", note: ""}, | |
{name: "Ein", age: 2} | |
].map{|row| Hashie::Mash.new(row) }.each do |data| | |
User.create!( | |
name: data.name, | |
age: data.age, | |
birth: data.birth, | |
note: data.note | |
) | |
end |
This file contains 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
def update | |
respond_to do |format| | |
if @user.update(user_params) | |
format.html { redirect_to request.referer, notice: 'User was successfully updated.' } | |
format.json { render :show, status: :ok, location: @user } | |
else | |
format.html { render :edit } | |
format.json { render json: @user.errors, status: :unprocessable_entity } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment