I'm calling a custom action with simple_form. I'm having trouble passing the :id parameter to the action.
routes
post '/posts/:id/admin_vote' => 'posts#admin_vote', as: 'admin_vote'
form
<%= simple_form_for :post, url: admin_vote_path(:post_id), :html => {:class => 'form-inline admin-vote-form'} do |f| %>
<%= f.select :vote, 1..20 %>
<%= f.submit 'Vote', :class => 'btn btn-primary btn-xs' %>
<% end %>
partial render
<%= render 'layouts/admin_vote', :locals => { :post => post, :post_id => post.id } %>
For some reason the action receives params[:id] = 'post_id' instead of the actual id.
You're providing :post_id symbol to the admin_vote_path, so it uses that. Change it to:
admin_vote_path(params[:post_id])
or a different parameter depending on the context of your form.
it seems to me your form should be
<%= simple_form_for post, url: admin_vote_path, :html => {:class => 'form-inline admin-vote-form'} do |f| %>
<%= f.select :vote, 1..20 %>
<%= f.submit 'Vote', :class => 'btn btn-primary btn-xs' %>
<% end %>
you should build form based on variable post
Related
I have two models: Inbox and Equipments. They have has_many relation :through => :inbox_equipments
In Inbox view I have the next code
<%= form_for #inbox, :html => { :multipart => true } do |f| %>
<%= f.text_area :number, placeholder: "Enter inbox number" %>
<% Equipment.all.each do |equipment| %>
<%= check_box_tag :equipment_ids, equipment.id, #inbox.equipments.include?(equipment), :name => 'inbox[equipment_ids][]' %>
<%= equipment.name %>
<% end %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>
I need text area for find (or filter) desired value of equipment in check_box_tag
Like this:
<%= form_tag equipment_index_path, :method => 'get', :id => "equipments_search" do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Find", :name => nil, class: "btn btn-small btn-primary" %>
Help, please.
Although your description was not the best, I think you're looking for ajax
I would do this:
Call From Ajax
Ajax will allow you to pull the other form from your Rails app as a partial, appending it as required. This will give you the ability to treat the form as an asynchronous request, removing any issue from the form itself
Here's what I'd do:
#app/assets/javascripts/application.js
$("input[type=checkbox]").on("change", function() {
$.ajax({
url: "controller/search",
data: $("input[name=inbox[equipment_ids]").serialize(),
error: function(data) {
alert(data)
}
});
});
#config/routes.rb
match 'search(/:search)', :to => 'controller#search', :as => :search, via: [:get, :post]
#app/controllers/your_controller.rb
def search
#results = Model.where(:id => params[:equipment_ids])
respond_to do |format|
format.js
end
end
#app/views/your_controller/search.js.erb
$("form").append(data)
This won't work out of the box, but it will give you some ideas as to what you can do
<%= form_tag 'select_domain', :url => administer_admin_domain_path(:id), :method => :get do %>
<%= select_tag "id", options_from_collection_for_select(#domains, :id, :caption), :onchange => "this.form.submit();" %>
<% end %>
I want the option selected's id to be the :id inside my form's url when it submits, is this possible?
How about:
<%= form_tag 'select_domain', :url => administer_admin_domains_path, :method => :get do %>
<%= select_tag "id", options_from_collection_for_select(#domains, :id, :caption), :onchange=>"this.form.action=this.form.action + '/' + this.value; this.form.submit(); %>
<% end %>
I didn't test the code. Just giving u some idea which might help. : )
Why do I get error like this?
undefined method `community_topics_path' for #<#<Class:0x00000009d79098>:0x00000009d70a38>
Extracted source (around line #1):
1: <%= form_for #community_topic, :html => { :class => 'form-horizontal' } do |f| %>
2: <div class="control-group">
3: <%= f.label :community_id, :class => 'control-label' %>
4: <div class="controls">
rake routes shows I use 'to_param' for community's id but I haven't defined :community_id at all in my routes.rb. I wonder why rake routes shows this :community_id. Is that probably because I use 'to_param' for my community model?? That's why it automatically detect that and replace :id to :community_id?
new_community_topic GET /communities/:community_id/topic/new(.:format) community_topics#new
routes.rb
resources :communities do
resources :community_topics, :path => "topic", :as => :'topic'
end
views/communities/_form.html.erb
<%= form_for #community_topic, :html => { :class => 'form-horizontal' } do |f| %>
.......
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
community_topic_path, :class => 'btn' %>
</div>
<% end %>
Updated!!!
**rake routes | grep community_topic
community_topic_index GET /communities/:community_id/topic(.:format) community_topics#index
POST /communities/:community_id/topic(.:format) community_topics#create
new_community_topic GET /communities/:community_id/topic/new(.:format) community_topics#new
edit_community_topic GET /communities/:community_id/topic/:id/edit(.:format) community_topics#edit
community_topic GET /communities/:community_id/topic/:id(.:format) community_topics#show
PUT /communities/:community_id/topic/:id(.:format) community_topics#update
DELETE /communities/:community_id/topic/:id(.:format) community_topics#destroy
As you are using nested routing then you have to pass and community into form_for:
<%= form_for [#community, #community_topic], :html => { :class => 'form-horizontal' } do |f| %>
upd: Or #community_topic.community in case you ain't set #community.
<%= form_for [#community_topic.community, #community_topic], :html => { :class => 'form-horizontal' } do |f| %>
You can watch this episode of RailsCasts to fully understand nested resources. Episode use Rails 2 for examples but you should understand the conception.
Controller: #micropost = Micropost.new(params[:micropost])
But this form_tag is sending me params[:content] instead of params[:micropost][:content]
<%= form_tag( {:controller => :microposts, :action => :create}, :remote => true) do %>
<%= text_area_tag :content, "", :size=> "20x2" %>
...
...
...
<%= submit_tag "submit" %>
<% end %>
server:
Processing by MicropostsController#create as JS
Parameters: {"utf8"=>"✓", "content"=>"sdfsdf", "commit"=>"submit"}
You have to do either of the following
<%= text_area_tag "micropost[content]", "", :size=> "20x2" %>
OR
<%= form_for :micropost, :url=>{ :controller => :microposts, :action => :create}, :remote => true do |f| %>
<%= f.text_area :content, "", :size=> "20x2" %>
<% end %>
You have to avoid mixing form_for and input_tag.
When you declare a form_for #an_object do |form|, the best practice is to use form.text_area :content when :content is an attribute of your #an_object.
In this case, you can also write: text_area_tag "an_object[content]", but it's a little more dirty.
I have a'Remove' button in a show erb of the trackers_controller.show:
<%= form_for :user_tracker, :url => user_tracker_path,:method => :delete do |f| %>
<%= f.hidden_field :tracker_id, :value => #tracker.id %>
<%= f.submit :save, :value => 'Remove' %>
<% end %>
This works fine and calls user_trackers_controller.destroy
The User models looks like:
has_many :user_trackers
has_many :trackers, :through => :user_trackers
If I put the exact same button in another erb I get this error:
No route matches {:action=>"show", :controller=>"user_trackers"}
I have a few different combinations like :html => {:method => :delete }
If I try it this way
<% current_user.user_trackers.each do |user_tracker| %>
<%= user_tracker.tracker %>
<%= form_for user_tracker, :method => :delete do |f| %>
<%= f.submit :delete, :value => 'Remove' %>
<% end %>
I get the same routing error
Here are the routes:
user_trackers GET /user_trackers(.:format) user_trackers#index
POST /user_trackers(.:format) user_trackers#create
new_user_tracker GET /user_trackers/new(.:format) user_trackers#new
edit_user_tracker GET /user_trackers/:id/edit(.:format) user_trackers#edit
user_tracker GET /user_trackers/:id(.:format) user_trackers#show
PUT /user_trackers/:id(.:format) user_trackers#update
DELETE /user_trackers/:id(.:format) user_trackers#destroy
I do not understand, why will it not pick up that this is a destroy when in an unrelated erb?
Edit:
This is one seems to work but the html generates ids and classes like edit_user_tracker_7 but also the javascript to make it a delete so it seems I still have something wrong:
<%= form_for user_tracker, :url => user_tracker_path(user_tracker), :method => :delete do |f| %>
<%= f.hidden_field :tracker_id, :value => user_tracker.tracker_id %>
<%= f.submit :delete, :value => 'Remove from my portfolio' %>
<% end %>
in first line it should be
:url => user_tracker_path(#user)
Its not a bug, he just have to know who to remove :) so he needs id (in REST).
Also :method should be in :html
:html => { :method => :delete }
or
html: { method: "delete" }
with 1.9+ notation.
full form_for
user_tracker_path(#user), :html => {:method => :delete} do |f| %>
ofc if in your case #user is current_user then you have to swap it :)
Sorry for typos & english i'm not native :)
Cheers!