Rails - How to send parameters from form to def create in controller

时间:2015-10-31 00:04:50

标签: ruby-on-rails

I have this create function def create @project = current_user.projects.where(id: params[:id]).first_or_create(project_params) if @project.save flash[:success] = "Project created!" redirect_to root_url else flash[:success] = "Project not created!" redirect_to root_url end end What im trying to do is get data to the params:id so i can check if the object already exists. Heres the relevant form <%= bootstrap_form_for(@project) do |f| %> <%= render 'shared/error_messages', object: f.object %> <div class="col-md-4"> <div class="field"> <%= f.text_field :project_title, :value=>params[:project_title] , label: "Title"%> </div> <div class="col-md-4"> <div class="field project-save-button"> <%= f.submit "Save Entry", class: "btn-sm btn-danger" %> </div> <% end %> When i hit the submit button the SQL query generated is SELECT "projects".* FROM "projects" WHERE "projects"."user_id" = ? AND "projects"."project_title" IS NULL ORDER BY "projects"."created_at" DESC LIMIT 1 [["user_id", 1]] How do i properly send through a parameter from form to controller? projects_controller class ProjectsController < ApplicationController before_action :logged_in_user, only: [:create, :destroy] before_action :correct_user, only: :destroy def create if current_user.projects.create(project_params) flash[:success] = "Project created!" redirect_to root_url else flash[:success] = "Project not created!" redirect_to root_url end end def destroy @project.destroy flash[:success] = "Project deleted" redirect_to request.referrer || root_url end def update @project = current_user.projects.find(params[:id]) if @project.update_attributes(project_params) flash[:success] = "Project updated!" redirect_to root_url else flash[:success] = "Project not updated!" redirect_to root_url end end private def project_params params.require(:project).permit(:project_title, :project_subject, :project_type, :project_worth, :project_due_date, :project_details) end def correct_user @project = current_user.projects.find_by(id: params[:id]) redirect_to root_url if @project.nil? end end

2 个答案:

答案 0 :(得分:0)

You could add a hidden field to the form with the projects id: <%= f.hidden_field :id, @project.id %> Update I think the main problem is caused by you trying to use the create action for either creating a project or updating a project. You should really only be creating a new project in the create action and use an update action to update an existing project. You could do something like the below to make your create action work for both but it's a bit hacky: In your view <%= f.hidden_field(:id, @project.id) if @project.persisted? %> In your controller if params[:id] @project = current_user.projects.find(params[:id]) @project.assign_attributes(project_params) else @project = current_user.projects.build(project_params) end if @project.save ... The right way I haven't used bootstrap_form_for but it should work that same as the normal form_for except for the field formatting. When you do: <%= bootstrap_form_for(@project) do |f| %> It should automatically check if @project is persisted or not and make the form a new form if it isn't or make it a edit form if it is. The new form will submit to the create action and the edit form should automatically insert the projects id in a hidden_field and will submit to the update action. Therefore you should be able to keep your original view code but add an update action in your controller like so: Update action def update @project = current_user.projects.find(params[:id]) if @project.update_attributes(project_params) flash[:success] = "Project updated!" redirect_to root_url else flash[:success] = "Project not updated!" redirect_to root_url end end Create action def create @project = current_user.projects.build(project_params) if @project.save flash[:success] = "Project created!" redirect_to root_url else flash[:success] = "Project not created!" redirect_to root_url end end You'll need to make sure you have the update route in your routes too. Probably just: # routes.rb resources :projects Adding an edit form Somewhere in your views you need to create an edit form. This is if you want to make a edit form for all of current_users's projects: <% current_user.projects.each do |project| %> <%= bootstrap_form_for(project) do |f| %> ... # rest of form here etc <% end %> <% end %>

答案 1 :(得分:0)

你只提供create方法,你还能提供渲染html表单的控制器方法吗?我的意思是什么 @project中的<%= bootstrap_form_for(@project) do |f| %> 我认为它应该是你在同一个控制器中的新方法