Reputation: 289
Market is a model with a markets table with 3 columns: Name, created_at, and updated_at. I am trying to build a simple admin market index view where I can use a text field to create a market. I thought I had set up the market_params appropriately, but apparently I'm doing something wrong, because I get a Forbidden AttribuesError in MarketsController#create, and it highlights this line:
@market = Market.new(params[:market])
Any help on what I'm doing wrong would be greatly appreciated!
here is my markets controller:
class MarketsController < ApplicationController
def index
@markets = Market.all
@market = Market.new
end
def create
@market = Market.new(params[:market])
if @market.save
flash[:success] = "Market Created!"
render 'markets/index'
else
render 'markets/index'
end
end
private
def market_params
params.require(:market).permit(:name)
end
end
Here is my form:
<%= form_for(@market) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :name, placeholder: "Enter Market name here" %>
</div>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
Upvotes: 1
Views: 740
Reputation: 6942
Right now you're trying to create a market from params[:market]
. Assuming you're using strong_parameters
, you need to do this instead:
@market = Market.new(market_params)
Upvotes: 2
Reputation: 2675
Why? ForbittionAttributError
Rails wont allow direct database create or update operations without whitelisting params. You have written method called market_params which does that but you are not using it.You are directly using params[:market] in new method.
Change this line
@market = Market.new(params[:market])
To
@market = Market.new(market_params)
Upvotes: 2