How pass variable submitted in form into 'new' action - ruby-on-rails-3

I am trying to send a message to another user. But when I leave a field blank, the controller renders another 'new' action.
Here is the view code that links to the first submit:
<%= link_to 'Send Mail', {:controller => 'messages', :action => 'new', :recipient_id => #profile.id}, :class => 'btn btn-large btn-primary' %>
Here is the message#new code:
def new
#message = Message.new
#recipient = Profile.find(params[:recipient_id])
end
Here is the message#new view code:
<%= simple_form_for #message do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.input :recipient_id, :as => :hidden, :input_html => {:value => #recipient.id} %>
<%= f.input :sender_id, :as => :hidden, :input_html => {:value => #recipient.id} %>
<%= f.button :submit %>
<% end %>
Here is the message#create code:
def create
#message = Message.new(params[:message])
if #message.save
flash[:notice] = "New message created"
redirect_to #message
else
flash[:error] = "Did not work"
#recipient = params[:recipient_id]
render action: "new"
end
end
How do I get the :recipient_id value submitted in the form and pass into the render new action?

When you rendering new method rails won't execute it codes instead it only renders the view(new.html.erb) of the new action and you already generating
a recipient_id which is enough for your form to pass the parameter
Try this
def create
#message = Message.new(params[:message])
if #message.save
flash[:notice] = "New message created"
redirect_to #message
else
flash[:error] = "Did not work"
#recipient = Profile.find(params[:message][:recipient_id])
render action: "new"
end
end
When rendering the new action we have to create the instances manually which is used in the view.
Note: If you want generate a new parameter use redirect_to method, where render uses the previous parameters until a new request is generated

Related

Custom Action causes "No route matches" Routing Error

I'm am trying to have a form submit to a custom action. The form won't even render, it just displays a error: 'No route matches'".
routes.rb
resources :users do
member do
post :add_foo
end
end
users_controller.rb
def add_foo
puts "!!! in add_new_foo .. params = " + params + " !!!"
end
users/new.html.erb
<%= simple_form_for(#user) do |f| %>
<%= button_to 'download', add_foo_user_path(#user) %>
<% end %>
I have also tried
<%= simple_form_for #user, :url => add_foo_user_path(#user), :method => :post do |f| %>
<%= f.submit "Add" %>
<% end %>
Any help would be appreciated!
Everything is defined correctly. The error is most likely raised because the #user instance is not present.

Rails 3: Using a submit button to update a model attribute

I have a user model which contains an "email switch" column with a boolean value. I'd like to create a button in my view which allows the user to turn "on" and "off" their emails. I can't get the submit button to update the value in the User model.
<%= form_for :user do |f| %>
<label>On</label>
<%= f.radio_button :email_switch, true %>
<label>Off</label>
<%= f.radio_button :email_switch, false %>
<%= f.submit "Save", :controller => "dashboard_emails", :action => "update", :method => "put" %>
<% end %>
class DashboardEmailsController < ApplicationController
before_filter :require_user
def index
end
def update
end
private
def require_user
#user = #logged_in_user
end
class User
field :email_switch, type: Boolean, default: false
end
You need to pass the arguments to form_for not to the f.submit call. If you have a persisted user assigned to #user you should be able to do:
<%= form_for #user do |f| %>
<label>On</label>
<%= f.radio_button :email_switch, true %>
<label>Off</label>
<%= f.radio_button :email_switch, false %>
<%= f.submit "Save" %>
<% end %>
Of course you need resources :users in your config/routes.rb to get this working. This should then send a PUT request to /users/47, which in turn fires the #update action of your UsersController

Ruby on Rails - Multibutton combined with checkboxes and advanced search

Currently I have the situation that one button is add to the index-form. This workes perfectly in combination with the other functionalties as search and checkboxes which are also part of the index-form.
Code of the index.html.erb:
<%= form_tag order_path, :method => 'get' do %>
<p>
<%= submit_tag "Historical Search", :account_short => nil %>
<%= text_field_tag :search, params[:search] %>
</p>
<% end %>
<%= form_tag sendnext_order_path, :method => :put do %>
<%= submit_tag "Send to Desk" %><br/>
-- other code from index-form
<% end %>
Combined with the controller:
def sendnext
Order.update_all(["status_id = ? ", "2"], :id => params[:order_ids])
redirect_to order_path, notice: 'Order succesfully send to desk.'
end
Now I want to add a second button next to the Send to Desk button with another action than the excisting working one. Until now I'm not capable to realise this.
Please advice. Any feedback is welcome.
Use button_to
<%= button_to('Send to Desk', 'sendnext', :method => "put") %>
<%= button_to('Cancel Order', 'cancelorder ', :method => "put") %>
Will take care of the form for you. submit_tag is submitting the first form I believe.
For example
<%= button_to "New", :action => "new" %>
will generate
# => "<form method="post" action="/controller/new" class="button_to">
# <div><input value="New" type="submit" /></div>
# </form>"
Thanks for your help. I have found a working solution which realised my current requirements:
The index.html.erb looks like the following:
<%= form_tag updateorder_order_path, :method => :put do %>
<%= submit_tag "To Desk" %><br/>
<%= submit_tag "Cancel Order" %>
-- other code like data fields
<%end %>
The controller.rb looks like the following:
def updateorder
if params[:commit] == "To Desk"
Order.update_all(["status_id = ? ", "2"], :id => params[:order_ids])
redirect_to order_path, notice: 'Order(s) successfully send to desk.'
elsif params[:commit] == "Cancel Order"
Order.update_all(["status_id = ? ", "3"], :id => params[:order_ids])
redirect_to order_path, notice: 'Order(s) successfully cancelled.'
else
Order.update_all(["status_id = ? ", "5"], :id => params[:order_ids])
redirect_to order_path, notice: 'Order(s) successfully updated.'
end
end
The routes.rb contains the next code:
resources :orders do
put 'updateorder', :on => :collection
end

uninitialized constant ConfirmationsController error while trying to overide devise confirmations controller

am trying to confirm a user account without using the built in devise confirmations controller but i happen to get the following error "uninitialized constant Confirmations Controller". Below is my confirmations controller class.
class ConfirmationsController < Devise::ConfirmationsController
def show
#user = User.find_by_confirmation_token(params[:confirmation_token])
if !#user.present?
render_with_scope :new
end
end
def confirm_account
#user = User.find(params[:user][:confirmation_token])
if #user.update_attributes(params[:user]) and #user.has_password?
#user = User.confirm_by_token(#user.confirmation_token)
flash[:notice] = "Hi " + #user.first_name + " your email has been verified. You can now start shopping and recommending other users to your supplier networks."
redirect_to #user
else
render :action => "show"
end
end
end
And in my routes.rb file i have the following:
devise_for :users, :controllers => { :confirmations => "confirmations" } do
match "confirm_account", :to => "confirmations#confirm_account"
end
And finally i have the following partial:
<p>Welcome <%= #user.first_name %>,</p><br/>
<%= form_for(resource, :url => confirm_account_path) do |f| %>
<%= f.label :email %>
<%= #user.email %>
<%= f.hidden_field :confirmation_token %>
<%= f.submit 'Confirm Account' %>
<p>Thank you for joining. Before you can purchase any item from your supplier or shared network, you will need to confirm your account first. Please follow the link below in order to confirm your account.</p>
<p><%= link_to 'Confirm my account', confirmation_url(#resource, :confirmation_token => #resource.confirmation_token) %></p><br/>
<p>Yours faithfully.</p>
<%end%>
Devise is can be easily modified for your needs. Here is a similar topic, which may be helpful for you:
Override devise registrations controller

Adding name value with link_to

I'm using a link_to to create an object in my Rails 3 app. Searching has given me the proper way to use link_to with the :post method, but I'm wondering if using link_to to pass in a name value for my object as well. Here is my link:
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :method => :post}, :class => "button white" %>
My profiles_controller.rb:
def create_inside
#todo = Insidetodo.new
#todo.save!
if #todo.save
redirect_to #profile.todo, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
My todo.rb model:
class Todo < ActiveRecord::Base
has_and_belongs_to_many :profiles
validates :name, :presence => true
end
Is there a way to add in :name => "#{#user.profile.todotext}" to the link_to so that it passes and saves? I don't know if it's creating properly because at the moment when I click a link_to I get a validation error - Validation failed: Name can't be blank.
For passing name in the link_to
<%= link_to "Todo text", {:controller => "profiles", :action => "create_inside", :name => "#{#user.profile.todotext}", :method => :post}, :class => "button white" %>
and the controller must be
def create_inside
#todo = Insidetodo.new(:name => params[:name])
if #todo.save
redirect_to #todo.goal, :notice => 'Todo successfully added.'
else
render :action => 'new'
end
end
But the link_to will pass the name parameter in url only(like Todo text).
If you want the name not to be sent in the url, you might want to use a form and use a submit button instead of the link as it is representing an action and not a link.
<%= form_tag(:controller => "profile", :action => "create_profile") do -%>
<%= hidden_field_tag :name, #user.profile.todotext %>
<%= submit_tag "Todo Text" %>
<% end -%>