I'm refactoring a form into an AJAX post request.
Here's the existing form:
<%= form_for([#group, #invitation], :remote => true, :html => { :'data-type' => 'html', :class => 'fbinvite_form', :id => friend[:identifier]}) do |f| %>
<%= f.hidden_field :recipient_email, :value => "facebook#meetcody.com" %>
<div class = "fbinvite btn_list_right" id = "<%= friend[:identifier] %>">
<%= f.submit "Invite", :class => "btn btn-medium btn-primary", :name => "fb" %>
</div>
<% end %>
In the controller I have logic that checks for the name of the submit input:
if params[:fb]
...
elsif params[:somethingelse]
...
end
That's the background. I am now trying to replace the above form with the following ajax (added dummy data to make it clearer):
$.post('/groups/7/invitations', {
"recipient_email": "facebook#meetcody.com",
"commit" : "fb",
"fb" : "fb"
}, function(response) {
alert("invite created!");
});
This successfully hits the controller, but doesn't evaluate as true on the condition if params[:fb]. As you can see I tried some really basic ways of passing that parameter in, but it obviously didn't work.
How can I change my ajax request so that params[:fb] evaluates as true?
Related
I have seen this code in this repository:
<%= link_to(:controller => 'account', :action => 'select', :account_id => account.customer_id) { account.customer_id.to_s } %>
(<%= account.login %> / <%= account.company_name %> )
This actually converts to the following HTML:
<a account_id="8282277272" action="select" controller="account">8282277272</a>
( loginname / companyname )
I am wondering how would you pass a block to link_to in order to make this work?
I think this is what you're looking for. The stuff inside the "do..end" will be put inside the a tag.
<%= link_to(:controller => 'account', :action => 'select', :account_id => account.customer_id) do %>
(<%= account.login %> / <%= account.company_name %> )
<% end %>
It should produce
<a href="<path to controller with account_id parameter>">
(username / Company, Inc.)
</a>
What was happening in your original code was that the expression { account.customer_id.to_s } was being passed as the block to link_to. If you want the customer id to be displayed along with the "login" and "company_name", put it inside the block.
<%= link_to(account_select_path(:account_id => account.customer_id.to_s)) do%>
<%= account.customer_id.to_s %>
<%end%>
(<%= account.login %> / <%= account.company_name %> )
and in your config/routes.rb, in the beginning of the file, add
match 'account/select' => "account#select", :as => :account_select
Now I have 2 forms that submit a comment.
Form Type A
<%=form_for(([#community, #comment]), :remote => true, :class => 'form' ) do |f| %>
<%= f.text_field :body, :id => "body", :class => "chat" %>
<button type="submit" class="btn">submit</button>
<% end %>
Form Type B
<%=form_for(([#user, #comment]), :remote => true, :class => 'form' ) do |f| %>
<%= f.text_field :body, :id => "body", :class => "chat" %>
<button type="submit" class="btn">submit</button>
<% end %>
Now, I want to have link_to button that functions as same as those forms do if a user clicks it.
When the user click on the link, #comment will be automatically filled just like below.
From Form Type A
#comment = "this is for community"
From Form Type B
#comment = "this is for user"
How can I do that? As far as I understand my situation.
Form is put type, then link_to is get type so it's impossible to re-use transaction of form.
Not sure what you mean by "transaction of form" but if you're asking if you can create/modify data via a single button or link than the answer is Yes, it is possible.
You can actually put with a link_to in rails ({:method => :put} (http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
If you want a button to do this you should checkout button_to (http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to)
It's better to use button_to.
User has_one UserProfile.
Then UserProfile has wanted_message column as string.
Here, I'm showing input box to update wanted_message
I want it to be updated if certain user inputs any message then press "Update" button.
How can I? now, It's taking me to the url "/users/12" if I press "Update"
I don't want that:( I want it to be updated without any page loading(Ajax Request).
Then I want to have Action called update_message in User contoroller
How can I?
<%= form_for(current_user, :url => {:controller => "user", :action => "update_message" }, :remote => true, :class => 'form-search') do |f| %>
<%= f.fields_for :user_profile do |profile_form| %>
<div class="input-append">
<%= profile_form.text_field :wanted_message, :class => 'span6' %>
<button type="submit" class="btn">Update</button>
</div>
<% end %>
<% end %>
You have to set remote => true in your form and also set :method => put Put lets you update columns in your database and remote true will configure the form to send an ajax request. You'll also have to configure the update_message action in your controller to handle ajax requests. Finally, make sure your routes are configured correctly. You'll have to define that route in routes.rb and probably do an :as => 'update_message' to have access to that route helper method
This may help you with ajax in rails forms http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html
Here's a sample form, it's in haml though:
= link_to "Start", polls_toggle_live_path(poll.id), :method => :put, :remote => true, :class=> 'btn btn-success btn-small start-poll', :id => poll.id
Which links to this controller action
def toggle_live
#poll = Poll.find(params[:poll_id])
#poll.toggle_live
#poll.save!
end
Calls this method in the associated polls model to switch the db column value
def toggle_live
self.is_live = !self.is_live
end
Finally its configured in routes.rb this way to pass along the correct updates
match '/polls/toggle_live/:poll_id' => 'polls#toggle_live', :via => :put, :as => 'polls_toggle_live'
OK, I'm totally new to Ruby on Rails. I created a form that posts to an external widget that returns JSON. So I have this form:
<%= form_for :email, :url => 'http://XXX.XX.XXX.212/widgetapi.0.1.php', :html => {:class => "new_email"} do |f| %>
<%= f.text_field :email, :value => "Your email address...", :class => "text", :id => "email", :name => 'email',
:onFocus => "change(this,'#222222'); this.value=''; this.onfocus=null;",
:size => "26" %>
<%= f.hidden_field :apiKey, :id => "apiKey", :name => 'apiKey', :value => "ABC123" %>
<%= f.hidden_field :lrDomain, :id => "lrDomain", :name => 'lrDomain', :value => "signup.triplingo.com" %>
<%= f.hidden_field :urlPrefix, :id => "refCodeUrl", :name => 'refCodeUrl', :value => "http://signup.website.com/" %>
<%= f.hidden_field :ref_code, :id => 'ref_code', :name => 'ref_code', :value => #referralid %>
<%= submit_tag "Enter To Win", :class => "button-positive submit" %>
<% end %>
Which works. Now I get back a JSON response that is:
({"email":"testing#testing2.com","reflink":"fi1ts","newuser":true})
Ok now the result it the browser sits on the response page with the JSON.
I'm guessing I need to do something with the #response in the controller, but I'm not sure what. All I want to do is if that "newuser" is true, provide them a success page. If false, go to an error page.
Thanks.
You should change that form, so that it will send request to your controller. In that controller you should do API Call with parameters from a form(using for example Curb: https://github.com/taf2/curb or Net/http: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html). Then You can parse result in JSON and show user correct page.
So:
1) User send request to YOUR application
2) YOUR application make request to http://XXX.XX.XXX.212/widgetapi.0.1.php using data from user
3) YOUR application receives JSON, and check if newuser is true.
4) If it is true action render success page, otherwise it render error page.
I know I have done this before, but for the life of me I can't figure it out.
I have a table with a "called" field in it. I need to use a checkbox to update the db and check if "called" is "true" or not. Doesn't need to be AJAX, just needs to update the field.
table: rsvp
field: called
Thanks a ton.
A simple approach without ajax could be using a checkbox inside a form and submitting the form with the checkbox javascript onclick event.
Example:
View:
<% form_for #rsvp, :id => "rsvp" do |f| %>
<%= f.check_box :called, :onclick => "$('#rsvp').submit()" %>
<% end %>
this if you are using JQuery... with prototype the onclick string will be:
$('rsvp').submit()
Controller:
#rsvp = Rsvp.find(params[:id])
if #rsvp.update_attributes(params[:rsvp])
# success
else
# fail
end
Reference:
check box
In view:
<% form_for :rsvp, :url => {:controller => "rsvp", :action => "update"} do |f| %>
<%= f.check_box :called %>
<%= f.submit "Update" %>
<% end %>
In rsvp controller, update method:
#RSVPobject.updateAttribute(:called, params[:rsvp][:called])
If you just want to do this just by clicking the checkbox, you need to go the Ajax road.
Try using an "observe_field" in your view.
<%= observe_field ":called",
:frequency => 0.25,
:update => 'feedback_to_user',
:url => {:action => :mark_as_called},
:with => 'called',
:on => 'click' %>
All the details here.
Don't forget to adjust your routes so that the "mark_as_called" action can be found.