Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to create or edit a market #8

Merged
merged 1 commit into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/controllers/markets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ def index
end

def new
@mymarket=Market.new
@market_method = :post
@market_path = markets_create_path
end

def create
@mymarket = Market.new
@mymarket.name = params[:market][:name]
@mymarket.address = params[:market][:address]
@mymarket.city = params[:market][:city]
@mymarket.county=params[:market][:county]
@mymarket.state=params[:market][:state]
@mymarket.zip=params[:market][:zip]
@mymarket.save
redirect_to markets_index_path
end

def show
#for some reason you don't need to change params[:id] to an integer
@mymarket = Market.find(params[:id])
@myvendors=@mymarket.vendors
if @mymarket == nil
Expand All @@ -18,8 +31,35 @@ def show
end

def edit
@mymarket = Market.find(params[:id])
@market_method = :put
@market_path = markets_update_path(@mymarket.id)
if @mymarket == nil
render :file => 'public/404.html',
:status => :not_found
end

end

def update
@mymarket = Market.find(params[:id])

if @mymarket == nil
render :file => 'public/404.html',
:status => :not_found
end

@mymarket.name = params[:market][:name]
@mymarket.address = params[:market][:address]
@mymarket.city = params[:market][:city]
@mymarket.county=params[:market][:county]
@mymarket.state=params[:market][:state]
@mymarket.zip=params[:market][:zip]
@mymarket.save
redirect_to markets_show_path(@mymarket.id)
end


def destroy
end
end
23 changes: 23 additions & 0 deletions app/views/markets/_marketsform.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%= form_for @mymarket,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! I think I'll be able to use this logic for edit page.

method: @market_method,
url: @market_path do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :address %>
<%= f.text_field :address %>

<%= f.label :city %>
<%= f.text_field :city %>

<%= f.label :county %>
<%= f.text_field :county %>

<%= f.label :state %>
<%= f.text_field :state %>

<%= f.label :zip %>
<%= f.text_field :zip %>

<%= f.submit %>
<% end %>
1 change: 1 addition & 0 deletions app/views/markets/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<h1>Markets#edit</h1>
<p>Find me in app/views/markets/edit.html.erb</p>
<%= render partial: "marketsform" %>
4 changes: 4 additions & 0 deletions app/views/markets/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
<%= "#{market[:city]}, #{market[:state]}" %></p>
<% end %>
</section>

<section class='button'>
<%= button_to("add market", {action: "new"}, method: :get) %>
</section>
1 change: 1 addition & 0 deletions app/views/markets/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<h1>Markets#new</h1>
<p>Find me in app/views/markets/new.html.erb</p>
<%= render partial: "marketsform" %>
4 changes: 4 additions & 0 deletions app/views/markets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<p><%= vendor.name %></p>
<%end%>
</section>
<section class='button'>

<%= button_to("edit market", markets_edit_path(@mymarket.id), method: :get) %>
</section>

2 changes: 2 additions & 0 deletions app/views/markets/update.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Markets#update</h1>
<p>Find me in app/views/markets/update.html.erb</p>
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@




put 'markets/:id/update' => 'markets#update', as:'markets_update'

get 'markets/index' => 'markets#index', as: 'markets_index'

get 'markets/new'

get 'markets/create'
post 'markets/create' => 'markets#create', as: 'markets_create'

get 'markets/show/:id' => 'markets#show', as: 'markets_show'

get 'markets/edit'
get 'markets/:id/edit' => 'markets#edit', as: 'markets_edit'

get 'vendors/edit' => 'vendors#edit', as: 'vendors_edit'

Expand Down