Right, so http://localhost:3000/reviews/new/5 tells my app to set up a product review for product with ID 5. However, when I submit the form with bogus information I trigger this code in the 'create' action:
#style = Style.find(params[:review][:style_id])
render 'new'
When the page reloads I get the new URL http://localhost:3000/reviews which is fine, but if I submit bogus info AGAIN, then on the next page load #style fails to be assigned.
This is the code in the 'new' action:
#style = Style.find(params[:id])
#review = current_user.reviews.build(style_id: params[:id])
I'm sure this is simple. Breaking for dinner.
I fixed it by adding an if statement to my view... I changed:
<%= f.hidden_field :style_id, :value => params[:id] %>
to:
<% if params[:id] == nil %>
<%= f.hidden_field :style_id, :value => params[:review][:style_id] %>
<% else %>
<%= f.hidden_field :style_id, :value => params[:id] %>
<% end %>
Instead of this:
<% if params[:id] == nil %>
<%= f.hidden_field :style_id, :value => params[:review][:style_id] %>
<% else %>
<%= f.hidden_field :style_id, :value => params[:id] %>
<% end %>
You could simply do this:
<%= f.hidden_field :style_id, :value => #style.id %>
Related
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!
I am trying to just get an input and send it to controller.
But the params in controller is always nil
View (index.html.erb)
<%= form_tag(:controller => "zip_code_lookup", :action => 'index') do %>
<%= text_field_tag :zip_code, params[:zip_code] %>
<%= submit_tag("go") %>
<% end %>
Controller
class ZipCodeLookupController < ApplicationController
def index
render :text => params[:zip_code].inspect
end
end
You should use params[:zip_code] in your controller, but NOT in your view.
In your index.html.erb view, replace:
<%= text_field_tag :zip_code, params[:zip_code] %>
with:
<%= text_field_tag :zip_code %>
Then, grab the zip_code value using params[:zip_code] in your controller action.
So, your view (index.html.erb)) becomes:
<%= form_tag(:controller => "zip_code_lookup", :action => 'index') do %>
<%= text_field_tag :zip_code %>
<%= submit_tag("go") %>
<% end %>
See this article for some more information on how form_tag works.
I have these forms:
<%= form_for(#user) do |f| %>
<div>
<%= f.number_field :money, :value => #user.money %>
</div>
<% end %>
and
<%= form_for #product, :url => product_path, :html => { :multipart => true } do |f| %>
<div>
<%= f.label :count, 'How Many product?' %><br />
<%= f.number_field :count, :value => "1" %>
</div>
<div>
<%= f.submit('submit') %>
</div>
<% end %>
is there any way to submit this two at once when clicking submit button ? Thanks!
A service object might be a good way to approach this.
class Order
include ActiveModel::Model
attr_accessor :money, :count
def initialize(user=nil, product=nil)
#user = user
#product = product
#money = user.money
#count = 1
end
def persisted?
false
end
def save
// this code needs to save to db
end
end
I'm new to rails and trying to make a simple site to start learning. When I submit my form, however, the data isn't saved to the db. I'm really not sure what's wrong, I've been trying to figure it out for a while. If I make a record in the rails console and save it, that one successfully shows up in the db (and on the index page).
calculate.rb:
class Calculate < ActiveRecord::Base
attr_accessible :number, :root
end
calculates_controller.rb:
class CalculatesController < ApplicationController
def index
#calculate = Calculate.all
end
def new
#calculate = Calculate.new
end
def create
#calculate = Calculate.new(params[:calculate])
if #calculate.save
redirect_to '/calculates'
else
render 'new'
flash[:notice] = "Didn't work"
end
end
end
new.html.erb:
<%= form_for(#calculate) do %>
<%= label_tag(:number, "Enter the number") %>
<%= text_field_tag :number %>
<%= label_tag(:root, "root") %>
<%= text_field_tag :root %>
<%= submit_tag("Submit") %>
<% end %>
if you are using form_for, use the form_for syntax
<%= form_for(#calculate) do |form| %>
<%= form.label :number %>
<%= form.text_field :number %>
<%= form.label :root %>
<%= form.text_field :root %>
<%= form.submit "Submit" %>
<% end %>
this will automatically handle the routes if the #calculate is new object it will submit it to create or if it is already saved it will send a put request to edit action
Ah hah! I updated my view to:
<%= form_for #calculate, :url => { :action => "create" } do |f| %>
<%= f.label :number %>
<%= f.text_field :number %>
<%= f.label :root %>
<%= f.text_field :root %>
<%= submit_tag("Submit") %>
<% end %>
And now it works. Awesome.
My use case is a bit more complicated than the one shown in RailsCasts.
I get an unknown attribute: user error.
Issues and users are related by a many-to-many through another model.
I HAVE specified the accepts_nested_attributes_for in my Issue model.
My view code:
<% semantic_form_for #issue do |form| %>
<% form.inputs do %>
<%= form.input :description, :input_html => { :rows => 5, :cols => 1, :class => 'autogrow' } %>
<%= form.input :location %>
<%= form.input :issue_type %>
<% end %>
<% form.inputs :for => :user do |user_form| %>
<%= user_form.input :email %>
<% end %>
<% form.buttons do %>
<%= form.commit_button "Submit" %>
<% end %>
<% end %>
My Controller code:
def create
#issue = Issue.new(params[:issue])
if #issue.save
flash[:notice] = "Thank you"
else
render :action => 'new'
end
end
Any ideas?
Thanks!
Try using #user instead of :user in the user_form.