Presetting a parameter in controller rails - ruby-on-rails

Basically a user is on a project page where they click add new website, which is set out like this
<%= link_to 'Add Asset', new_website_path(:project_id => "#{#project.id}" ), class: 'btn btn-md btn-danger' %>
and then they fill out a form with some data. However I want it so that when they submit that form, that the project id (from the project page they were just on) gets saved under the project_id variable of the website but for some reason this isn't working.
class WebsitesController < ApplicationController
def new
#project = Project.find(params[:project_id])
#website = Website.new(:type => 'Website', :project_id => #project)
end
def create
#website = current_user.assets.build(website_params)
if #website.save
flash[:notice] = "Asset successfully added."
redirect_to(:controller => 'projects', :action => 'show', :id => #website.project_id)
else
render(:action => 'new')
end
end
private
def website_params
params.require(:website).permit(:id, :type,:url, :page_rank, :rev_company ,:social_pages)
end
end
Is there something missing as to why this isn't working? When I check in the console, all the websites I add have a project_id of nil.
When i go onto the form in my browser this is the address:
http://localhost:3000/websites/new?project_id=1
Also, I think it is getting the right project ID because in the form I have a condition which looks at that project ID to see if it is of type website as in:
#views/websites/new.html.erb
<div class="col-md-5 col-md-offset-3">
<div class="panel panel-default newasset">
<div class="panel-heading">
<h3 class="panel-title ptd"><%= "New #{#project.type}" %></h3>
</div>
<div class='panel-body'>
<div class='form group'>
<%= simple_form_for(#website, html: {class: 'form-group' }) do |f| %>
<% if (#project.type == 'Website') %>
<div class="col-xs-4 col-xs-offset-3">
<%= f.input :title, label: 'Website Name' %>
<%= f.input :url %>
<%= f.input :rev_company, label: "Revenue Company" %>
<%= f.input :social_pages, label: "Number of Social Sites" %>
<%= f.input :page_rank %><br />
<div class="form-actions">
<%= f.button :submit, class: 'btn btn-primary' %>
</div>
<% else %>
You should not see this
<% end %>
<% end %>
</div>
</div>
</div>
</div>

Assuming that you are passing :project_id correctly then all you need to permit project_id in website_params method.
def website_params
params.require(:website).permit(:id, :project_id, :type,:url, :page_rank, :rev_company ,:social_pages)
## ^
## Permit project_id
end
Currently, you have not permitted the field project_id which is why its not getting saved.
For Rails 4+ (with Strong Parameters) you need to explicitly specify the fields which you would like to save in database.

#project is an object, and the database must be expecting an integer.
You should call:
#project.id
The best solution anyway is to do:
def new
#project = Project.find(params[:project_id]
#website = #project.websites.build(:type => 'Website')
end
To allow rails to do his magick association through the models if you have declared the: belongs, and has_one/many

Related

Submit values with link_to in Rails 5.1

So currently i have a link_to, where signed in users can click on:
<%= link_to "Enroll", [#task.project, #task] %>
The user has an association with the project, through subscription. To create a new subscription for a user with a project, i wrote some simple form for it.
<%= form_for([#project, #subzz]) do |f| %>
<%= f.hidden_field :project_id, :value => #project.id %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Which works fine and creates the association. However, i want that the user is able to create the subscription whenever he clicks on 'enroll' instead of a second, extra submit button.
Any ideas how to approach this? I thought about using jQuery, but not sure how to inject the ids with it and if its the 'right' way to do it.
Thanks in advance everyone!
EDIT:
When using the method posted as answer, i get:
param is missing or the value is empty: sub
My updatet form:
<%= form_for([#project, #subzz], html: {role: "form", id: "project_form"}) do |f| %>
<%= hidden_field_tag :project_id, :value => #project.id %>
<%= hidden_field_tag :user_id, :value => current_user.id %>
<%= link_to "Enroll", [#task.project, #task], :onclick => "$('#project_form').submit() "%>
<% end %>
subs_controller.rb
class SubsController < ApplicationController
def create
#subz = Sub.create(sub_params)
project = #subz.project
redirect_to root_path
end
private
def sub_params
params.require(:sub).permit(:project_id, :user_id)
end
end
You can be using the existing form and link_to, just edit some like edit the dorm_tag like this
<%= form_for([#project, #subzz], html: {role: "form", id: "project_form"}) do |f| %>
and remove the button into form like this one
<div class="actions">
<%= f.submit %>
</div>
and edit the link_to like this
<%= link_to 'Enroll', "", :onclick => "$('#project_form').submit()" %>
it will work
Update
You can achieve this without a form, comment out this form and edit the link like below
<%= link_to 'Enroll', subs_path(project_id: #project.id, user_id: current_user.id), method: :post %>
and the create method update like below
def create
#subz = Sub.new(sub_params)
if #subz.save
flash[:success] = 'Sub was successfully submited.'
redirect_to root_path
else
flash[:danger] = 'Sub not submited'
redirect_to request.referer
end
end
that is easier
Or if you keep before one with form then the link out from the form and the create method edit like the following
def create
#subz = Sub.new(sub_params)
if #subz.save
flash[:success] = 'Sub was successfully submited.'
redirect_to root_path
else
flash[:danger] = 'Sub not submited'
redirect_to request.referer
end
end
and the form will look like this
<%= form_for([#project, #subzz], html: {role: "form", id: "project_form"}) do |f| %>
<%= hidden_field_tag :project_id, :value => #project.id %>
<%= hidden_field_tag :user_id, :value => current_user.id %>
<% end %>
<%= link_to "Enroll", [#task.project, #task], :onclick => "$('#project_form').submit() "%>
if you confused this [#task.project, #task] on link tag then use direct link
So i came up with the following solution:
I've added the sub handling to the application_controller, so that its availiable for the project_controller. I also added the project, tasks as a reference, so that i am able to redirect to a task via the sub_controller, instead of the project_controller.
application_controller.rb
def create
#subs = Sub.new(sub_params)
project = #subs.project
taskz = project.tasks.first
if #subs.save
redirect_to [taskz.project, taskz]
end
end
private
def sub_params
params.require(:sub).permit(:project_id, :user_id)
end
Inside the show.html.erb from the project_controller, i use the old form:
<%= form_for([#project, #subz] do |f| %>
<%= f.hidden_field :project_id, :value => #project.id %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.submit "Submitted" %>
<% end %>
which works fine. Thanks for any previous help!

undefined method `model_path' when using resource :model for routing in form_for edit

There are a few similar questions that exist to this, but I could not clearly understand the answer. I am trying to make a simple rails application with CRUD operations for a specific model. I am specifically having trouble with doing this using the form_for function for updating the record.
My routes.rb file:
Rails.application.routes.draw do
root 'main#index'
devise_for :users
resource :models
get 'main/index'
end
My models_controller.rb
class ModelsController < ApplicationController
def edit
#nc = Model.find(params[:id])
end
def update
#nc = Model.find(params[:id])
#nc.update(model_params)
if #nc.save
flash[:success] = 'Changes saved.'
redirect_to model_path
else
flash[:error] = #nc.errors.empty? ? 'Error' : #nc.errors.full_messages.to_sentence
render :action => :edit
end
end
def model_params
params.require(:model).permit(:first_name, :last_name)
end
end
and finally my model/edit.html.erb
<%= form_for #nc do |nc| %>
<div class='container-fluid'>
<form role='form'>
<div class='row'>
<div class='col-md-6'>
<div class='form-group'>
<%= nc.label :first_name %>
<%= nc.text_field :first_name, :class => 'form-control', :value => #nc.first_name != nil ? #nc.first_name : '' %>
</div>
<div class='form-group'>
<%= nc.label :last_name %>
<%= nc.text_field :last_name, :class => 'form-control', :value => #nc.last_name != nil ? #nc.last_name : '' %>
</div>
</div>
</div>
<div class='row'>
<div class='col-md-12'>
<%= nc.submit 'Submit', class: 'btn btn-primary' %> <%= link_to 'Cancel', :back, class: 'btn btn-default' %>
</div>
</div>
</form>
</div>
<% end %>
When I actually run this and attempt to edit the model, before I can submit anything I get:
undefined method `model_path' for #<#<Class:0x00000003cb52c0>:0x007fb7022b18b0>
Did you mean? models_path
Change
redirect_to model_path
to
redirect_to #nc
If you run your rake routes, you'll see that model_path requires an id - using #nc tells rails to do what you want it to do.

Send multiple invitation using devise-invitable gem in rails

I want to send multiple invitations at a time using devise-invitable gem.
new.html.erb
<h2>
<%= t "devise.invitations.new.header" %>
</h2>
<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %>
<%= devise_error_messages! %>
<div>
<% resource.class.invite_key_fields.each do |field| -%>
<p><%= f.label field %><br />
<%= f.email_field field, placeholder: "Invitation email" %></p>
<% end -%>
<%= f.collection_select(:role_id, Role.all, :id, :role_name, :prompt => true) %>
</div>
<p>
<%= f.submit t("devise.invitations.new.submit_button") %>
</p>
<% end %>
my controller:-
class Users::InvitationsController < Devise::InvitationsController
def create
exit
if params[:user][:email]== "" || params[:user][:role_id] == ""
flash[:alert]="Please enter email first and Select any role for invitees"
redirect_to new_user_invitation_path
else
if User.invite!(:email => params[:user][:email], :company_id => current_user.id, :type => 'Employee', :role_id => params[:user][:role_id])
flash[:notice]="Invitation is send successfully."
redirect_to new_user_invitation_path
else
flash[:alert]="Invitation is not send."
redirect_to new_user_invitation_path
end
end
end
end
I think one solution is to pass comma separated emails in invite method but how can I pass it? I really don't know how.
If you have any other solution then please tell me.
Thanks.
i trouble with this problem but finally i got solution to send multiple invitation email at a time.
below i explain my code that how i become possible this.
here is my html view.
new.html.erb
<h2>
<%= t "devise.invitations.new.header" %>
</h2>
<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %>
<%= devise_error_messages! %>
<div>
<% resource.class.invite_key_fields.each do |field| -%>
<%= f.label field %><br />
<%= f.email_field field, name: "user[email][]", placeholder: "Invitation email", required: true %></p>
<div id="createNewTextbox" >
</div>
<a id="btnLinkCreate" href="#" onClick="create_new();">
+ INVITE MORE
</a>
<% end -%>
</div>
<p>
<%= f.submit t("devise.invitations.new.submit_button") %>
</p>
<% end %>
in this i take one text-box by-default with name : "user[email][]" (it is important because using this rails automatically create email array and send in params whene you submit form )
i also generate dynamic text-box using JavaScript and it will create below div when click on invite more link button :
<div id="createNewTextbox" >
</div>
<a id="btnLinkCreate" href="#" onClick="create_new();">
+ INVITE MORE
</a>
here is my dynamic text-box code of JavaScript.
$('#createNewTextbox').append('<input type="email" id="email'+i+'" name="user[email][]" placeholder="Invitation email" required/>');
now you can see that i give same name to dynamic text-box (name="user[email][]"), so rails automatically create hash array like this:
"user" => { "email" => { "email-1","email-2","email-3"... } }
now this hash array is pass in create method in which we fetch every email from params and give it to invite method to send the invitation.
my controller :-
class Users::InvitationsController < Devise::InvitationsController
def create
params[:user][:email].each do |email|
User.invite!(:email => email)
end
end
end
thats it...
if still you have any query then tell comment me.

Rails - how to send some information from one form to other controller

Firstly, I would like to describe what I am going to do. So, my application is set of tasks (eg. exercises depend on writing computer program) which user it available to send solutions through form and server will have compiled it and return results of process.
My problem is that I have model Solution which is presented below:
Solution(id: integer, exercise_id: integer, code: string, user_id: integer,
created_at: datetime, updated_at: datetime, language_id: integer)
and exercise_id, user_id, language_id are foreign keys and postgresql return error that it couldn't be null values...
The form which I use to send this information looks like below:
<fieldset style="add">
<legend>Send solution</legend>
<%= form_for [#user, #exercise, #solution], url: exercise_solutions_path, :html => { role: "form" } do |f| %>
<div class="form-group">
<%= f.label "Source code" %>
<br>
<%= f.text_area :code %>
</div>
<div class="form-group">
<%= f.label "Programming language" %>
<br>
<%= f.select(:language_id, available_lang) %>
</div>
<div class="form-group">
<%= f.submit "Send", :class => "btn btn-default" %>
</div>
<% end %>
</fieldset>
Frankly speaking I don't how to include more information such as user.id or exercise.id. Params look like:
#_params
{"utf8"=>"✓", "authenticity_token"=>"abwhTn3KrVneli0pCVccoWHyzlI7cRTFK5Wo7DdYRK+lL4GwZ8jUPqdzn8ZznCHsWTkfDAFS2RP6gTkWuk33iw==", "solution"=>{"code"=>"ds", "language_id"=>"C++"}, "commit"=>"Wyślij", "controller"=>"solutions", "action"=>"create", "exercise_id"=>"1"}
SolutionController
class SolutionsController < ApplicationController
def index
#solutions = Solution.last(20)
end
def new
#solution = Solution.new
end
def create
#solution = Solution.new(solution_params)
if #solution.save
redirect_to #solution
else
render :new
end
end
def edit
#solution = Solution.find(params[:id])
if #solution.update(solution_params)
redirect_to #solution
else
render :edit
end
end
def destroy
end
private
def solution_params
params.require(:solution).permit(:language_id, :code)
end
end
Edit:
<fieldset style="add">
<legend>Send solution</legend>
<%= form_for [#user, #exercise, #solution], url: exercise_solutions_path, :html => { role: "form" } do |f| %>
<div class="form-group">
<%= f.label "Source code" %>
<br>
<%= f.text_area :code %>
</div>
<div class="form-group">
<%= f.label "Programming language" %>
<br>
<%= f.select(:language_id, available_lang) %>
</div>
<div class="form-group">
<%= f.hidden_field :exercise_id, value: #exercise.id %>
<%= f.hidden_field :user_id, value: #user.id %>
<%= f.submit "Send", :class => "btn btn-default" %>
</div>
<% end %>
</fieldset>
You could 1) add hidden_fields in your form to set the missing attributes:
<div class="form-group">
<%= f.hidden_field :exercise_id, value: #exercise.id %>
<%= f.hidden_field :user_id, value: #user.id %>
<%= f.submit "Send", :class => "btn btn-default" %>
</div>
Make sure to add these attributes to the solution_params permitted parameters:
def solution_params
params.require(:solution).permit(:language_id, :code, :exercise_id, :user_id)
end
A better idea would be to manually set the exercise and user in the controller and leave your solution_params as is. That way you're not allowing the exercise and user to be set by mass assignment, which could be spoofed.
So instead do 2) in your controller:
def create
#solution = Solution.new(solution_params)
#solution.exercise_id = params[:exercise_id]
#solution.user_id = params[:user_id]
if #solution.save
redirect_to #solution
else
render :new
end
end
I'm assuming that :exercise_id and :user_id are in the URL. If not add them as hidden_fields as needed (but this time not on the form object):
<%= hidden_field_tag :user_id, #user.id %>
Also remember to actually select a language_id from the dropdown before you submit the form.

Ruby on Rails - Form for multiple new Objects

I got a backend(Namespace admin) and want to create 4 new database entries with 1 form submit(4 new countries). What I got so far:
In my countries_controller.rb
class Admin::CountriesController < Admin::AdminController
def new
#countries = Array.new(4) { Country.new }
end
end
In my new.html.erb:
<%= form_for [:admin, #countries] do |f| %>
<% #countries.each do |country| %>
<div class="row">
<div class="col-md-6">
<div class="form-group col-md-6">
<%=f.text_field :country, :name, :class => "form-control", :placeholder => "Country 1" %><br>
<%=f.text_field :country, :iso, :class => "form-control", :placeholder => "us" %>
</div></div></div>
<% end %>
<% end %>
But that doesn't work and I get a undefined method model_name for Array:Class error. What is the right way to do this?
form_for is for a single ActiveRecord object, but you're using it with an Array of object :
<%= form_for [:admin, #countries] do |f| %>
Either create a form per object (with each form a save button):
<% #countries.each do |country| %>
<%= form_for [:admin, country] do |f| %>
...
<% end %>
<% end %>
Or see this question for a solution using only one form: Multiple objects in a Rails form

Resources