Reputation: 137
I'm trying to pass URL params into my Rails app (Ex: /entries/new?name=asdf
Controller:
def new
@entry = Entry.new(params[:name])
end
def create
@entry = Entry.new(params[:name])
if @entry.save
redirect_to @entry, notice: 'Entry was successfully created.'
else
render :new
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_entry
@entry = Entry.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def entry_params
params.require(:entry).permit(:name)
end end
Form field :
<%= hidden_field_tag :name, params[:name] %>
I'm getting the error "When assigning attributes, you must pass a hash as an argument."
Any ideas?
Upvotes: 0
Views: 79
Reputation: 11580
params
is a Hash. params[:name]
will return a string ('asdf' in your example above). When instantiating Entry
you're only passing the string when you should be passing a hash. Assuming that name
is an attribute on your Entry model, you'll need to do this
@entry = Entry.new(name: params[:name])
Upvotes: 1
Reputation: 1137
Use rails generate scaffold Entry
and take a look to the generated code. What you're trying to do don't have much sense
Upvotes: 0