Dan Morgan Dan Morgan - 2 months ago 12
Ruby Question

Rails - two forms in one (ShopifyAPI)

Part of the app i'm developing involves a form which has to create a product on the users shopify store and also create a row in my own database for an identical product (with a few extra bits of information).

Where i'm struggling is with the form itself, i can do this with a html based form, but i can't get a single ruby form to do both jobs. A shortened version of my controller create code is as follows;

def create
@item = Item.new
@item.item_title = params[:item_title]
@item.item_profit = params[:item_profit]
@new_product = ShopifyAPI::Product.new
@new_product.title = params[:item_title]
@new_product.save
@item.save
end


So as you can see i'm using the same params to set values for both the shopify product and the product in my own db. The HTML form looks like this:

<form action="/items/submit" >
<input type="text" name="item_title">
<br>
<input type="text" name="item_profit">
<br>
<br>
<input type="submit"/>
</form>


And it works fine, but how do i convert this into a ruby form that does the same job?

Answer

You can do like this:

form_tag('/items/submit')
  text_field_tag 'item_title'
  tag("br")
  text_field_tag 'item_profit'
  tag("br")
  tag("br")
<%= submit_tag 'Save' %>

Instead of tag("br"), you can directly use < br />.

Comments