ruby - Rails redirect to .json file instead the created item -
in rails app need add together new item catalog on json, after post it's redirect me .json file instead created item's page. create function is:
def create @item = item.new(items_params) respond_to |format| if @item.save format.html{redirect_to @item, success: 'item created'} format.js {} format.json{render :json => @item} else format.html { render action: 'new' } format.js {} format.json { render json: @item.errors.full_messages, status: :unprocessable_entity } end end end my form new item:
<%= form_for @item, remote: true, format: :json, :html => {:multipart => true} |f|%> <%= f.label :name, 'item name:' %> <%= f.text_field :name, :class=>'input-block-level' %> <%= f.label :type_id, 'cathegory:' %> <%= collection_select(:item, :type_id, type.all, :id, :name, {}, {:class=>'input-block-level'}) %> <%= f.label :description, 'description' %> <%= f.text_area :description, :class=>'input-block-level' %> <%= f.label :price, 'price' %> <%= f.text_field :price %> <p> <label>photo</label> <%= f.file_field :image, :class=>'input-block-level' %> </p> <%= f.submit 'add item', class: 'btn btn-success btn-block btn-large' %> <% end %> json file in browser after post:
{"id":80,"type_id":7,"image":{"url":"/uploads/item/image/80/3572746998.png"},"description":"sample description","created_at":"2014-06-20t10:02:01.574z","updated_at":"2014-06-20t10:02:01.574z","name":"test","price":100} what can prepare issue? thanks.
remove format: :json form_for helper method.
<%= form_for @item, :html => {:multipart => true} |f|%> .... <% end %> ruby-on-rails ruby json ruby-on-rails-4
No comments:
Post a Comment