Dan Morgan Dan Morgan - 4 months ago 42
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]

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">
<input type="text" name="item_profit">
<input type="submit"/>

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


You can do like this:

  text_field_tag 'item_title'
  text_field_tag 'item_profit'
<%= submit_tag 'Save' %>

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