Add extra options to link_to do - ruby

I would like to pass a parameter with this link_to helper. How do I do that?
<%= link_to ('/my_controller/my_action') do %>
<div>haha</div>
<% end %>
The original code I had used the standard text link:
<%= link_to("link text", {:controller => 'my_controller', :action => 'my_action', :id => my_id}) %>

Just like in your second example:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
My text goes here
<% end %>
If you have a path helper for this (i.e. if it's something like PostsController's show action) then you could do:
<%= link_to post_path(my_id) do %>
My text goes here
<% end %>

From my understanding you want to pass options to the link_to helper when using a block.
From the documentation at http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
This is possible with the following format
link_to(options = {}, html_options = {}) do
# name
end
To give you an example and using the code you provided:
<%= link_to({:controller => 'my_controller', :action => 'my_action', :id => my_id}) do %>
<div>haha</div>
<% end %>

Related

Padrino and sequel nested attributes form

I am trying to make a form for storing data hierarchy through the associations.
My structure of database is
meta
- ID (PK)
version
- meta_id (FK of meta)
- ID (PK)
- lang (FK from lang ...)
- ....
data
- ID (FK of version)
I need to store multiple language content under one ID.
Models are
class Meta < Sequel::Model(:link_meta)
one_to_many :version
end
class Version < Sequel::Model(:link_version)
one_to_one :meta, key: :meta_id
one_to_many :data, key: :id
one_to_one :lang, key: :lang
end
class Link < Sequel::Model(:link)
plugin :timestamps, :create => :created, :update => :updated
many_to_one :version, key: :id
end
And my form for creating input is.
<% form_for :links, url(:links, :create), :class => 'form-horizontal' do |f| %>
<%= partial 'links/form', :locals => { :f => f } %>
<% end %>
And this is my partial
<% f.fields_for :version, :class => 'form-horizontal' do |cz| %>
<%= cz.hidden_field :lang, :value => Lang.select(:id).where(:lang => "cz").map(:id) %>
<% cz.fields_for :data, :class => 'form-horizontal' do |link| %>
<% error = #links.errors.key?(:url) && #links.errors[:url].count > 0 %>
<fieldset class='control-group <%= error ? 'has-error' : ''%>'>
... form content
</fieldset>
<% end %>
<% f.fields_for :version, :class => 'form-horizontal' do |en| %>
<%= en.hidden_field :lang, :value => Lang.select(:id).where(:lang => "cz").map(:id) %>
<% en.fields_for :data, :class => 'form-horizontal' do |link| %>
<% error = #links.errors.key?(:url) && #links.errors[:url].count > 0 %>
<fieldset class='control-group <%= error ? 'has-error' : ''%>'>
... another form content
</fieldset>
<% end %>
<% end %>
But it fails on
undefined method `data' for [:class, "form-horizontal"]:Array
I tried different association combinations etc, I feel lost.
Thanks
Levi

Selecting a specific metafield for a product using Shopify API, Ruby on Rails

In liquid templates this is achieved like so:
{{ product.metafields.book.author }}
Which returns the value of 'author' for it's key 'book'
I'm using Shopify API and Ruby on Rails and have successfully looped over each metafield for a given product:
In the controller:
#products = ShopifyAPI::Product.find(:all, :params => {:limit => 10})
In the view:
<% #products.metafields.each do |metafield| %>
<%= metafield.key %> : <%= metafield.value %>
<% end %>
This returns all of the metafields for a product, as expected. How do I return only those metafields matching a specific key i.e. 'book' from the example above?
# add metafield
product = ShopifyAPI::Product.find(product_id)
product.add_metafield(ShopifyAPI::Metafield.new({
:description => 'Author of book',
:namespace => 'book',
:key => 'author',
:value => 'Kurt Vonnegut',
:value_type => 'string'
}))
# retrieve metafield
author = ShopifyAPI::Metafield.find(:first,:params=>{:resource => "products", :resource_id => product.id, :namespace => "book", :key => "author"}).value
More info: http://www.shopify.com/technology/3032322-new-feature-metafields
This seems to do the trick:
<% product.metafields.each do |metafield| %>
<% if metafield.key == "book" %>
<%= metafield.key %>: <%= metafield.value %><br/>
<% end %>
<% end %>
or
<% product.metafields.each do |metafield| %>
<% if metafield.key.include?("book") %>
<%= metafield.key %>: <%= metafield.value %><br/>
<% else %>
<% end %>
This works:
ShopifyAPI::Metafield.find(:first,:params=>{:resource => "products", :resource_id => 94549954, :key => "book"})
ShopifyAPI::Metafield.find(:all,:params=>{:product_id => product.id, :key=> 'book'})
You can get all metafields for a product
#product = ShopifyAPI::Product.find(params[:id])
After that, you can get its metafields
#metafields = #product.metafields
Or if you need just a specific metafield for that product
#metafield = ShopifyAPI::Product::Metafield.find(:all, where: {"product_id = ? AND metafield.key = ?", product.id, "book"})

how to request a fields_for partial via ajax?

i have a nested form model
<%= simple_form_for(#product, :html => { :multipart => true }) do |f| %>
<%= f.input :name %>
#... and some other
<%= f.simple_fields_for :productgroups do |builder| %>
<%= render 'form_productgroup', { :f => builder } %>
<% end %>
<% end %>
## partial-level-1: form_productgroup
<%= f.input :extrapercentage %>
#... and some other
<%= f.simple_fields_for :productmaterials do |builder| %>
<%= render 'form_productmaterial', { :f => builder } %>
<% end %>
## partial-level-2: form_productmaterial
<%= f.input :price %>
#... and some other
=> everything fine serverside, but i need to add records via ajax because each productgroup or productmaterial belongs to a specific type
now the question:
how to create a controller action which returns the new material form?
This code returns an error that f is not existing:
def new_productgroup
#product = Product.find(params[:id])
#productgroup = Productgroup.new(:product_id => #product.id, :materialgroup_id => params[:materialgroup_id])
render "form_productgroup", { :f => ???? }
end
please help me!

how does a button_to in rails request a JSON response?

Im a rails noob, so bear with me.
I have the following in new.html.erb:
<h1>New page</h1>
<%= render 'pages/form' %>
<% if #page != 1 %>
<%= button_to 'New Page', pages_path %>
<%= button_to 'Done!', :action=> 'generate' %>
<% end %>
my controller looks like this
def generate
#presentation = current_presentation
respond_to do |format|
format.json { render json: #presentation.pages}
format.html {render :text => "html"}
end
end
I want the response to be JSON, but the code goes to format.html.
How do I instruct the button_to that my requested response is JSON?
Not sure if just one of them or both, but try :remote => true and data-type:
<%= button_to "Create", :action => "create", :remote => true, :form => { "data-type" => "json" } %>
Using a path helper to build the path worked for me. eg.
<%= button_to "Create Widgit", widgits_path(format: 'json'), remote: true %>

Client-side validation of form in rails 3?

I installed the gem client_side_validations, did everything exactly according to the manual, for greater certainty, I checked it on a test project, it worked, but when i migrate it to my project it gave this error:
undefined method `validate_options' for #<User:0xb67365fc>
this is the form code:
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validations => true, :html => {:id => 'commentform'}) do |f| %>
<%= f.input :name, :label => t('general.name') %>
<%= f.input :last_name, :label => t('general.last_name') %>
<%= f.input :email, :label => t('general.email') %>
<%= f.input :phone, :label => t('general.phone') %>
<% if #user.password_required? %>
<%= f.input :password, :label => t('general.password') %>
<%= f.input :password_confirmation, :label => t('general.password_confirm') %>
<%= f.hidden_field :is_anonym, :value => false %>
<% end %>
<br/>
<%= f.button :submit, :label => t('general.button') %>
<% end %>
anyone help me..
For what its worth I've rewritten the ClientSideValidation gem completely. The previous version I wrote at the DNC had some pretty big issues.
Feel free to check out the 3.0 rewrite: https://github.com/bcardarella/client_side_validations

Resources